DEV Community

Davisonyeas
Davisonyeas

Posted on

Getting Started With Python PyStart(Variables)

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;
Enter fullscreen mode Exit fullscreen mode
y = "Davis"
Enter fullscreen mode Exit fullscreen mode

To display the result of this, We make use of the print() built-in function;

print(x)
Enter fullscreen mode Exit fullscreen mode
print(y)
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode
print(type(y))
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
Bad: 23spam  #sign   var.12
Enter fullscreen mode Exit fullscreen mode
Different: spam   Spam   SPAM
Enter fullscreen mode Exit fullscreen mode

Top comments (0)