Assignment isn't just about the equals sign PREMIUM

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read 4 min. video Python 3.10—3.14
Python Morsels
Watch as video
03:33

Assignment in Python isn't just about the equal sign.

What is assignment?

Usually when we think of assignment, we think of something like this:

>>> x = 4
>>> x
4

We took the variable x and assigned it to the value 4.

And here we're setting the variable numbers to some list of numbers:

>>> numbers = [2, 1, 3, 4, 7]

For loops assign to variables

Let's loop over this list of numbers with a for loop:

>>> for x in numbers:
...     print(x*2)
...
4
2
6
8
14

In each iteration of this loop, we actually did an assignment.

That for x in numbers line assigns to the variable x over and over in our loop. So the thing in between the for and the in, within the first line of a for loop does the same thing as the left-hand side of an equal sign.

That means that x is now 7 because that was the last value that x was assigned to in our loop:

>>> x
7

Just as an equals sign causes a variable assignment, for loops also do an implicit assignment for each iteration of the loop.

The "import" statement does an assignment

Python's import statement also does an assignment.

Let's assign the variable statistics to the string 'important':

>>> statistics = 'important'

Now let's import the statistics module from the Python standard library:

>>> import statistics

If we look at the variable statistics now, we'll see that it's not important anymore: it's a module object now!

>>> statistics
<module 'statistics' from '/usr/lib/python3.9/statistics.py'>

Anytime you do something and a variable changes its value (or comes into existence), an assignment must have occurred.

When you import a module in Python, Python will give you access to that module object by assigning a variable to it.

Using the from syntax with importing also does an assignment:

>>> from math import pi

This doesn't give us access to math:

>>> math
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined

But it does assign the variable pi to the number π (3.141592653589793):

>> pi
3.141592653589793

Function definition does an assignment too

Defining a function also does an assignment in Python!

Here we've defined a function called pi:

>>> def pi():
...    print('pi is lovely')
...

We previously had a variable pi which pointed to a number. Now pi points to a function object:

>>> pi
<function pi at 0x7fd555e9a8b0>

That's the way Python gives us access to a function object: it points a variable to it.

Assignments are all over the place in Python. Anytime a variable changes its value or a new variable appears, an assignment must have occurred.

Assignment expressions

In Python 3.8, they added a new assignment syntax which uses :=. It looks like this:

>>> x = 100
>>> while (x := x/2) > 1:
...     print(x)
...
50.0
25.0
12.5
6.25
3.125
1.5625

We're repeatedly assigning x to x / 2 in each iteration of our while loop. We're repeatedly taking the result of x/2 and we're both assigning that value back into the variable x and checking the result of that x/2 expression to see whether it's greater than 1. As long as that x value is greater than 1, we print out x.

Essentially we're repeatedly dividing x by 2 and printing it out until it's less than 1.

So the value of x is now less than 1 (because our while loop completed):

>>> x
0.78125

The := syntax is called an assignment expression and that := operator is often called the walrus operator because it kind of looks like a sideways walrus with its tusks hanging out.

Assignment expressions are a little bit cryptic and I wouldn't recommend them in many cases, but they're good to know about.

Summary

Variable assignment in Python is not all about an equal sign.

Python's for loops do an assignment for each iteration of the loop. And import statements do an assignment. Even function definitions do an assignment (they assign a variable to a new function object).

Any time a variable changes its value, an assignment has occurred.

Series: Assignment and Mutation

Python's variables aren't buckets that contain things; they're pointers that reference objects.

The way Python's variables work can often confuse folks new to Python, both new programmers and folks moving from other languages like C++ or Java.

To track your progress on this Python Morsels topic trail, sign in or sign up.

0%
Python Morsels
Watch as video
03:33
This is a free preview of a premium screencast. You have 1 preview remaining.