This is the first day of my participation in the August Text Challenge.More challenges in August


Definition of a tuple

  • TupleTuples are similar to lists except that of tuplesElements cannot be modified
    • A tuple represents a sequence of elements
    • tuplesPythonIn development, there are specific application scenarios
  • Used to storeA bunch of information.dataUsed between.separated
  • A tuple with(a)define
  • Of a tupleThe index0start
    • An index is the number of positions of data in a tuple
info_tuple = ("hui".21.1.75)
Copy the code


Creating a tuple

info_tuple = ()    # empty tuple
Copy the code


When a tuple contains only one element, you need to add a comma after the element

info_tuple = (21, )
Copy the code

If you don’t add a comma, it’s not a tuple. Test this with IPython:

In [1]: info_tuple = (50)

In [2]: info_tuple
Out[2] :50

In [3] :type(info_tuple)
Out[3] :int

In [4]: info_tuple = (50, )

In [5] :type(info_tuple)
Out[5] :tuple

In [6]: info_tuple
Out[6] : (50,)

In [7] :Copy the code

When tested, int is not a tuple without a comma.

So when you create a tuple with only one element, you add a comma after the element

  • Info_tuple = (21,) √ The type is a tuple
  • Info_tuple = (21) The X type is not a tuple but an integer


Tuple elements cannot be modified


In [69]: info_tuple = ('hui'.21.1.75)

In [70]: info_tuple[0]
Out[70] :'hui'

In [71]: info_tuple[0] = 'wang'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-71-40015c5375d3> in <module>
----> 1 info_tuple[0] = 'wang'

TypeError: 'tuple' object does not support item assignment
Copy the code

Type error: Tuple object does not support element assignment


Common operations of tuples

There are rarely just two methods in tuples

  • Index () gets the index of the element’s first occurrence in a tuple
  • Count () Number of occurrences of a statistical element in a tuple


  • inipythonTo define atuples, such as:info_tuple = (50, )
  • The inputinfo_tuple.Press theTABThe key,ipythonYou will be promptedtuplesThe following functions can be used:


Tuple index() method test

The index() method gets the index of the first occurrence of an element in a tuple

In [8]: info_tuple = ('hui'.'zack'.'wang'.'hui')

In [9]: info_tuple.index('hui')
Out[9] :0

In [10]: info_tuple.index('zack')
Out[10] :1

In [11]: info_tuple.index('wang')
Out[11] :2
Copy the code


Tuple count() method test

The count() method counts the number of occurrences of elements in a tuple

In [12]: info_tuple = ('hui'.'zack'.'wang'.'hui')

In [13]: info_tuple.count('hui')
Out[13] :2

In [14]: info_tuple.count('zack')
Out[14] :1

In [15]: info_tuple.count('wang')
Out[15] :1
Copy the code


To iterate over

  • Retrieves data stored at a specified location from a tuple
  • Traversal is the process of retrieving data from a tuple from beginning to end
In [17]: info_tuple = ('hui'.21.1.75)

In [18] :for obj ininfo_tuple: ... :print(obj) ... : hui21
1.75

In [19] :Copy the code

In Python, you can use the for loop to iterate over all non-numeric variables: lists, tuples, dictionaries, and strings

In real development, there is not much need to loop over tuples unless the data types in tuples can be identified


Application scenarios

Swap the two variables

Usually we use a temporary variable to aid in the interchange and connect the values of the variables

In [23]: a = 10

In [24]: b = 20

In [25] :# case with temporary variables

In [26]: temp = a

In [27]: a = b

In [28]: b = temp

In [29]: a
Out[29] :20

In [30]: b
Out[30] :10

Copy the code


In Python, tuples are a very convenient way to interchange values.

In [31] :# Use tuples

In [32]: a = 10

In [33]: b = 20

In [34]: a, b = b, a

In [35]: a
Out[35] :20

In [36]: b
Out[36] :10
Copy the code


As arguments and return values to the function

A function can take any number of arguments, or return more than one data at a time

Function returns multiple data

In [45] :def get_info() :. : name ='hui'. : age =21. :returnname, age ... : In [46]: name, age = get_info()

In [47]: name
Out[47] :'hui'

In [48]: age
Out[48] :21

In [49] :type(get_info())
Out[49] :tuple
Copy the code


As you can see from the above code, the result type returned by the function is a tuple

Q: Why is the tuple type returned?

A: First, the Python interpreter packages the name and age variables into A whole, or tuple, but the variables returned must be separated by commas. Therefore, the type returned is a tuple, which enables the function to return multiple data.


Q: The return is a tuple type. How can you use multiple variables to return the result from the receiving function?

A: Return can pack multiple variables into tuples, so the interpreter can unpack tuples into multiple variables

This is done implicitly by the Python interpreter for packing and unpacking tuples.


The function takes any arguments

In [54] :def set_info(*args) :. :print(type(args)) ... :print(args) ... : In [55]: set_info('hui'.21)
<class 'tuple'>
('hui'.21)

In[56] :
Copy the code

Pack non-keyword arguments into tuples for the parameter form, which will be explained later in the function progression section.


Format string

The () following the formatted string is essentially a tuple

In [38]: name = 'hui'

In [39]: age = 21

In [40]: info = (name, age)

In [41] :type(info)
Out[41] :tuple

In [43] :print('%s's age is %d'% (name, age)) The age of hui is21

In [44] :print('%s's age is %d'% info) Hui for age21

In [45] :Copy the code


Conversion between tuples and lists

Make lists into tuples that cannot be modified to protect data security

Use the list function to convert a tuple to a list

In [63]: infos = ('hui'.21.1.75)

In [64] :type(infos)
Out[64] :tuple

In [65]: infos
Out[65] : ('hui'.21.1.75)

In [66]: infos = list(infos)

In [67] :type(infos)
Out[67] :list

In [68]: infos
Out[68] : ['hui'.21.1.75]
Copy the code


You can use the tuple function to convert lists into tuples

In [72]: infos = [1.2.3.4.5]

In [73] :type(infos)
Out[73] :list

In [74]: infos
Out[74] : [1.2.3.4.5]

In [75]: infos = tuple(infos)

In [76] :type(infos)
Out[76] :tuple

In [77]: infos
Out[77] : (1.2.3.4.5)
Copy the code


The tail language

✍ Code writes the world and makes life more interesting. ❤ ️

✍ thousands of rivers and mountains always love, ✍ go again. ❤ ️

✍ code word is not easy, but also hope you heroes support. ❤ ️