We can see that the list and string have many identical attributes, such as the index and slice operators. The following are two examples of sequence data types. Since python is an ever-evolving language, other sequence data types can be added. There is now a standard sequence data type: tuples
A tuples are well-known and separated by commas. For example:
>>> T = 12345,543 21, 'Hello! '
>>> T [0]
12345
>>> T
(12345,543 21, 'Hello! ')
>>># Tuples can also be nested
... U = t, (1, 2, 3, 4, 5)
>>> U
(12345,543 21, 'Hello! '), (1, 2, 3, 4, 5 ))
>>> # The tuples are unchangeable.
... T [0] = 88888
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment tuples do not support element assignment,
>>># But they can contain mutable objects: but it can contain variable objects.
... V = ([1, 2, 3], [3, 2, 1])
>>> V
([1, 2, 3], [3, 2, 1])
As you can see, when the output is often enclosed in parentheses, the nested tuples can be correctly interpreted. Although they are often enclosed in parentheses during input, both the package and the package can be entered. You cannot assign values to a single object in a tuples, but create tuples containing variable objects, such as lists.
Although tuples are similar to lists, they are often used in different situations and for different purposes. Tuples are immutable and often contain a heterogeneous sequence of elements that can be accessed through retrieval and indexing. The list is variable, and these elements are equal. They can only be accessed through traversal.
A special problem is that it only contains the construction of one or more empty tuples. To comply with these rules, this syntax is a bit special. Each empty tuples can be created by a pair of empty brackets. You can add "," to a single value to create a tuple containing one value. (It is meaningless to enclose a single value with parentheses ). It looks ugly but effective. For example:
>>> Empty = ()
>>> Singleton = 'hello', # <-- note trailing comma
>>> Len (empty)
0
>>> Len (singleton)
1
>>> Singleton
('Hello ',)
Statement t = 12345,543 21, 'Hello! 'Is an example of a tuples containing different values: the value is 12345,543 21
And 'Hello! 'Are all placed in the same tuples.
You can perform the following operations to flip tuples:
>>> X, y, z = t
The number of variables on the left side of the sequence is the same as the number of elements in the sequence. Note that variable parameters are only a combination of tuples encapsulation and sequential unblocking!