Python Variables - Assign Multiple Values
Last Update : 21 Aug, 2022In this tutorial, You will learn about assigning multiple values of Python variables.
Python uses the = sign to assign values to variables.
For example -:
x = 200
y = "Hello World"
print(x)
# 200
print(y)
# Hello World
Python allows you to assign multiple values to multiple variables at the same time using commas. And also, Python allows assigning the same value to multiple variables at the same time. This will save a lot of time for us when assigning a large number of variables.
Assign Multiple Variables with Multiple Values
Python allows you to assign multiple values to multiple variables in one line by separating variables and values with commas.
For example -:
x, y = 500, 1000
print(x)
# 500
print(y)
# 1000
It is also possible to assign different data types.
For example -:
x, y, z = 100, "Hello", 0.5
print(x)
# 100
print(y)
# hello
print(z)
# 0.5
When you use one variable on the left side with multiple values, these values are assigned as a tuple.
For example -:
x = 500, 0.34, "Hello", True
print(x)
print(type(x))
This program produces the following result -:
(500, 0.34, 'Hello', True)
<class 'tuple'>
When the number of variables on the left does not match with the number of values on the right, the ValueError will occur.
For example -:
x, y = 500, 100, 1500
# ValueError: too many values to unpack (expected 2)
x, y, x = 500, 100
# ValueError: not enough values to unpack (expected 3, got 2)
However, you can assign the rest values as a list by appending * before to the variable name.
For example -:
x, *y = 500, 1000, 1500
print(x)
print(type(x))
print(y)
print(type(y))
*x, y = 500, 1000, 1500
print(x)
print(type(x))
print(y)
print(type(y))
This program produces the following result -:
500
<class 'int'>
[1000, 1500]
<class 'list'>
[500, 1000]
<class 'list'>
1500
<class 'int'>
Assign Multiple Variables with Single Value
Python allows you to assign the same values to multiple variables in one line by using the = operator consecutively.
For example -:
x = y = z = 124
print(x)
# 124
print(y)
# 124
print(z)
# 124
You should be careful, when assigning mutable objects like dict or list instead of immutable objects like str, int, or float. Because, When you add a new value or change the existing value of one variable, other variable values will also change.
For example -:
x = y = [1, 2, 3]
print(x is y)
# True
x[0] = 120
print(x)
# [120, 2, 3]
print(y)
# [120, 2, 3]
y = [1, 2, 3]
x = y
print(x is y)
# True
x[0] = 120
print(x)
# [120, 2, 3]
print(y)
# [120, 2, 3]
Unpack a Collection
If your program has a values collection such as list, tuple, etc. You can extract values into variables using Python.
For example -:
months = ["January", "February", "March"]
x, y, z = months
print(x)
print(y)
print(z)
This program produces the following result -:
January
February
March
Summary
- To assign values to variables, The = sign is used in Python programming.
- Python allows assigning multiple values to multiple variables.
- Python allows assigning the same value to multiple variables.
- Python allows extracting values collection (list, tuple, etc.) values into variables.