Basic operations for tuples:
1. Create a tuple:
tuple= (1,26); Tuple1= ("" ","sy");
Create an empty tuple:
Tuple= ();
When you include only one element in a tuple, you need to add a comma after the element to eliminate ambiguity;
Tuple= (50,)
2. Access to tuples:
Tup1 = ('Physics','Chemistry', 1997, 2000); Tup2= (1, 2, 3, 4, 5, 6, 7 );Print "Tup1[0]:", Tup1[0]Print "Tup2[1:5]:", Tup2[1:5]#The result of the above example output:#Tup1[0]: Physics#Tup2[1:5]: [2, 3, 4, 5]
3. Modify Tuples:
Tup1 = (34.56 = ('abc''xyz'); # The following modifications to tuple element operations are illegal. # tup1[0] = +; # Create a new tuple Tup3 = tup1 + tup2; Print Tup3; # The result of the above example output: # (34.56, ' abc ', ' XYZ ')
4. Delete the tuple:
Tup = ('physics'chemistry', 1997,); Print Tup; del Tup; Print " "print tup;
5. As with strings, you can use the + sign and the * number to operate between tuples. This means that they can be combined and copied, and a new tuple is generated after the operation.
6.
The Python tuple contains the following built-in functions
1. CMP (Tuple1, Tuple2): Compares two tuple elements.
2. Len (tuple): Calculates the number of tuple elements.
3, Max (tuple): Returns the maximum value of an element in a tuple.
4, min (tuple): Returns the element minimum value in the tuple.
5, tuple (seq): Converts a list to a tuple.
Operations on tuples in Python