1.0 VARIABLES
Variables are containers for storing data values. Python has no command for declaring variable. A variable is created the moment you assign a value to it.
For example,
x = 4;
y = "Davis"
To display the result of this, We make use of the print() built-in function;
print(x)
print(y)
Output
4
Davis
Note:
In Python, we do not have to say x = int 4; just as we have in other programming languages like Java, Python automatically detects the data type of the value)
Also, when declaring variable names, we take note of keywords, there are some reserved keywords in python that shouldn't be used as a variable name, some of such keywords includes return, is, continue, def, finally, and a few other words. Read further on Python keywords Click here for Python keywords
1.1 Checking the data type of the variable
It is important that we know how to check data types in Python, this is a basic concept that will come in handy when we get to more advanced topics like Data Preprocessing and Analysis.
We check the type of our data by using the type() method.
print(type(x))
print(type(y))
Output
int
str
1.2 Python Variable Naming Rules
The following are the some good, bad and different naming conventions.
The Good shows some good naming conventions that can be used and that is accepted by developers.
The Bad shows some bad practices that shouldn't be used while coding.
The Different shows that each individual variable is independent of the others, they all have the same spelling but the fact that some are capital letters and some aren't. They are all different.
Good: spam eggs spam23 _speed
Bad: 23spam #sign var.12
Different: spam Spam SPAM
Top comments (0)