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

Skip to content

Commit 5b1406f

Browse files
committed
#14840: merge with 3.2.
2 parents 307ef8a + f90ea1f commit 5b1406f

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
@@ -354,17 +354,31 @@ A tuple consists of a number of values separated by commas, for instance::
354354
... u = t, (1, 2, 3, 4, 5)
355355
>>> u
356356
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
357+
>>> # Tuples are immutable:
358+
... t[0] = 88888
359+
Traceback (most recent call last):
360+
File "<stdin>", line 1, in <module>
361+
TypeError: 'tuple' object does not support item assignment
362+
>>> # but they can contain mutable objects:
363+
... v = ([1, 2, 3], [3, 2, 1])
364+
>>> v
365+
([1, 2, 3], [3, 2, 1])
366+
357367

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

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

396-
.. XXX Add a bit on the difference between tuples and lists.
397-
398410

399411
.. _tut-sets:
400412

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,7 @@ Larry Wall
10921092
Kevin Walzer
10931093
Rodrigo Steinmuller Wanderley
10941094
Greg Ward
1095+
Zachary Ware
10951096
Barry Warsaw
10961097
Steve Waterbury
10971098
Bob Watson

0 commit comments

Comments
 (0)