@@ -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
358368As you see, on output tuples are always enclosed in parentheses, so that nested
359369tuples are interpreted correctly; they may be input with or without surrounding
360370parentheses, 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
369383A special problem is the construction of tuples containing 0 or 1 items: the
370384syntax 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
393407sequence. Note that multiple assignment is really just a combination of tuple
394408packing and sequence unpacking.
395409
396- .. XXX Add a bit on the difference between tuples and lists.
397-
398410
399411.. _tut-sets :
400412
0 commit comments