Python 中的元组解包是什么?

Python 中的元组解包是什么?

在定义元组解包之前,我们需要了解什么是元组。

元组 :在Python中,元组用于存储不可变对象。元组是不可变的Python对象序列。元组是序列,元组不能被改变,元组使用圆括号。它将(RHS)右手边的值分配到(LHS)左手边。用另一种方式称它为将元组值解包到变量中。在元组解包中,LHS上的变量数应等于给定元组中的值数。在打包中,我们将值放入新元组中,而在解包中,我们将这些值提取到一个变量中。

阅读更多:Python 教程

示例1

tuple = ("Tutorials Point", 132, "Employees") # 元组打包
(companyname , Employerscount ,Information) = tuple # 元组解包
print(companyname)
print(Employerscount)
print(Information)

输出

Tutorials Point
132
Employees

示例2

tuple = ("RRS College of Engg and Technology", 6000, "Engineering") # 元组打包
(college, student, graduates) = tuple # 元组解包
# 打印学院名称
print(college)
# 打印学生数量
print(student)
# 打印学院类型
print(graduates)

输出

RRS College of Engg and Technology
6000
Engineering

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程