Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
f90ea1f0
Commit
f90ea1f0
authored
Jun 17, 2012
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
parent
14d99a14
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
9 deletions
+22
-9
Doc/tutorial/datastructures.rst
Doc/tutorial/datastructures.rst
+21
-9
Misc/ACKS
Misc/ACKS
+1
-0
No files found.
Doc/tutorial/datastructures.rst
View file @
f90ea1f0
...
...
@@ -349,17 +349,31 @@ A tuple consists of a number of values separated by commas, for instance::
... 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])
As you see, on output tuples are always enclosed in parentheses, so that nested
tuples are interpreted correctly; they may be input with or without surrounding
parentheses, although often parentheses are necessary anyway (if the tuple is
part of a larger expression).
Tuples have many uses. For example: (x, y) coordinate pairs, employee records
from a database, etc. Tuples, like strings, are immutable: it is not possible
to assign to the individual items of a tuple (you can simulate much of the same
effect with slicing and concatenation, though). It is also possible to create
tuples which contain mutable objects, such as lists.
part of a larger expression). 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.
Though tuples may seem similar to lists, they are often used in different
situations and for different purposes.
Tuples are :term:`immutable`, and usually contain an heterogeneous sequence of
elements that are accessed via unpacking (see later in this section) or indexing
(or even by attribute in the case of :func:`namedtuples <collections.namedtuple>`).
Lists are :term:`mutable`, and their elements are usually homogeneous and are
accessed by iterating over the list.
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
...
...
@@ -388,8 +402,6 @@ 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.
.. XXX Add a bit on the difference between tuples and lists.
.. _tut-sets:
...
...
Misc/ACKS
View file @
f90ea1f0
...
...
@@ -986,6 +986,7 @@ Larry Wall
Kevin Walzer
Rodrigo Steinmuller Wanderley
Greg Ward
Zachary Ware
Barry Warsaw
Steve Waterbury
Bob Watson
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment