Tuples and Sequences
Tuples are immutable, and usually contain a heterogeneous sequence of
elements that are accessed via unpacking (see later in this section) or indexing
(or even by attribute in the case of
>>> t = 12345, 54321, 'hello!' # Packing
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')
>>> # Tuples may be nested: ...
u = t, (1, 2, 3, 4, 5)
>>> u ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
>>> # Tuples are immutable: ...
t[0] = 88888 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
>>> # but they can contain mutable objects: ...
v = ([1, 2, 3], [3, 2, 1])
>>> v ([1, 2, 3], [3, 2, 1])
Note :
They may be input with or without surrounding parentheses .
It is not possible to assign to the individual items of a tuple, however it is possible to create tuples which contain mutable objects, such as lists.
A special problem is the construction of tuples containing 0 or 1 items: the
syntax has some extra quirks to accommodate these. Empty tuples are constructed
by an empty pair of parentheses; a tuple with one item is constructed by
following a value with a comma (it is not sufficient to enclose a single value
in parentheses). Ugly, but effective. For example:
This is called, appropriately enough, sequence unpacking and works for any
sequence on the right-hand side.
Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.
>>> t=(1)
>>> t
1
>>> type(t)
<type 'int'>
>>> t=(1,) <-- note trailing comma
>>> t
(1,)
>>> type(t)
<type 'tuple'>
>>>
namedtuples
).
Lists are mutable, and their elements are usually homogeneous and are
accessed by iterating over the list.
A tuple consists of a number of values separated by commas, for instance:
>>> t = 12345, 54321, 'hello!' # Packing
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')
>>> # Tuples may be nested: ...
u = t, (1, 2, 3, 4, 5)
>>> u ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
>>> # Tuples are immutable: ...
t[0] = 88888 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
>>> # but they can contain mutable objects: ...
v = ([1, 2, 3], [3, 2, 1])
>>> v ([1, 2, 3], [3, 2, 1])
Note :
They may be input with or without surrounding parentheses .
It is not possible to assign to the individual items of a tuple, however it is possible to create tuples which contain mutable objects, such as lists.
>>> empty = ()
>>> singleton = 'hello', # <-- note trailing comma or ('hello',)
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)
The statement
t = 12345, 54321, 'hello!'
is an example of tuple packing:
the values 12345
, 54321
and 'hello!'
are packed together in a tuple.
The reverse operation is also possible:
>>> x, y, z = t
x=12345
y=54321
z='hello!'
Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.
Difference between Lists and Tuples ?
Lists : Mutable
Tuples : Immutable
Lists can't be declared without square brackets .
Tuples can be declared without brackets .
Eg :
t = 12345, 54321, 'hello!'
( 12345, 54321, 'hello!'
)
>>> t=(1)
>>> t
1
>>> type(t)
<type 'int'>
>>> t=(1,) <-- note trailing comma
>>> t
(1,)
>>> type(t)
<type 'tuple'>
>>>
0 comments:
Post a Comment