As in other programming languages, there are variables in Python programming. Python variables are the containers that we store data values in them. We can assign a python number, a list, a tuple etc. to Python variables.
So, how can we create variables in Python? We can do this easily by assigning a value to a variable. When we assing this value, the variable is automatically created. This value can be a number a string, a list a tuple, a set, a dictionary etc.
The names of the variables are case sensitive. So, the variable that you create with lower case is not the same variable that is created with upper case. As an example, a and A are two different variables in python programming.
You can learn also Python Lambda Function
Now, to see how to use variables in Python, let’s do a simple example in which we will assign different values to different variables.
The output will be:
You can also watch the video of this lesson!
We can change the value of a variable multiple times in our code. We can assign a value in one type and then we can change it also to another data type. For example, firstly we can assign a string value to a variable and then we can change it as list type by assigning a list.
You can also Download Python Cheat Sheet!
While we assign a value to the variables, if it is the same value, we can do this in one line. Below, you can find an example of this usage.
The output will be:
We can also assign the values of one more variable by using comma (,).
We can use different variable names in python programming. This can be one letter like a, b, x etc., one more letter or a combination of letters, digits and underscore character (_).
A variable name can start with a lower case, upper case or underscore character. But a python variable name can not start with a digit. Digits can only used in the other characters.
The output of this code will be like below:
In Python programming, we can see the type of a variable with type() function. In other words, type() function gives us the data type. Let’s show this with an example.
As you can see, the class of each data type is given below:
Here, we have talked about one of the beginning lessons of python programming, Python variables. You will use variables always in your codes in different formats.