Basic Python syntax (1) introduction to Python
Python basic grammar (2) Zen of Python
Python basic syntax (3) use of miniconda
Python basic syntax (4) install Python language based on CentOS7 source code
Python basic syntax (5) numbers of basic types
Python basic syntax (6) string of basic types
Python basic syntax (7) list of basic types
Python basic syntax (8) tuples of basic types
Definition and characteristics of 1 tuple
- (1) The definition of tuples is enclosed in parentheses (), and can also be directly assigned to multiple values. If a value is followed by a comma
>>> a=(1,2,3,4) >>> type(a) <class 'tuple'> >>> a=1,2,3 >>> a (1, 2, 3) >>> type(a) <class 'tuple'> >>> a=1, >>> a (1,) >>> type(a) <class 'tuple'>
- (2) Tuples are immutable and cannot be modified
>>> a=(1,2,3,4,5,6) >>> a[1]=100 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
Common operations of 2 tuples
- (1) Elements in tuples can be oriented by position subscripts. The subscript of the first element is 0, and the subscript cannot exceed the length range of the element
>>> a=(1,2,3,4,5,6) >>> a[0] 1 >>> a[5] 6 >>> a[7] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: tuple index out of range
- (2) The subscript of an element in a tuple can be a negative number. A negative number indicates the mark from the right, - 1 indicates the last element, and - 2 indicates the penultimate element
>>> a=(1,2,3,4,5,6) >>> a[-1] 6 >>> a[-2] 5 >>> a[-6] 1 >>> a[-7] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: tuple index out of range
- (3) Elements in tuples can obtain part of tuples through slicing operation, which adheres to the principle of left closing and right opening
>>> a=(1,2,3,4,5,6) >>> a[0:4] (1, 2, 3, 4) >>> a[4:5] (5,)
- (4) The second parameter of tuple slicing operation can exceed the length of tuple. When it exceeds the length of tuple, the last element is taken by default
>>> a=(1,2,3,4,5,6) >>> a[2:6] (3, 4, 5, 6) >>> a[2:10] (3, 4, 5, 6)
- (5) The first and second parameters of tuple slicing operation can be omitted. When the first parameter is omitted, it means that the left side starts from the first element and the second parameter
When omitted, it means that the last element is taken from the right
>>> a=(1,2,3,4,5,6) >>> a[:4] (1, 2, 3, 4) >>> a[3:] (4, 5, 6) >>> a[:] (1, 2, 3, 4, 5, 6)
- (6) The position represented by the first parameter can be on the right side of the position represented by the second parameter. At this time, it is returned as an empty element
>>> a=(1,2,3,4,5,6) >>> a[5:1] () >>> a[-1:-3] ()
- (7) The slicing operation of the element can also have a third parameter, representing the step size
>>> a=(1,2,3,4,5,6,7,8,9,0) >>> a[1:9:3] (2, 5, 8) >>> a[::-1] (0, 9, 8, 7, 6, 5, 4, 3, 2, 1)
- (8) The len() function returns the tuple length, that is, the number of elements
>>> a=(1,2,3,4,5,6) >>> len(a) 6
- (9) The max() function returns the maximum value of the element in the tuple
>>> a=(1,2,3,4,5,6) >>> max(a) 6
- (10) The min() function returns the minimum value of the element in the tuple
>>> a=(1,2,3,4,5,6) >>> min(a) 1
- (11) The sum() function returns the sum of all elements in the ancestor
>>> a=(1,2,3,4,5) >>> sum(a) 15
- (12) in, not in determines whether a tuple contains an element
>>> a=(1,2,3,4,5,6) >>> 0 in a False >>> 0 not in a True >>> 4 in a True
- (13) Two tuples can be added with a plus sign to form a new tuple
>>> (1,2,3)+(1,2,3) (1, 2, 3, 1, 2, 3)
- (14) Tuples can also be multiplied by a number to copy the elements of an existing tuple multiple times to construct a new tuple
>>> (1,2,3)*3 (1, 2, 3, 1, 2, 3, 1, 2, 3)
Functions commonly used in 3 tuples
Because tuples are immutable and the elements in tuples cannot be modified, there are few available functions for tuples, only count and index
- (1) count(value) returns the number of elements in the tuple
>>> a=(1,2,3,2,1,2,3,2,1) >>> a.count(1) 3 >>> a.count(4) 0
- (2) index(value, start=0, stop=9223372036854775807) returns the location index value of the first element queried. You can specify the start and end locations of the query
>>> a=(1,2,3,4,3,2,1,2,3,4) >>> a.index(3) 2 >>> a.index(3,3) 4 >>> a.index(3,5,8) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: tuple.index(x): x not in tuple