immutable data types in Python , there is a more important role that is the tuple (tuple). If a pair of images is defined as a tuple type, then it means that its value cannot be modified unless a new pair of images is redefined. Tuples and list lists are often put together for comparison, and they are all sequences, so there are many identical ways of doing things, but the former are immutable data types, which are mutable data types, which are inherently different.
How to create tuple tuples
1, Creating a tuple method is simple, the tuple is enclosed in parentheses (), and () the elements in parentheses are separated by commas, which completes the creation of the tuple.
>>> (a)
(a)
If new is not used () parentheses are included, delimited by commas only python will also recognize it as a tuple
>>>1,2,3
(a)
If you have only one value in your newly created tuple, remember to add a comma after this value
>>>1,
(1,)
If there is no comma, Python should treat it as an integral type.
>>>1
1
2, There is an interesting tuple creation method, need to use the function of the tuple function: it can be a list of parameters, convert it to a tuple.
Tuple ([1,2,3,4])
>>> (1,2,3,4)
Tuple (' Hello ')
>>> (' h ', ' e ', ' l ', ' l ', ' o ')
Using the string as the argument, the result is that each letter in the string ' hello ' is added to the new tuple as a new element.
How to read data, values in tuples
Tuples are an ordered set of data that can be offset to a list. Simply put, tuples support indexing and slicing operations.
Index (subscript) and slice values:
x = (0,1,2,3)
X[1] Index subscript
X[2:4] Slice value does not contain four
How to modify a tuple's action method
Tuple a = (2,3,3) to turn it into (3,3,3)
List (a) Convert tuples to list a =[2,3,3]
a[0]=3 changing tuples to the first value of a list
Tuple (a)
1. First refer to the value of the variable A in the list () method and assign it to the variable B to get the list B = [2,3,3]
2, take the value of the No. 0 bit of the list variable B, and modify it to 3, when the variable b = [3,3,3]
3. Finally, the value of the variable B passed in the Tupel () method is re-assigned to the variable A, at which point A = (3,3,3)
If there are variable data types in the elements contained within the tuple, it is very convenient to modify them.
The variable name is a tuple of x, and the No. 0-bit subscript corresponds to a list because the list is a mutable data type, so you can modify it directly
1, tuples are an ordered set,
2, tuples and lists can use indexes, slices to take values.
3, after the creation of tuples can not be in situ modification and replacement operations.
4. Tuples support nesting, which can include lists, dictionaries, and different tuples.
5, tuples support the operation of general sequence, for example: +, *
>>> (+) + (3,4)
(1,2,3,4)
>>> (7,8)
(7,8,7,8,7,8,7,8)
Note the +, * operation, returns a new tuple
Python Tuple Traversal Sort operation method