Basic operations for tuples:
- Gets the number of elements: Len ()
- Connect two tuples: Tuple 1 + tuple 2
- Copy multiple tuples: tuples * Number of copies
- Get a single element: tuple [index position]
- Get multiple elements: tuples [start position: End Position: number of intervals]
- Get the largest element of a tuple: Use the max (Iterable,key) function, the parameter iterable as an iterative object, such as a list, a tuple, and a dictionary or collection that we will learn later, which is an iterative object; The parameter key is a function, which defaults to null Note that the element must be of the same type, or an exception will be thrown.
- Get tuple minimum element: use min (iterable,key) function, parameter with Max function.
- Determines whether a tuple contains a specified element: an element in a tuple
- Find the location of the specified element in the tuple: using the index (object,start,stop) function, the argument object is the objects, because the elements of the tuple can be one of all Python objects; The argument start is the starting position of the query, and the parameter stop is the query terminating position. Returns the position of the element in the tuple's element when it is queried for the same element as the parameter, otherwise throws an exception.
- Gets the number of occurrences of an element in a tuple: Use the Count (object) function, which is the object of the parameter.
Tup1 = (1,2,3,4,5,6,7,8,9)#Creating tuplesTup2 = ('a','b','C') Tup3= ('You're','I'm','You're','his','I'm','I'm')Print(Len (tup1))#gets the number of elements in the tuplePrint(TUP1+TUP2)#connecting multiple tuples to a new tuplePrint(tup2*3)#repeating a tuple element as a new tuplePrint(Tup1[0])#gets the element at the specified position of the tuple, starting at 0 from left to right index positionPrint(Tup1[-1])#gets the element at the specified position of the tuple, starting from right to left by-1Print(Tup1[2:8:2])#gets the elements of the specified fragment in the tuple, and can set the interval to getPrint(Max (TUP1))#gets the largest element in a tuple when all elements in the tuple are of the same typePrint(min (tup1))#gets the smallest element in a tuple when all elements in the tuple are of the same typePrint('a' inchTUP1)#determines whether an element is contained in a tuple, or false if it contains a return value of truePrint(Tup1.index (3))#queries whether a tuple contains an element, or throws an exception if it contains a return value of index positionPrint(Tup3.count ('I'm'))#gets the number of occurrences of an element in a tuple>>>9(1, 2, 3, 4, 5, 6, 7, 8, 9,'a','b','C')('a','b','C','a','b','C','a','b','C')19(3, 5, 7)91False23
Python data type-tuples