tuples and The list is almost the same, The difference is that the elements of the tuple after the creation of tuples can not be modified, such as (add, expand, remove, and so on, but the elements of the elements of the tuple can be modified)
Tuple built-in functions
Function |
Function |
CMP (TUPLE1,TUPLE2) |
Comparison of two tuple elements |
Len (tuple) |
Calculating the number of tuple elements |
Max (tuple) |
Returns the maximum value of an element in a tuple |
MIN (tuple) |
Returns the element minimum value in a tuple |
Tuple (SEQ) |
Convert a list to a tuple |
Count (P_object) |
Count the number of occurrences of an element in a tuple |
Index (self) |
Gets the index position of the specified element in the tuple |
Basic Operation: Index slices Loops length include
#!/usr/bin/env python#-*-coding:utf-8-*-A= ("Zou","Lu","Chen") b=("Bob","John") C=CMP (A, B)Print(c)#returns 1 in a tuple with more than 1 elements in BPrint(A.count ("Zou"))#returns 1Print(A.index ("Zou"))#returns 0D=Len (a)Print(d)#returns 3 tuple a with 3 elementsE=Max (a)Print(e)#back to Zouf=min (a)Print(f)#back to Chena=["Zou","Lu","Chen"]h=tuple (a)Print(h)#return (' Zou ', ' Lu ', ' Chen ')
Creating tuples
#!/usr/bin/env python#-*-coding:utf-8-*-A= ("Zou","Lu","Chen")#orB=tuple (("Zou","Lu","Chen"))
Tuple conversion tuples
(Convert Narimoto groups, iterative data variables that need to be converted) Note: The ability to convert Narimoto groups must be iterative, that is, can be used for loop
string, dictionary, List > can all convert Narimoto Group, convert Narimoto group can be for loop, the for loop every time the data is one element of tuple
# !/usr/bin/env python # -*-coding:utf-8-*-a=" Young No regrets "b=tuple (a)print(b )
Index
format: Tuple variable plus How to [index subscript]
# !/usr/bin/env python # -*-coding:utf-8-*-a= ("Zou","lu"," Chen " print (a[1])# prints out LU prints elements labeled 1
Slice
format: Tuple variable plus [Start subscript: End subscript]
# !/usr/bin/env python # -*-coding:utf-8-*-a= ("Zou","lu"," Chen " )print(A[0:3])
#打印出lu prints elements with elements subscript 0 to 2
Len (P_object)
(Number of elements in the statistical tuple)
# !/usr/bin/env python # -*-coding:utf-8-*-a= ("Zou","lu"," Chen " print(Len (a))# prints out 3 prints the total number of elements in the tuple
While loop
# !/usr/bin/env python # -*-coding:utf-8-*-a= ("Zou","lu"," Chen " ) b=0 while b<Len (a): print(a[b]) b +=1# prints out Zou Lu Chen loops all elements in the tuple
For loop
# !/usr/bin/env python # -*-coding:utf-8-*-a= ("Zou","lu"," Chen " ) for in A: print(b)# Print out all the elements of the Zou Lu Chen loop out of the tuple sequentially
Count (self, value)
(Calculates the number of occurrences of an element in a tuple) the element to be calculated
# !/usr/bin/env python # -*-coding:utf-8-*-a= ("Zou","lu"," Chen " )print(A.count ("lu"))#
Index (self, value, Start=none, Stop=none)
(Gets the index position of the specified element in the tuple) the element to find, start position, end position
# !/usr/bin/env python # -*-coding:utf-8-*-a= ("Zou","lu"," Chen " )print (A.index ("lu"))# prints out 1
Element append for element in tuple
Tuple elements are not modifiable and append, that is, the tuple's children are not modifiable, the elements of the tuple element is the sun level can be modified
#!/usr/bin/env python#-*-coding:utf-8-*-A= ("Zou","Lu",["Chen","Job"]) b="John"a[2].insert (0,b)Print(a)#Print out (' Zou ', ' Lu ', [' John ', ' Chen ', ' Job ')
modifying tuples
element values in tuples are not allowed to be modified, but they can be combined in groups of tuples
The concatenation of tuples is equivalent to the aggregation of multiple sets
#!/usr/bin/env python#-*-coding:utf-8-*-A= ("Zou","Lu","Chen") b=("Bob","John") C=a+bPrint(c)#Print out (' Zou ', ' Lu ', ' Chen ', ' Bob ', ' John ')
Replication of tuples
#!/usr/bin/env python#-*-coding:utf-8-*-A= ("Zou","Lu","Chen") b=a*3Print(b)#Copy Tuple aC="Zou" inchb#checks if element "Zou" is in tuple B, returns true, otherwise falsePrint(c)
Functions of tuples
Conversion list
Index
Slice
For loop
Length
Reverse
Sort
Index location
Number of statistics elements
Tenth lesson of Python learning-basic data type (tuple)