This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 2: Shortcut assignment statements

Simultaneous variable assignments in Python

face Josiah Wang

You have seen how you can swap variables when you have only the most basic programming features.

There is actually a different, recommended way to swap variables in Python.

In Python, you can actually assign values to multiple variable simultaneously. Of course, whether this is easy to read is a different question!

x, y = 15, 24
print(x)
print(y)

You can, however, take advantage of this feature, and swap variables the Pythonic way!

This is actually the recommended way to swap variables in Python.

x = 15
y = 24
x, y = y, x
print(x)
print(y)