The concept of variables is same in any programming language. A variable is nothing but a memory location that is capable of storing some data or a specified value. Whenever you create a variable you reserve some space in memory to store the desired information.
You can consider a variable as a box with some empty space. Here a value or a data is your item that you want to store in this box. Now to remember in which box you have stored a particular item you will write some name to identify the box. For example, if you are storing all your books in a box then you will name that box as “Books” or “Novels” to remember that this box contains your favorite collection of novels.
Data types as defined in our Top 20 Programming Terms that Everyone Should Know article are the means to identify the types of data used in a programming language. With the help of data types, we can identify the type of data we working with (int, float, double, string, char) and then perform associated operations for handling that type of data. In other programming languages, you are supposed to specify the data type along with the name of the variable in order to create a variable.
In Python, a variable is created by just assigning a value to the name of the variable without explicitly defining the data type. This is the syntax of creating a variable in python.
num = 12 # integer name = "Edward" # string sum = 12.5 # float print(num) print(name) print(sum)
Since Python is a dynamically typed language it automatically understands the type of variable in the code just by assigning the values. In the given example num is an integer variable because it is assigned value 12, name is a string variable since it is assigned the value “Edward” and sum is a float variable since it is assigned 12.5
Assignment Operator ( = ) :
“Equal to” symbol is the assignment operator in python. It is used to assign values to the variables. The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. For example, variable num is an integer assignment, the variable name is a string assignment and sum is a float assignment.
In Python, you can assign values to more than one variable in a single python statement simultaneously.
x, y, z = 3, 4.5, 'peters'
Now variables x will contain 3, y will contain float value 4.5 and z will have string ‘peters’. The value will be assigned in the exact order written in the statement.