Getting Started with Python [4-2] created tuple, create a single-element tuple, the tuple variable

1. Create a 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:

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

Create a tuple and create list only difference is () instead of the [].

Now, this t can not be changed, ** tuple no append () method, there is no insert () and pop () method. ** So, 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 is exactly the same way, we can access the elements using t [0], t [-1] such as indexing, but can not be assigned to other elements, ** do not believe you can try:

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

Task:
create a tuple, the sequence containing 0--9 that 10 number.

Since writing code

t = (0,1,2,3,4,5,6,7,8,9)
print t

Here Insert Picture Description

2. Create a single element Tuple

and the same tuple list, may comprise 0, 1, and any number of elements.

Contains multiple elements of tuple, earlier we have created before.

Contains zero elements tuple, i.e. empty tuple, by a direct ():

>>> t = ()
>>> print t
()

Create a tuple contains one element of it? To try:

>>> t = (1)
>>> print t
1

Where not like! t is not a tuple, but an integer. why?

Since () may be expressed tuple, but also as brackets indicate the priority calculation result, (1) was calculated result Python interpreter 1, we get tuple not cause, but an integer.

It is because with () defined single-element tuple ambiguous, so Python provides a single-element tuple to be more a comma ",", thus avoiding the ambiguity:

>>> t = (1,)
>>> print t
(1,)

Python when printing single-element tuple, also automatically add a "," in order to more clearly tell you that this is a tuple.

Multi-element tuple plus without the extra "," the effect is the same:

>>> t = (1, 2, 3,)
>>> print t
(1, 2, 3)

Task:
Please indicate the right code editor Why not create that contains a student tuple:

t = ('Adam')

print t

Please modify the code, make sure that t is a tuple.

Modify the code:

t = ('Adam',)
print t

3. The variable tuple

Earlier we saw a tuple can not be modified once created. Now, we look at a "variable" in the tuple:

>>> t = ('a', 'b', ['A', 'B'])

Noting t has three elements: 'a', 'b' and a list: [ 'A', 'B']. list as a whole is the third element of the tuple. Object list by t [2] to get:

>>> L = t[2]

Then, we altered the list of two elements:

>>> L[0] = 'X'
>>> L[1] = 'Y'

Look at the contents of the tuple:

>>> print t
('a', 'b', ['X', 'Y'])

Not to say that once the tuple variable is not defined yet? How now changed?

Do not worry, when we look at the definition of tuple contains three elements:

When we list the elements of 'A' and 'B' after modified 'X' and 'Y', tuple becomes: On the surface, the tuple element has indeed changed, but in fact is not changed tuple elements, but the list of elements.Here Insert Picture Description


Here Insert Picture Description

tuple a start point of the list is not changed to another list,

So, tuple-called "change" is that each element of the tuple, point to never change. That point 'a', can not be changed to point 'b', a point list, pointing to other objects can not be changed, but this list points to itself is variable!

After understanding the "point change", to create a content is constant tuple how to do? We must ensure that each element of the tuple itself can not be changed.

Task:
defines the tuple:

t = (‘a’, ‘b’, [‘A’, ‘B’])

Since t contains a list of elements, causes the contents of the tuple is variable. Can I modify the code above, let tuple content immutable?

Since writing:

t = ('a', 'b', ('A', 'B'))
print t
Published 20 original articles · won praise 0 · Views 416

Guess you like

Origin blog.csdn.net/yipyuenkay/article/details/103875625