tuple作为不可变对象,每个tuple对象在第一次创建后,持有的元素不能改变,这是元组的基本概念,而元组解包功能是其中一个很常见的技术点,今天一起学习元组解包常用的3种情况
temp = ("hi","yuan","wai")
first,second,third = temp
等同于
temp = ("hi","yuan","wai")
first = temp[0]
second = temp[1]
third = temp[2]
说明:元组会自动解包,每个元素赋值给多个变量
注意:元素数量需要与变量数量相同,否则报错
temp = (1,2,3)
def hello(first,second,third):
print(first)
print(second)
print(third)
hello(*temp)
等同于
hello(1,2,3)