Everything you need to know about variables in Python

Codewithgomez
5 min readDec 8, 2021

--

Photo by Kaushal Moradiya from Pexels

The concept of variables is common in almost every programming language. Programs need a way to store data during their lifecycle. By lifecycle I mean from the point when the program starts running to the time it’s terminated. For persistent storage then there are other options like databases.

In this article, I will discuss everything you need to know about variables in python. Well, I will not exhaust everything but I will cover a lot.

A Python variable is a symbolic name that is a reference or pointer to an object in memory.

To declare and assign a value to a variable we use the assignment operator followed by the value.

# use the assignment operator followed by the value.
name = “Joe”
age = 13
height = 8.1

The following is what happens under the hood when you assign a value to a variable.

  • An object is created in memory with the provided value.
  • The variable/variable name points to the created object.

Take for example the variable age, the object is created in memory with the value 13. Then the variable name age points to the object in memory, so much so that when we print(age) it retrieves the value stored in memory.

An object with multiple pointers

Since variables are pointers to objects in memory, an object may have multiple variables pointing to it.
Take a closer look at the code below.

age = 13
myAge = age

At first glance of the above code, you may think python will create two objects, but that’s not the case. Since we are just assigning the value of age to myAge, python does not create a new object. It just creates a new pointer to the same object that age is pointing to.

Object Identity

In Python, every object that is created is given a number that uniquely identifies it.
Object identity guarantees that there will be no single time during the lifecycle of a program where two objects will have the same identifier.
To check the id of an object we use the id() function.

#Let’s use the same example as we used above:
age = 13
myAge = age
print(“The object identifier for age: “, id(age))
print(“The object identifier for myAge: “, id(myAge))

output:
The object identifier for age: 4459862752
The object identifier for myAge: 4459862752

We can see that the identifiers are the same. As I said, this time around we are not creating another object for myAge because we have assigned it the value of an object that is already created.

#Let’s look at another example:
#remember that the value of age was 13 at first
age = 18
myAge = 20

We have assigned new values to our variables, therefore two new objects will be created. One with the value 18 and another with the value 20.
Now that age and myAge are pointing to new objects in memory, the object with the value 13 becomes an orphan and is deleted via the process called garbage collection. Garbage collection is the process of cleaning the memory of unreferenced objects.

Python is a dynamically typed programming language

Maybe you have heard before that python is a dynamically typed language, but what does that mean?
With dynamically typed languages like Python, you are not required to specify the variable type in advance. Python automatically identifies the variable type under the hood. Because of this, a variable may be assigned a value of one type and then later re-assigned a value of a different type.

In the code below you will see that even though we did not specify the type, python identifies the type. We can verify this by using the type() function.
The type() function is used to identify the type of an object in Python.

name = “Joe”
type(name)

Output:
str

Assign the same value to several variables simultaneously.

In Python, you can assign a value to multiple variable names.
All you need to do is separate the variable names by an assignment operator.

x= y = z = 10

Assigning multiple values to multiple variables.

You can also Assign the values to multiple variables on the same line.

x, y ,z = 10, 23 ,90

Rules for naming variables

Python allows you to name variables to your liking, as long as the names follow the following rules.

  • 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 (_)
  • Never use special symbols like !, @, #, $, %, etc.
  • Don’t start a variable name with a digit.
  • No variable name can have the same name as a reserved word.

Naming conventions

PEP 8 recommends that Snake Case should be used for functions and variable names.
When multiple words are used to form a variable, the snake case joins those words together, with an underscore to create separation.

this_is_snake_case =10
student_name = “Gomez”
student_id = 14

Conclusion

As I said, the topic of variables is very important in programming. I hope you have learned a couple of things you did not know about variables in Python. If you want to learn more about Python, feel free to check out my course at codewithgomez.net.
The following are some of the topics I have covered.

Python datatypes
Python Flow Control
Python Functions
Object-oriented programming with Python
File handling in Python

--

--

Codewithgomez

Gomez is a freelancer whose real passion is teaching web development & programming in a simple and understandable way. He now runs codewithgomez.net.