什么是Python中的元组解包?
在定义元组拆包之前,我们需要了解什么是元组。
元组:在Python中,元组用于存储不可变的对象。元组是不可变的Python对象的序列。元组是序列,元组不能更改,并且元组使用括号。它将值的(RHS)右侧分配给(LHS)左侧。换句话说,这称为将值的元组拆包为变量。在对元组进行解包时,LHS上的变量数应等于给定元组中的值数。在打包时,我们将值放入新的元组中,而在打包时,我们将这些值提取到单个变量中。
例子1
tuple = ("nhooo.com", 132, "Employees") # tuple packing
(companyname , Employerscount ,Information) = tuple # tuple unpacking
print(companyname)
print(Employerscount)
print(Information)输出结果
nhooo.com 132 Employees
例子2
tuple = ("RRS College of Engg and Technology", 6000, "Engineering") # tuple packing
(college, student, graduates) = tuple # tuple unpacking
# print college name
print(college)
# print no of student
print(student)
# print type of college
print(graduates)输出结果
RRS College of Engg and Technology 6000 Engineering
热门推荐
10 祝福语简短唯美10字
11 家人毕业祝福语简短励志
12 红包祝福语简短网名
13 毕业祝福语简短给同学
14 恋爱祝福语大全简短搞笑
15 简短正能量生日祝福语
16 14年祝福语简短情话
17 同学道别祝福语简短
18 老师高考后祝福语 简短