Skip to content
Justice Tech School

Assigning Many Values

You can assign several variables at once, give them all the same value, or unpack a list or tuple into variables. The star (*) collects the leftovers.

Swapping two variables is just a, b = b, a.

Example
x, y, z = "red", "green", "blue"
print(x, y, z)
a = b = c = 0
print(a, b, c)
first, *rest = [1, 2, 3, 4]
print(first, rest)
x, y = y, x
print(x, y)

Result

red green blue
0 0 0
1 [2, 3, 4]
green red

Exercise

Give three variables the same value.

a = b = c = 

Quiz Test

What does a, b = b, a do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.