Python3 tuple
Python is similar to a list of tuples, except that the elements of the tuple can not be modified.
Tuples use parentheses, square brackets list.
Tuple create very simple, only you need to add elements in parentheses and separated by commas to
TUP1 = >>> ( 'the Google', 'Runoob', 1997, 2000); >>> tup2 = (. 1, 2,. 3,. 4,. 5); >>> tup3 = "A", "B", " c "," d "; # brackets need not be >>> type (tup3) <class 'tuple'>
Empty tuples
tup1 = ();
When tuple contains only one element, the element needs to be added after the comma, or brackets will be used as the operator:
TUP1 = >>> (50) >>> type (TUP1) # without comma, type Integer <class 'int'> >>> = TUP1 (50,) >>> type (TUP1) # comma type tuple <class 'tuple'>
Access tuple
Tuple subscripts may be used to access the index value tuples, the following examples:
#!/usr/bin/python3 tup1 = ('Google', 'Runoob', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1[0]: ", tup1[0]) print ("tup2[1:5]: ", tup2[1:5])
Examples of the above output: TUP1 [0]: the Google tup2 [. 1:. 5]: (2,. 3,. 4,. 5)
Delete tuple
Element value tuples can not be deleted, but we can use the del statement to delete an entire tuple, the following examples:
tup = ( 'Google', ' Runoob', 1997, 2000) print (tup) del tup;
Operators tuple
Like the string, a tuple can be used between + and calculates asterisk. This means that they can be combined and after replication operation will generate a new tuple.
len ((1, 2, 3)) -> # 3 calculates the number of elements (1, 2, 3) + (4, 5, 6) -> (1, 2, 3, 4, 5, 6) # connection ( 'Hi!',) * 4 -> ( 'Hi!', 'Hi!', 'Hi!', 'Hi!') # copy 3 in (1, 2, 3 ) -> True # element exists for x in (1, 2, 3): print (x,) -> 1 2 3 # iterations
Tuple index, taken
Because a sequence of tuples is, we can access the element at the specified tuple, some elements may be taken in the index, a character string, a list of similar
Tuple built-in functions
Python tuple contains the following built-in functions
Count the number of tuples of elements. len (tuple) >>> tuple1 = ( 'the Google', 'Runoob', 'Taobao') >>> len (tuple1) . 3
Tuple element returns the maximum value max (tuple) >>> Tuple2 = ( '. 5', '. 4', '. 8') >>> max (Tuple2) '. 8'
Returns the minimum element tuple. min (tuple) >>> Tuple2 = ( '. 5', '. 4', '. 8') >>> min (Tuple2) '. 4'
Convert list is a tuple. tuple (SEQ) >>> List1 = [ 'the Google', 'Taobao', 'Runoob', 'Baidu'] >>> tuple1 = tuple (List1) >>> tuple1 ( 'the Google', 'Taobao', 'Runoob ',' Baidu ')