Assigning Multiple Values in One Line in Python

In Python, you can assign values to more than one variable at a time. You can also assign the same value to multiple variables. This can be helpful when you want to initialize multiple variables with the same value or when you want to update multiple variables at the same time. Let’s take a look at how to do both of these things.

You can assign values to multiple variables by separating the variables and values with commas.

x, y, z = 14, [1, 2, 3], 'New data'
print(x) #Output: 14
print(y) #Output: [1, 2, 3]
print(x) #Output: 'New data'

x, y = 100, 50
add, multi = (x+y), (x*y)
print(add) #Output: 150
print(multi) #Output: 50000

If you want to assign the same value to multiple variables, you can do so by using the assignment operator (=).

first_name = last_name = middle_name = 'Tom'
print(first_name) #Output: 'Tom'
print(last_name) #Output: 'Tom'
print(middle_name) #Output: 'Tom'

last_name = 'Mary'
print(first_name) #Output: 'Tom'
print(last_name) #Output: 'Mary'
print(middle_name) #Output: 'Tom'

Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close