Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky

Writing in the front

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

Define and use tuples

define

Defining elements usually uses the bracketed () literal syntax, as shown in the following example:

<class 'tuple'> print(tuple1) # ('Hello', 'tuple1 ', 'Hello')Copy the code

use

Tuples support the same operators and use them as lists. Example code is as follows:

Tuple1 = (" Hello ", "a bowl of week", "nuggets", "developer", "strive") tuple2 = (10, 20 and 60, Print (tuple3) # ('Hello', 'week ',' developer ', 'strive', 10, 20, 60, Print (tuple1[1]) print(tuple2 * 2) # Tuple1 [4]) # a bowl of a bowl of weeks print (tuple3 [:] 2) # (' Hello ', 'a bowl of weeks) print (tuple3 [2] : :) # (' Hello,' the nuggets', 'strive, 20. For ch in tuple1: Print (" strain "in tuple1) # True print(" strain" not in tuple1) # FalseCopy the code

An empty () is an empty tuple. A tuple with two elements is called a tuple. A tuple with five elements is called a quintuple. If you want it to be parentheses, add a comma, otherwise it’s not a tuple. Example code is as follows:

A = () print(type(a)) # <class 'tuple'> b = (' a ') print(type(b)) # <class 'STR '> c = (100) print(type(c)) # <class 'int' > d = (' a bowl of weeks') print (type (d)) # < class 'tuple > e = (100), print (type (e)) # < class >' tuple 'Copy the code

Application scenarios of tuples

Pack and unpack

When multiple comma-separated values are assigned to a variable, the values are packaged into a tuple type; When assigning a tuple to multiple variables, the tuple is unpacked into multiple values and assigned to the corresponding variables, as shown in the following code:

T = "Hello", "a bowl of week", "nuggets", "developer", "strive" print (type (t)) # < class 'tuple > print # (t) (' Hello', 'a bowl of weeks',' the nuggets', 'developers', 'strive') x, y, z, I, j = t print(x, y, z, I, j) # Hello a bowl week nuggets developerCopy the code

If the number of elements and variables does not match when unpacking, an error occurs, as in this code

T = (" Hello ", "a bowl of 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 unpackCopy the code

ValueError: Too many values to unpack Insufficient values

ValueError: Not enough values to unpack Too many values to unpack

The * wildcard allows a variable to receive multiple values, turning a variable into a list. This wildcard can only occur once, as shown in the following code:

T = (" Hello ", "a bowl of week", "nuggets", "developer", "strive") the x, y, t print * z = (x, y, z) # Hello a bowl weeks [' the nuggets', 'developers',' strive] x * y, Z = t print(x, y, z) # Hello [' struggler ', 'struggler ',' struggler ', 'struggler'Copy the code

Swap the values of two variables

In Python, swapping the values of two variables A and b is all you need to do is use the code shown below

a, b = b, a
Copy the code

The same can be done if the values of variables A, B, and C are interchanged, so that B is assigned to A, C to B, and A to C.

a, b, c = b, c, a
Copy the code

Let the function return multiple values

If a return statement has two values, the two values are assembled into a binary and returned. So calling the defined function yields this tuple, or you can assign two values from the tuple to two variables by unpacking the syntax.