什么是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
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短