Python Single Line Multiple Assignment

本文介绍了Python中一次性为多个变量赋值和同时给多个变量分配相同值的方法,包括使用逗号分隔、元组赋值、*运算符以及注意事项。讲解了如何处理不同类型的变量、避免对象共享导致的问题以及利用深浅拷贝技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Multiple assignment in Python: Assign multiple values or the same value to multiple variables | note.nkmk.meIn Python, use the = operator to assign values to variables.You can assign values to multiple variables on one line.This article describes the following two cases.Assign multiple values to multiple variables Assign the same value to multiple variables You can assign multiple values to multiple varia...icon-default.png?t=M1FBhttps://note.nkmk.me/en/python-multi-variables-values/In Python, use the = operator to assign values to variables.

a = 100
b = 200

print(a)
# 100

print(b)
# 200

source: multi_variables_values.py

You can assign values to multiple variables on one line.

This article describes the following two cases.

  • Assign multiple values to multiple variables
  • Assign the same value to multiple variables

Assign multiple values to multiple variables

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

a, b = 100, 200

print(a)
# 100

print(b)
# 200

source: multi_variables_values.py

You can assign to more than three variables. It is also possible to assign to different types.

a, b, c = 0.1, 100, 'string'

print(a)
# 0.1

print(b)
# 100

print(c)
# string

source: multi_variables_values.py

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'>

source: multi_variables_values.py

If the number of variables on the left and the number of values on the right do not match, a ValueError will occur, but you can assign the rest as a list by appending * to the variable name.

# 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'>

source: multi_variables_values.py

For more information on * and how to assign elements of a tuple and list to multiple variables, see the following article.

It is also possible to swap the values of multiple variables in the same way. See the article below.

Assign the same value to multiple variables

You can assign the same value to multiple variables by using = consecutively.

This is useful, for example, when initializing multiple variables to the same value.

a = b = 100

print(a)
# 100

print(b)
# 100

source: multi_variables_values.py

It is also possible to assign another value into one after assigning the same value. As described later, care must be taken when assigning mutable objects such as lists or dictionaries.

a = 200

print(a)
# 200

print(b)
# 100

source: multi_variables_values.py

Even three or more can be written in the same way.

a = b = c = 'string'

print(a)
# string

print(b)
# string

print(c)
# string

source: multi_variables_values.py

Be careful when assigning mutable objects such as list or dict instead of immutable objects such as intfloat, or str.

If you use = consecutively, the same object is assigned to all variables, so if you change the value of element or add a new element, the other will also change.

a = b = [0, 1, 2]

print(a is b)
# True

a[0] = 100
print(a)
# [100, 1, 2]

print(b)
# [100, 1, 2]

source: multi_variables_values.py

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]

source: multi_variables_values.py

If you want to handle them separately, you need to assign them 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.8.0 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]

source: multi_variables_values.py

You can also use copy() or deepcopy() of the copy module to make shallow and deep copies. See the following article.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值