In this Python article, we will discuss Python variables in detail and how object references work.
We learned about Identifiers in one of our previous articles. So now we will see how we use those identifiers in our program.
Table of Contents
1. What is a Variable in Python?
Variables are the most fundamental aspect of any programming language.
Variables are like containers or boxes which we used to store data. Assume when we are shifting from a place to another, we separate our items and keep them in different boxes. Then we label those boxes so that we know what is there in the box inside. In Python programming, boxes are like variables and those name tags are our identifiers.
Python variables are dynamically typed as we assign different data types to the same variable.
Whenever we think we require a variable, we assign a value to it and the interpreter will automatically decide the data type. We can use the same variable again to store values of different data types.
Let’s discuss the dynamically typed nature of Python using an example.
m = 5
m = "It's a beautiful day to save lives"
It's a beautiful day to save lives
2. Assigning variables
2.1. Single variable assignment
Variable assignment is done by using = operator. Example
m = 500
n = 2.5
l = "It's a beautiful to save lives"
2.2. Assign same values to multiple variables
Let’s implement the example to assign the same value to multiple Python variables in a single line.
m = n = l = 1
2.3. Assign multiple variables of same data type
Let’s implement the example to assign the different values to multiple Python variables in a single line.
m , n = 500 , 600
2.4. Assign multiple variables of different data type
Let’s implement the example to assign the values of different data types to multiple Python variables in a single line.
m , n , l , s = 25 , "It's a beautiful day to save lives" , 2.52, [2,8,9,15]
It's a beautiful day to save lives
2.5. Assign multiple variables with calculation
Let’s implement the example to assign the values to multiple Python variables while performing some operations on them.
a , b = 10 , 30
c , d = a+b , b-a
3. Rules and naming convention for creating variables
Constant and variable names should have a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). But we can not start a variable name with a digit.
We should always use a descriptive name. For example: we can use count instead of x.
For the variable names with more than one word, use underscore(_) as a separator. For example: employee_name or tax_percentage
Use capital letters possible to declare a constant. This is just a standard, other naming conventions like camel case or any other is equally valid. For example: PI or MAX_VALID_SUM etc.
We should never use special symbols like !, @, #, $, %, etc
Based on the scope or on the location we define a variable, a variable can be of 3 types :
A variable is called a Local variable if it is defined within a function scope or a class or we can also say if it is accessible within a boundary.
If a variable is passed as function parameters then it is known as formal parameters.
If a variable is declared outside of all functions or classes such that it is accessible everywhere then it is known as a global variable.
We will discuss these variables in detail in future article.
5. Object References
Sometimes we wonder what exactly happens in the background when we assign a value to a variable. An interesting point to remember is that this answer differs for different programming languages.
Since Python is a highly object-oriented language and each data type is represented by an object, so whenever we assign a value to a variable, it creates an object into the memory that contains the actual object value, and the variable acts like a symbolic name that points or refer to the object.
Let’s go through an example in which we assign value to different variables and then update them.
So here we are assigning our integer object a name tag by doing m = 500. So it will be easy to access it.
Now, when we assign the integer object of value 500 to another variable (n=500 or m=n), then that simply means we have two different variables that are referencing/pointing to the same object.
We can confirm the above theory by using id() the function which returns the id of the object. id() function in python returns the unique identity of the object.
Now when we change the value of variables then it will remove the reference from the previous object and will refer to the newly assigned object.
When the reference to an object drops to zero then its lifetime is over. The compiler will automatically detect that the object is inaccessible, so it will delete the object to create space for other objects. This memory is then freed by the compiler so that new objects can be created using this space. This process of freeing up memory is called garbage collection.
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding. Do not forget to share and Subscribe. Happy coding!! 😊
Python Variables and Object References was last modified: February 7th, 2021 by Lakshika Parihar