In Python, the = operator is used to assign values to variables.
a = 100
b = 200
print(a)
# 100
print(b)
# 200
As in the example above, you can assign values to multiple variables at once instead of one at a time, which is convenient because it requires only one simple line of code to write.
The following two cases are described.
- Assign multiple values to multiple variables
- Assign the same value to multiple variables
Assign multiple values to multiple variables
Multiple values can be assigned to multiple variables simultaneously by separating variables and values with commas.
a, b = 100, 200
print(a)
# 100
print(b)
# 200
Three or more variables, each of a different type, are acceptable.
a, b, c = 0.1, 100, 'string'
print(a)
# 0.1
print(b)
# 100
print(c)
# string
If there is one variable on the left side, it is assigned as a tuple.
a = 100, 200
print(a)
print(type(a))
# (100, 200)
# <class 'tuple'>
If the number of variables on the left-hand side does not match the number of values on the right-hand side, a ValueError error will result, but the rest can be assigned as a list by adding an asterisk to the variable.
# a, b = 100, 200, 300
# ValueError: too many values to unpack (expected 2)
# a, b, c = 100, 200
# ValueError: not enough values to unpack (expected 3, got 2)
a, *b = 100, 200, 300
print(a)
print(type(a))
# 100
# <class 'int'>
print(b)
print(type(b))
# [200, 300]
# <class 'list'>
*a, b = 100, 200, 300
print(a)
print(type(a))
# [100, 200]
# <class 'list'>
print(b)
print(type(b))
# 300
# <class 'int'>
For more information about asterisks and how to assign elements of a tuple or list to multiple variables, see the following article.
Assign the same value to multiple variables
The same value can be assigned to multiple variables by using = consecutively. This is useful for initializing multiple variables to the same value.
a = b = 100
print(a)
# 100
print(b)
# 100
More than 3 pieces are acceptable.
a = b = c = 'string'
print(a)
# string
print(b)
# string
print(c)
# string
After assigning the same value, another value can be assigned to one of them.
a = 200
print(a)
# 200
print(b)
# 100
Be careful when assigning mutable objects such as lists and dictionary types, rather than immutable (unchangeable) objects such as integers, floating point numbers, and strings.
Using = consecutively means that all variables point to the same object, so if you change the value of one element or add a new element, the other will change as well.
a = b = [0, 1, 2]
print(a is b)
# True
a[0] = 100
print(a)
# [100, 1, 2]
print(b)
# [100, 1, 2]
Same as below.
b = [0, 1, 2]
a = b
print(a is b)
# True
a[0] = 100
print(a)
# [100, 1, 2]
print(b)
# [100, 1, 2]
If you wish to process them separately, simply assign to each.
after c = []; d = [], c and d are guaranteed to refer to two different, unique, newly created empty lists. (Note that c = d = [] assigns the same object to both c and d.)
3. Data model — Python 3.10.4 Documentation
a = [0, 1, 2]
b = [0, 1, 2]
print(a is b)
# False
a[0] = 100
print(a)
# [100, 1, 2]
print(b)
# [0, 1, 2]
There are also methods to generate shallow and deep copies with copy() and deepcopy() in the copy module.