Getting two python (List and Tuple type) [2-7 python create Tuple]

2-7 python create Tuple

Another tuple is an ordered list, the Chinese translation for "tuple." tuple and the list is very similar, however, tuple Once created, it can not be modified.

Also represents the name of his classmates, with tuple is expressed as follows:

1 >>> t = ('Adam', 'Lisa', 'Bart')

The only difference is created and create list of the tuple is ( )replaced[ ] .

Now, this  t can not be changed, tuple no append () method, there is no insert () and pop () method . Therefore, new students can not be directly added to the tuple, old classmates wanted to quit tuple does not work.

Gets a tuple and list elements are exactly the same way, we can use the normal t [0], t [-1] and other indexed access to elements, but can not be assigned to other elements, do not believe you can try:

1 >>> t[0] = 'Paul'
2 Traceback (most recent call last):
3   File "<stdin>", line 1, in <module>
4 TypeError: 'tuple' object does not support item assignment

 

Guess you like

Origin www.cnblogs.com/ucasljq/p/11585892.html