Variables in Python
Variables are containers that are used to store data. Python is a dynamically-typed language which means variables can store any type of data such as integers, float, strings, etc.
Let us catch a glimpse of variables in this in detail.
The above code sets the value 5 to the variable 'a'. When you print a variable, the value associated with the variable will be printed. Thus the output for the above code will be 5.
Just like that, variables can have any type of data.
In the above code, we have different variables of different types. 'a' is of integer type, 'b' is of float, 'c' is of string type and 'd' is of boolean type.
When we define a variable, Python automatically analyses the type of assigned value and assigns it automatically to the variable so that we can define variables without defining their type.
Here, multiple values are assigned to the same variable one after another. So what will be the value of 'a' at the end of the program?
If you said 15, then you are correct. Because when a new value is assigned to a previously assigned value, then the newly assigned value will replace the previously assigned value.
Therefore at line 2 of the above code value of 'a' will be replaced from 5 to 10 and at line 3, the value of 'a' will be replaced from 10 to 15. So, in the end, the value of 'a' is 15.
In the above code, at line 2, the value of 'a' is changed from 5, which is an integer to 'lowercasepython', which is a string. When a value of different type replaces the previous value, the variable will automatically convert to the type of new variable.
Hope this gives you a clear idea about variables.
Happy coding!
Let us catch a glimpse of variables in this in detail.
Declaring variables in Python:
We might have seen in languages like C and Java where we have to define a variable with the type of data it will hold before assigning value to the variable. Unlike that, in Python, We don't have to explicitly define variables and their types. To create a variable, just type the name of the variable, use '=' sign, and assign a value.
a = 5
print(a)
print(a)
The above code sets the value 5 to the variable 'a'. When you print a variable, the value associated with the variable will be printed. Thus the output for the above code will be 5.
Just like that, variables can have any type of data.
a = 5
b = 10.3
c = 'lowercasepython'
d = True
b = 10.3
c = 'lowercasepython'
d = True
In the above code, we have different variables of different types. 'a' is of integer type, 'b' is of float, 'c' is of string type and 'd' is of boolean type.
When we define a variable, Python automatically analyses the type of assigned value and assigns it automatically to the variable so that we can define variables without defining their type.
Multiple Assignments to the Same Variable:
Consider the below code,
a = 5
a = 10
a = 15
a = 10
a = 15
Here, multiple values are assigned to the same variable one after another. So what will be the value of 'a' at the end of the program?
If you said 15, then you are correct. Because when a new value is assigned to a previously assigned value, then the newly assigned value will replace the previously assigned value.
Therefore at line 2 of the above code value of 'a' will be replaced from 5 to 10 and at line 3, the value of 'a' will be replaced from 10 to 15. So, in the end, the value of 'a' is 15.
a = 5
a = 'lowercasepython'
a = 'lowercasepython'
In the above code, at line 2, the value of 'a' is changed from 5, which is an integer to 'lowercasepython', which is a string. When a value of different type replaces the previous value, the variable will automatically convert to the type of new variable.
Hope this gives you a clear idea about variables.
Happy coding!
Comments
Post a Comment