Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Variables in Python
In the previous tutorial on Basic Data Types in Python, you saw how values of various Python data types can be created. But so far, all the values shown have been literal or constant values:
>>> print(5.3)
5.3
If you’re writing more complex code, your program will need data that can change as program execution proceeds.
Here’s what you’ll learn in this tutorial: You will learn how every item of data in a Python program can be described by the abstract term object, and you’ll learn how to manipulate objects using symbolic names called variables.
Free PDF Download: Python 3 Cheat Sheet
Take the Quiz: Test your knowledge with our interactive “Python Variables” quiz. Upon completion you will receive a score so you can track your learning progress over time:
Variable Assignment
Think of a variable as a name attached to a particular object. In Python, variables need not be declared or defined in advance, as is the case in many other programming languages. To create a variable, you just assign it a value and then start using it. Assignment is done with a single equals sign (=):
>>> n = 300
This is read or interpreted as “n is assigned the value 300.” Once this is done, n can be used in a statement or expression, and its value will be substituted:
>>> print(n)
300
Just as a literal value can be displayed directly from the interpreter prompt in a REPL session without the need for print(), so can a variable:
>>> n
300
Later, if you change the value of n and use it again, the new value will be substituted instead:
>>> n = 1000
>>> print(n)
1000
>>> n
1000
Python also allows chained assignment, which makes it possible to assign the same value to several variables simultaneously:
>>> a = b = c = 300
>>> print(a, b, c)
300 300 300
The chained assignment above assigns 300 to the variables a, b, and c simultaneously.
Variable Types in Python
In many programming languages, variables are statically typed. That means a variable is initially declared to have a specific data type, and any value assigned to it during its lifetime must always have that type.
Variables in Python are not subject to this restriction. In Python, a variable may be assigned a value of one type and then later re-assigned a value of a different type:
>>> var = 23.5
>>> print(var)
23.5
>>> var = "Now I'm a string"
>>> print(var)
Now I'm a string
Object References
What is actually happening when you make a variable assignment? This is an important question in Python, because the answer differs somewhat from what you’d find in many other programming languages.
Python is a highly object-oriented language. In fact, virtually every item of data in a Python program is an object of a specific type or class. (This point will be reiterated many times over the course of these tutorials.)
Consider this code:
>>> print(300)
300
When presented with the statement print(300), the interpreter does the following:
- Creates an integer object
- Gives it the value
300 - Displays it to the console
You can see that an integer object is created using the built-in type() function:
>>> type(300)
<class 'int'>
A Python variable is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name. But the data itself is still contained within the object.
For example:
>>> n = 300
This assignment creates an integer object with the value 300 and assigns the variable n to point to that object.
