Python allows you to assign values to multiple variables in one line:
Note: Make sure the number of variables matches the number of values, or else you will get an error.
And you can assign the same value to multiple variables in one line:
If you have a collection of values in a list, tuple etc. Python allows you to extract the values into variables. This is called unpacking.
Unpack a list:
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Try it Yourself »
Learn more about unpacking in our Unpack Tuples Chapter.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!