Definition and Application of Python Tuple


1. Preface

In Python A tuple is a data type very similar to a list, except that elements in a list can be modified, while elements in a tuple cannot be modified.

2. Define and use tuples

2.1 Definition

The parenthesis () literal syntax is usually used to define elements, and the example code is as follows:

tuple1 = ("Hello", "1 Bowl week ", " How do you do ")

print(type(tuple1))  #  View tuple types   <class 'tuple'>
print(tuple1)  # ('Hello', '1 Bowl week ', ' How do you do ')

2.2 Use

The operators and usage methods and lists supported by tuples are like 1, and the sample code is as follows:

tuple1 = ("Hello", "1 Bowl week ", " Nuggets ", " Developer ", "strive")
tuple2 = (10, 20, 60, 34)

#  Splice
tuple3 = tuple1 + tuple2
print(tuple3)  # ('Hello', '1 Bowl week ', ' Nuggets ', ' Developer ', 'strive', 10, 20, 60, 34)
print(tuple2 * 2)  # (10, 20, 60, 34, 10, 20, 60, 34)

#  Indexing and slicing
print(tuple1[1], tuple1[-4])  # 1 Bowl week  1 Bowl week
print(tuple3[:2])  # ('Hello', '1 Bowl week ')
print(tuple3[::2])  # ('Hello', ' Nuggets ', 'strive', 20, 34)

#  Traversal operation
for ch in tuple1:
    print(ch)
'''
Hello
1 Bowl week
 Nuggets
 Developer
strive
'''

#  Member operation
print("1 Bowl week " in tuple1)  # True
print(" Nuggets " not in tuple1)  # False

1 empty () It means an empty tuple. There are two elements in one tuple, which is called a 2 tuple. If there are five elements, it is called a 5 tuple, but if it is ('hello') Then it is not a tuple, and this parenthesis becomes a parenthesis to change the priority; If you want to turn it into parentheses, you need to add a comma, otherwise it does not represent a 1 tuple.

The sample code is as follows:

a = ()
print(type(a))    # <class 'tuple'>
b = ('1 Bowl week ')
print(type(b))    # <class 'str'>
c = (100)
print(type(c))    # <class 'int'>
d = ('1 Bowl week ', )
print(type(d))    # <class 'tuple'>
e = (100, )
print(type(e))    # <class 'tuple'>

3. Application scenarios of tuples

3.1 Packaging and Unpacking

When multiple comma-separated values are assigned to a variable, the values are packaged into a tuple type; When one tuple is assigned to multiple variables, the tuple will be unpacked into multiple values and then assigned to the corresponding variables respectively.

The sample code is as follows:

t = "Hello", "1 Bowl week ", " Nuggets ", " Developer ", "strive"
print(type(t))  # <class 'tuple'>
print(t)  # ('Hello', '1 Bowl week ', ' Nuggets ', ' Developer ', 'strive')

x, y, z, i, j = t
print(x, y, z, i, j)  # Hello 1 Bowl week   Nuggets   Developer  strive

If the number of elements and the number of variables are not 1 when unpacking, an error will be caused, such as the following code

t= ("Hello", "1 Bowl week ", " Nuggets ", " Developer ", "strive")

# x, y, z = t # ValueError: too many values to unpack

x, y, z, i, j, k, l = t # ValueError: not enough values to unpack

ValueError: too many values to unpack Insufficient value for unpacking

ValueError: not enough values to unpack Too many unpacked values

The way to solve the problem is to use the * wildcard character. With this wildcard character, one variable can receive multiple values, which turns a variable into a list. This wildcard can only appear once.

The sample code is as follows:

t = ("Hello", "1 Bowl week ", " Nuggets ", " Developer ", "strive")

x, y, *z = t
print(x, y, z)  # Hello 1 Bowl week  [' Nuggets ', ' Developer ', 'strive']

x, *y, z = t
print(x, y, z)  # Hello ['1 Bowl week ', ' Nuggets ', ' Developer '] strive

*x, y, z = t
print(x, y, z)  # ['Hello', '1 Bowl week ', ' Nuggets ']  Developer  strive

3.2 Exchanging the Values of Two Variables

In Python To exchange the values of the two variables a and b, you only need to use the following code

a, b = b, a

Similarly, if you want to interchange the values of three variables a, b and c, that is, b is assigned to a, c is assigned to b, and a is assigned to c, you can do the same.

a, b, c = b, c, a

3.3 Have a function return multiple values

If return Statement, which is assembled into a 2 tuple and then returned. Therefore, calling the defined function will get this 2 tuple, or you can assign two values in the 2 tuple to two variables through unpacking syntax.