[Python] tuple tuple ① ( tuple tuple introduction | tuple cannot be modified | tuple definition | define a tuple with only one element | tuple nesting )





1. Introduction to tuple




1. Tuple introduction


The data in the list List is modifiable. If there is such a scenario, after the data is generated, it cannot be modified, and the list is not applicable;

A new data container "tuple" is introduced here;

  • Data encapsulation: Multiple elements of different types can be encapsulated in a tuple, and this function is the same as that of a list;
  • Unmodifiable: Once the tuple is defined and initialized, the data in it cannot be changed;

Tuples can be understood as read-only List lists;


2. Tuple definition


Tuples are defined using parentheses (), and multiple data elements are separated by commas;

The data elements in the tuple can be of the same data type or of different data types;


Tuple literal definition syntax:

(元素1, 元素2, 元素3)

Tuple variable definition syntax:

变量名称 = (元素1, 元素2, 元素3)

Empty tuple variable definition syntax:

变量名称 = ()
变量名称 = tuple()

Code example:

"""
元组 tuple 代码示例
"""

# 定义元组字面量
("Tom", "Jerry", 18, False, 3.1415926)

# 定义元组变量
t1 = ("Tom", "Jerry", 18, False, 3.1415926)
# 打印 元组变量 信息
print(f"t1 类型 : {
      
      type(t1)}, 内容 : {
      
      t1}")

# 定义空元组变量
t2 = ()
t3 = tuple()
# 打印 空元组变量
print(f"t2 类型 : {
      
      type(t2)}, 内容 : {
      
      t2}")
print(f"t3 类型 : {
      
      type(t3)}, 内容 : {
      
      t3}")

Results of the :

t1 类型 : <class 'tuple'>, 内容 : ('Tom', 'Jerry', 18, False, 3.1415926)
t2 类型 : <class 'tuple'>, 内容 : ()
t3 类型 : <class 'tuple'>, 内容 : ()

3. Define a tuple with only one element


To define a tuple with only one element, you need to add a comma after the only element, and this comma must exist;

The syntax for defining a tuple with only one element:

元组变量 = (元素,)

If there is no comma after the only element, the definition is not a tuple;


In the code below, if the only element is not followed by a comma, the ("Tom") variable is treated as a string type;

Code example:

# 定义单个元素元组变量
t4 = ("Tom",)
# 打印 元组变量 信息
print(f"t4 类型 : {
      
      type(t4)}, 内容 : {
      
      t4}")

# 定义单个元素元组变量
t5 = ("Tom")
# 打印 元组变量 信息
print(f"t5 类型 : {
      
      type(t5)}, 内容 : {
      
      t5}")

Results of the :

t4 类型 : <class 'tuple'>, 内容 : ('Tom',)
t5 类型 : <class 'str'>, 内容 : Tom

4. Tuple nesting


The element data type in the tuple is not limited, so element type data can also be stored in the tuple, that is, tuple nesting;

Code example:

# 元组嵌套
t6 = (("Tom", 16), ("Jerry", 18))
# 打印 元组变量 信息
print(f"t6 类型 : {
      
      type(t6)}, 内容 : {
      
      t6}")

Results of the :

t6 类型 : <class 'tuple'>, 内容 : (('Tom', 16), ('Jerry', 18))




2. Complete code example



Code example:

"""
元组 tuple 代码示例
"""

# 定义元组字面量
("Tom", "Jerry", 18, False, 3.1415926)

# 定义元组变量
t1 = ("Tom", "Jerry", 18, False, 3.1415926)
# 打印 元组变量 信息
print(f"t1 类型 : {
      
      type(t1)}, 内容 : {
      
      t1}")

# 定义空元组变量
t2 = ()
t3 = tuple()
# 打印 空元组变量
print(f"t2 类型 : {
      
      type(t2)}, 内容 : {
      
      t2}")
print(f"t3 类型 : {
      
      type(t3)}, 内容 : {
      
      t3}")

# 定义单个元素元组变量
t4 = ("Tom",)
# 打印 元组变量 信息
print(f"t4 类型 : {
      
      type(t4)}, 内容 : {
      
      t4}")

# 定义单个元素元组变量, 不写逗号
t5 = ("Tom")
# 打印 元组变量 信息
print(f"t5 类型 : {
      
      type(t5)}, 内容 : {
      
      t5}")

# 元组嵌套
t6 = (("Tom", 16), ("Jerry", 18))
# 打印 元组变量 信息
print(f"t6 类型 : {
      
      type(t6)}, 内容 : {
      
      t6}")

Results of the :

t1 类型 : <class 'tuple'>, 内容 : ('Tom', 'Jerry', 18, False, 3.1415926)
t2 类型 : <class 'tuple'>, 内容 : ()
t3 类型 : <class 'tuple'>, 内容 : ()
t4 类型 : <class 'tuple'>, 内容 : ('Tom',)
t5 类型 : <class 'str'>, 内容 : Tom
t6 类型 : <class 'tuple'>, 内容 : (('Tom', 16), ('Jerry', 18))

Guess you like

Origin blog.csdn.net/han1202012/article/details/131065821