Overview
Tuples, identified with parentheses (), characterized by: elements in tuples are not modifiable
Let's look at an example of how to create a tuple:
Tuple1 = (U'deeptest', u'Open Source Optimization test', u'1') Tuple2= (1, 2, 3, 4, 5) Tuple3= ("a","b","C","D","e")
Built-in functions
The built-in functions commonly used in Python are:
Len
Number of elements used to calculate tuples
Max
Returns the maximum value of an element in a tuple
Min
Returns the element minimum value in a tuple
Tuple
Convert list to Narimoto Group
Let's take a look at the examples of these four functions:
#-*-coding:utf-8-*-__author__= u'Gubei'if __name__=="__main__": Tuple_demo= (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)#calculating the number of elements in a Tuple_demo Print(Len (tuple_demo))#returns the element with the largest value in Tuple_demo Print(Max (Tuple_demo))#returns the element with the smallest value in the Tuple_demo Print(min (tuple_demo))#Convert list to Narimoto groupList_demo = [1, 2, 3, 4, 5, 6] Tuple1=tuple (List_demo)#print a converted tuple Print(Tuple1)
The results of the operation are as follows:
1090 (1, 2, 3, 4, 5, 6)
Slice
Because tuples are also a sequence, we can use the tiling mechanism of Python to access the elements at a specified location in a tuple, or to intercept one of these elements.
Let's look at an example of a slice operation:
#-*-coding:utf-8-*-__author__= u'Gubei'if __name__=="__main__": Print(U"example of a tuple slice operation!") Data_demo= (U"deeptest", u"Appium", u"testingunion.com", u"Hello", u"Python3") #Read the second element Appium, note index subscript starting from 0E = data_demo[1] Print(e)#read the countdown to the second HelloE = data_demo[-2] Print(e)#slices, intercepting all elements from the beginning of the 2nd elementE = data_demo[1:] Print(e)#slicing, intercepting 第2-4个 elementsE = Data_demo[1:4] Print(e)
Execution results are as follows
元组切片操作示例
appiumhello ( " appium , ' testingunion.com , " hello , " " ) ( ' hello ' )
A summary of the above code:
Tuples can read elements by subscript index
Tuples can reverse-read elements by negative subscript index
Tuples can intercept an element by starting: terminating
Merge
Attributes of elements of a tuple: not modifiable. But we can either merge the tuples or connect to generate a new tuple, the code example is as follows:
#-*-coding:utf-8-*-__author__= u'Gubei'if __name__=="__main__": Print(U"example of a tuple merge or join operation!") Tup1= (U"deeptest", u"Appium") tup2= (U"testingunion.com", u"Hello"+ R"Python3") #merging into a new tupleTUP3 = Tup1 +tup2#Print to see the effect Print(tup1)Print(tup2)Print(TUP3)
Delete
Attributes of elements of a tuple: not modifiable. This means that we cannot delete a single element, but we can delete the tuple, for example:
#-*-coding:utf-8-*-__author__= u'Gubei'if __name__=="__main__": Print(U"example of a tuple merge or join operation!") Tup1= (U"deeptest", u"Appium") Print(tup1)#Delete a tuple delTup1Print(TUP1)
What happens when you execute the above code?
will prompt for output exception information, when:
Nameerror:name ' tup1 ' is not defined
Operation
Tuples and strings can be operated with + or *, and a new tuple can be generated after the operation.
#-*-coding:utf-8-*-__author__= u'Gubei'if __name__=="__main__": Print(U"Tuple Operations Example") Tup1= (U"deeptest", u"Open Source Optimization test") tup2= (1, 2, 3, 4) #calculate the element leader degree Print(Len (tup1))#Tuple ConnectionsTUP3 = Tup1 +tup2Print(TUP3)#tuple ReplicationTUP4 = Tup1 * 2Print(TUP4)#determines whether the element is in a tuple, or returns TRUE, otherwise returnsresult = 5inchtup2Print(Result)#traversing tuples forTinchtup2:Print(t)
List of tuples
Use the built-in function tuple to convert the list to the Narimoto group with the following code example:
# -*-coding:utf-8-*- __author__ = u' Gubei 'if__name__'__main__ " : # Convert list to Narimoto group List_demo = [1, 2, 3, 4, 5, 6] = tuple (list_demo) # print converted tuples Print(tuple1)
[Python3] Tuple (tuple)