Python Variables and Object References

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.

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
print(m)
m = "It's a beautiful day to save lives"
print(m)
Output:
5
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
print(m)
print(n)
print(l)
Output:
1
1
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
print(m)
print(n)
Output:
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]
print(m)
print(n)
print(l)
print(s)
Output:
25
It's a beautiful day to save lives
2.52
[2,8,9,15]

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
print(c)
print(d)
Output:
40
20

3. Rules and naming convention for creating variables

  1. 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.
  2. We should always use a descriptive name.
    For example: we can use count instead of x.
  3. For the variable names with more than one word, use underscore(_) as a separator.
    For example: employee_name or tax_percentage
  4. 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.
  5. We should never use special symbols like !, @, #, $, %, etc

NOTE: Rules of naming python variables are the same as rules to name identifiers. For more details please refer to Keywords and Identifiers in Python.


4. Types of Variables in Python

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.

m=500 (variable allocation)
Variable allocation when m=500

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.

Same Variable reference (m=n)
Same variable reference when m=n

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.

m=500
n=500
print(id(m))
print(id(n))
Output:
2660138002928
2660138002928

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.

m = 600 ; m = 700

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.


6. Conclusion

In this article we learned about :

  • How to store values in a variable
  • Rules to create a Python variable
  • How does object reference work
  • What is the garbage collection in Python

Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books


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!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index