在定义元组拆包之前,我们需要了解什么是元组。
元组:在Python中,元组用于存储不可变的对象。元组是不可变的Python对象的序列。元组是序列,元组不能更改,并且元组使用括号。它将值的(RHS)右侧分配给(LHS)左侧。换句话说,这称为将值的元组拆包为变量。在对元组进行解包时,LHS上的变量数应等于给定元组中的值数。在打包时,我们将值放入新的元组中,而在打包时,我们将这些值提取到单个变量中。
tuple = ("(niaoge.com)", 132, "Employees") # tuple packing (companyname , Employerscount ,Information) = tuple # tuple unpacking print(companyname) print(Employerscount) print(Information)
(niaoge.com) 132 Employees
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