什么是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 表达家人的祝福语简短
11 儿子31岁祝福语简短
12 新年祝同学祝福语简短
13 朋友明天手术祝福语简短
14 给儿子生日祝福语 简短
15 初八的祝福语简短
16 父亲简短结婚祝福语大全
17 生日祝福语男孩 简短独特
18 同事调动的祝福语简短