Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f90ea1f

Browse files
committed
#14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
1 parent 14d99a1 commit f90ea1f

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

Doc/tutorial/datastructures.rst

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,31 @@ A tuple consists of a number of values separated by commas, for instance::
349349
... u = t, (1, 2, 3, 4, 5)
350350
>>> u
351351
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
352+
>>> # Tuples are immutable:
353+
... t[0] = 88888
354+
Traceback (most recent call last):
355+
File "<stdin>", line 1, in <module>
356+
TypeError: 'tuple' object does not support item assignment
357+
>>> # but they can contain mutable objects:
358+
... v = ([1, 2, 3], [3, 2, 1])
359+
>>> v
360+
([1, 2, 3], [3, 2, 1])
361+
352362

353363
As you see, on output tuples are always enclosed in parentheses, so that nested
354364
tuples are interpreted correctly; they may be input with or without surrounding
355365
parentheses, although often parentheses are necessary anyway (if the tuple is
356-
part of a larger expression).
357-
358-
Tuples have many uses. For example: (x, y) coordinate pairs, employee records
359-
from a database, etc. Tuples, like strings, are immutable: it is not possible
360-
to assign to the individual items of a tuple (you can simulate much of the same
361-
effect with slicing and concatenation, though). It is also possible to create
362-
tuples which contain mutable objects, such as lists.
366+
part of a larger expression). It is not possible to assign to the individual
367+
items of a tuple, however it is possible to create tuples which contain mutable
368+
objects, such as lists.
369+
370+
Though tuples may seem similar to lists, they are often used in different
371+
situations and for different purposes.
372+
Tuples are :term:`immutable`, and usually contain an heterogeneous sequence of
373+
elements that are accessed via unpacking (see later in this section) or indexing
374+
(or even by attribute in the case of :func:`namedtuples <collections.namedtuple>`).
375+
Lists are :term:`mutable`, and their elements are usually homogeneous and are
376+
accessed by iterating over the list.
363377

364378
A special problem is the construction of tuples containing 0 or 1 items: the
365379
syntax has some extra quirks to accommodate these. Empty tuples are constructed
@@ -388,8 +402,6 @@ many variables on the left side of the equals sign as there are elements in the
388402
sequence. Note that multiple assignment is really just a combination of tuple
389403
packing and sequence unpacking.
390404

391-
.. XXX Add a bit on the difference between tuples and lists.
392-
393405

394406
.. _tut-sets:
395407

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ Larry Wall
986986
Kevin Walzer
987987
Rodrigo Steinmuller Wanderley
988988
Greg Ward
989+
Zachary Ware
989990
Barry Warsaw
990991
Steve Waterbury
991992
Bob Watson

0 commit comments

Comments
 (0)