• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. _tut-structures:
2
3***************
4Data Structures
5***************
6
7This chapter describes some things you've learned about already in more detail,
8and adds some new things as well.
9
10
11.. _tut-morelists:
12
13More on Lists
14=============
15
16The list data type has some more methods.  Here are all of the methods of list
17objects:
18
19
20.. method:: list.append(x)
21   :noindex:
22
23   Add an item to the end of the list; equivalent to ``a[len(a):] = [x]``.
24
25
26.. method:: list.extend(L)
27   :noindex:
28
29   Extend the list by appending all the items in the given list; equivalent to
30   ``a[len(a):] = L``.
31
32
33.. method:: list.insert(i, x)
34   :noindex:
35
36   Insert an item at a given position.  The first argument is the index of the
37   element before which to insert, so ``a.insert(0, x)`` inserts at the front of
38   the list, and ``a.insert(len(a), x)`` is equivalent to ``a.append(x)``.
39
40
41.. method:: list.remove(x)
42   :noindex:
43
44   Remove the first item from the list whose value is *x*. It is an error if there
45   is no such item.
46
47
48.. method:: list.pop([i])
49   :noindex:
50
51   Remove the item at the given position in the list, and return it.  If no index
52   is specified, ``a.pop()`` removes and returns the last item in the list.  (The
53   square brackets around the *i* in the method signature denote that the parameter
54   is optional, not that you should type square brackets at that position.  You
55   will see this notation frequently in the Python Library Reference.)
56
57
58.. method:: list.index(x)
59   :noindex:
60
61   Return the index in the list of the first item whose value is *x*. It is an
62   error if there is no such item.
63
64
65.. method:: list.count(x)
66   :noindex:
67
68   Return the number of times *x* appears in the list.
69
70
71.. method:: list.sort(cmp=None, key=None, reverse=False)
72   :noindex:
73
74   Sort the items of the list in place (the arguments can be used for sort
75   customization, see :func:`sorted` for their explanation).
76
77
78.. method:: list.reverse()
79   :noindex:
80
81   Reverse the elements of the list, in place.
82
83An example that uses most of the list methods::
84
85   >>> a = [66.25, 333, 333, 1, 1234.5]
86   >>> print a.count(333), a.count(66.25), a.count('x')
87   2 1 0
88   >>> a.insert(2, -1)
89   >>> a.append(333)
90   >>> a
91   [66.25, 333, -1, 333, 1, 1234.5, 333]
92   >>> a.index(333)
93   1
94   >>> a.remove(333)
95   >>> a
96   [66.25, -1, 333, 1, 1234.5, 333]
97   >>> a.reverse()
98   >>> a
99   [333, 1234.5, 1, 333, -1, 66.25]
100   >>> a.sort()
101   >>> a
102   [-1, 1, 66.25, 333, 333, 1234.5]
103   >>> a.pop()
104   1234.5
105   >>> a
106   [-1, 1, 66.25, 333, 333]
107
108You might have noticed that methods like ``insert``, ``remove`` or ``sort`` that
109only modify the list have no return value printed -- they return the default
110``None``.  This is a design principle for all mutable data structures in
111Python.
112
113
114.. _tut-lists-as-stacks:
115
116Using Lists as Stacks
117---------------------
118
119.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
120
121
122The list methods make it very easy to use a list as a stack, where the last
123element added is the first element retrieved ("last-in, first-out").  To add an
124item to the top of the stack, use :meth:`append`.  To retrieve an item from the
125top of the stack, use :meth:`pop` without an explicit index.  For example::
126
127   >>> stack = [3, 4, 5]
128   >>> stack.append(6)
129   >>> stack.append(7)
130   >>> stack
131   [3, 4, 5, 6, 7]
132   >>> stack.pop()
133   7
134   >>> stack
135   [3, 4, 5, 6]
136   >>> stack.pop()
137   6
138   >>> stack.pop()
139   5
140   >>> stack
141   [3, 4]
142
143
144.. _tut-lists-as-queues:
145
146Using Lists as Queues
147---------------------
148
149.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
150
151It is also possible to use a list as a queue, where the first element added is
152the first element retrieved ("first-in, first-out"); however, lists are not
153efficient for this purpose.  While appends and pops from the end of list are
154fast, doing inserts or pops from the beginning of a list is slow (because all
155of the other elements have to be shifted by one).
156
157To implement a queue, use :class:`collections.deque` which was designed to
158have fast appends and pops from both ends.  For example::
159
160   >>> from collections import deque
161   >>> queue = deque(["Eric", "John", "Michael"])
162   >>> queue.append("Terry")           # Terry arrives
163   >>> queue.append("Graham")          # Graham arrives
164   >>> queue.popleft()                 # The first to arrive now leaves
165   'Eric'
166   >>> queue.popleft()                 # The second to arrive now leaves
167   'John'
168   >>> queue                           # Remaining queue in order of arrival
169   deque(['Michael', 'Terry', 'Graham'])
170
171
172.. _tut-functional:
173
174Functional Programming Tools
175----------------------------
176
177There are three built-in functions that are very useful when used with lists:
178:func:`filter`, :func:`map`, and :func:`reduce`.
179
180``filter(function, sequence)`` returns a sequence consisting of those items from
181the sequence for which ``function(item)`` is true. If *sequence* is a
182:class:`str`, :class:`unicode` or :class:`tuple`, the result will be of the
183same type; otherwise, it is always a :class:`list`.  For example, to compute a
184sequence of numbers divisible by 3 or 5::
185
186   >>> def f(x): return x % 3 == 0 or x % 5 == 0
187   ...
188   >>> filter(f, range(2, 25))
189   [3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24]
190
191``map(function, sequence)`` calls ``function(item)`` for each of the sequence's
192items and returns a list of the return values.  For example, to compute some
193cubes::
194
195   >>> def cube(x): return x*x*x
196   ...
197   >>> map(cube, range(1, 11))
198   [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
199
200More than one sequence may be passed; the function must then have as many
201arguments as there are sequences and is called with the corresponding item from
202each sequence (or ``None`` if some sequence is shorter than another).  For
203example::
204
205   >>> seq = range(8)
206   >>> def add(x, y): return x+y
207   ...
208   >>> map(add, seq, seq)
209   [0, 2, 4, 6, 8, 10, 12, 14]
210
211``reduce(function, sequence)`` returns a single value constructed by calling the
212binary function *function* on the first two items of the sequence, then on the
213result and the next item, and so on.  For example, to compute the sum of the
214numbers 1 through 10::
215
216   >>> def add(x,y): return x+y
217   ...
218   >>> reduce(add, range(1, 11))
219   55
220
221If there's only one item in the sequence, its value is returned; if the sequence
222is empty, an exception is raised.
223
224A third argument can be passed to indicate the starting value.  In this case the
225starting value is returned for an empty sequence, and the function is first
226applied to the starting value and the first sequence item, then to the result
227and the next item, and so on.  For example, ::
228
229   >>> def sum(seq):
230   ...     def add(x,y): return x+y
231   ...     return reduce(add, seq, 0)
232   ...
233   >>> sum(range(1, 11))
234   55
235   >>> sum([])
236   0
237
238Don't use this example's definition of :func:`sum`: since summing numbers is
239such a common need, a built-in function ``sum(sequence)`` is already provided,
240and works exactly like this.
241
242.. _tut-listcomps:
243
244List Comprehensions
245-------------------
246
247List comprehensions provide a concise way to create lists.
248Common applications are to make new lists where each element is the result of
249some operations applied to each member of another sequence or iterable, or to
250create a subsequence of those elements that satisfy a certain condition.
251
252For example, assume we want to create a list of squares, like::
253
254   >>> squares = []
255   >>> for x in range(10):
256   ...     squares.append(x**2)
257   ...
258   >>> squares
259   [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
260
261We can obtain the same result with::
262
263   squares = [x**2 for x in range(10)]
264
265This is also equivalent to ``squares = map(lambda x: x**2, range(10))``,
266but it's more concise and readable.
267
268A list comprehension consists of brackets containing an expression followed
269by a :keyword:`for` clause, then zero or more :keyword:`for` or :keyword:`if`
270clauses.  The result will be a new list resulting from evaluating the expression
271in the context of the :keyword:`for` and :keyword:`if` clauses which follow it.
272For example, this listcomp combines the elements of two lists if they are not
273equal::
274
275   >>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
276   [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
277
278and it's equivalent to:
279
280   >>> combs = []
281   >>> for x in [1,2,3]:
282   ...     for y in [3,1,4]:
283   ...         if x != y:
284   ...             combs.append((x, y))
285   ...
286   >>> combs
287   [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
288
289Note how the order of the :keyword:`for` and :keyword:`if` statements is the
290same in both these snippets.
291
292If the expression is a tuple (e.g. the ``(x, y)`` in the previous example),
293it must be parenthesized. ::
294
295   >>> vec = [-4, -2, 0, 2, 4]
296   >>> # create a new list with the values doubled
297   >>> [x*2 for x in vec]
298   [-8, -4, 0, 4, 8]
299   >>> # filter the list to exclude negative numbers
300   >>> [x for x in vec if x >= 0]
301   [0, 2, 4]
302   >>> # apply a function to all the elements
303   >>> [abs(x) for x in vec]
304   [4, 2, 0, 2, 4]
305   >>> # call a method on each element
306   >>> freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
307   >>> [weapon.strip() for weapon in freshfruit]
308   ['banana', 'loganberry', 'passion fruit']
309   >>> # create a list of 2-tuples like (number, square)
310   >>> [(x, x**2) for x in range(6)]
311   [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
312   >>> # the tuple must be parenthesized, otherwise an error is raised
313   >>> [x, x**2 for x in range(6)]
314     File "<stdin>", line 1, in <module>
315       [x, x**2 for x in range(6)]
316                  ^
317   SyntaxError: invalid syntax
318   >>> # flatten a list using a listcomp with two 'for'
319   >>> vec = [[1,2,3], [4,5,6], [7,8,9]]
320   >>> [num for elem in vec for num in elem]
321   [1, 2, 3, 4, 5, 6, 7, 8, 9]
322
323List comprehensions can contain complex expressions and nested functions::
324
325   >>> from math import pi
326   >>> [str(round(pi, i)) for i in range(1, 6)]
327   ['3.1', '3.14', '3.142', '3.1416', '3.14159']
328
329
330Nested List Comprehensions
331''''''''''''''''''''''''''
332
333The initial expression in a list comprehension can be any arbitrary expression,
334including another list comprehension.
335
336Consider the following example of a 3x4 matrix implemented as a list of
3373 lists of length 4::
338
339   >>> matrix = [
340   ...     [1, 2, 3, 4],
341   ...     [5, 6, 7, 8],
342   ...     [9, 10, 11, 12],
343   ... ]
344
345The following list comprehension will transpose rows and columns::
346
347   >>> [[row[i] for row in matrix] for i in range(4)]
348   [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
349
350As we saw in the previous section, the nested listcomp is evaluated in
351the context of the :keyword:`for` that follows it, so this example is
352equivalent to::
353
354   >>> transposed = []
355   >>> for i in range(4):
356   ...     transposed.append([row[i] for row in matrix])
357   ...
358   >>> transposed
359   [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
360
361which, in turn, is the same as::
362
363   >>> transposed = []
364   >>> for i in range(4):
365   ...     # the following 3 lines implement the nested listcomp
366   ...     transposed_row = []
367   ...     for row in matrix:
368   ...         transposed_row.append(row[i])
369   ...     transposed.append(transposed_row)
370   ...
371   >>> transposed
372   [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
373
374
375In the real world, you should prefer built-in functions to complex flow statements.
376The :func:`zip` function would do a great job for this use case::
377
378   >>> zip(*matrix)
379   [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
380
381See :ref:`tut-unpacking-arguments` for details on the asterisk in this line.
382
383.. _tut-del:
384
385The :keyword:`del` statement
386============================
387
388There is a way to remove an item from a list given its index instead of its
389value: the :keyword:`del` statement.  This differs from the :meth:`pop` method
390which returns a value.  The :keyword:`del` statement can also be used to remove
391slices from a list or clear the entire list (which we did earlier by assignment
392of an empty list to the slice).  For example::
393
394   >>> a = [-1, 1, 66.25, 333, 333, 1234.5]
395   >>> del a[0]
396   >>> a
397   [1, 66.25, 333, 333, 1234.5]
398   >>> del a[2:4]
399   >>> a
400   [1, 66.25, 1234.5]
401   >>> del a[:]
402   >>> a
403   []
404
405:keyword:`del` can also be used to delete entire variables::
406
407   >>> del a
408
409Referencing the name ``a`` hereafter is an error (at least until another value
410is assigned to it).  We'll find other uses for :keyword:`del` later.
411
412
413.. _tut-tuples:
414
415Tuples and Sequences
416====================
417
418We saw that lists and strings have many common properties, such as indexing and
419slicing operations.  They are two examples of *sequence* data types (see
420:ref:`typesseq`).  Since Python is an evolving language, other sequence data
421types may be added.  There is also another standard sequence data type: the
422*tuple*.
423
424A tuple consists of a number of values separated by commas, for instance::
425
426   >>> t = 12345, 54321, 'hello!'
427   >>> t[0]
428   12345
429   >>> t
430   (12345, 54321, 'hello!')
431   >>> # Tuples may be nested:
432   ... u = t, (1, 2, 3, 4, 5)
433   >>> u
434   ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
435   >>> # Tuples are immutable:
436   ... t[0] = 88888
437   Traceback (most recent call last):
438     File "<stdin>", line 1, in <module>
439   TypeError: 'tuple' object does not support item assignment
440   >>> # but they can contain mutable objects:
441   ... v = ([1, 2, 3], [3, 2, 1])
442   >>> v
443   ([1, 2, 3], [3, 2, 1])
444
445
446As you see, on output tuples are always enclosed in parentheses, so that nested
447tuples are interpreted correctly; they may be input with or without surrounding
448parentheses, although often parentheses are necessary anyway (if the tuple is
449part of a larger expression).  It is not possible to assign to the individual
450items of a tuple, however it is possible to create tuples which contain mutable
451objects, such as lists.
452
453Though tuples may seem similar to lists, they are often used in different
454situations and for different purposes.
455Tuples are :term:`immutable`, and usually contain a heterogeneous sequence of
456elements that are accessed via unpacking (see later in this section) or indexing
457(or even by attribute in the case of :func:`namedtuples <collections.namedtuple>`).
458Lists are :term:`mutable`, and their elements are usually homogeneous and are
459accessed by iterating over the list.
460
461A special problem is the construction of tuples containing 0 or 1 items: the
462syntax has some extra quirks to accommodate these.  Empty tuples are constructed
463by an empty pair of parentheses; a tuple with one item is constructed by
464following a value with a comma (it is not sufficient to enclose a single value
465in parentheses). Ugly, but effective.  For example::
466
467   >>> empty = ()
468   >>> singleton = 'hello',    # <-- note trailing comma
469   >>> len(empty)
470   0
471   >>> len(singleton)
472   1
473   >>> singleton
474   ('hello',)
475
476The statement ``t = 12345, 54321, 'hello!'`` is an example of *tuple packing*:
477the values ``12345``, ``54321`` and ``'hello!'`` are packed together in a tuple.
478The reverse operation is also possible::
479
480   >>> x, y, z = t
481
482This is called, appropriately enough, *sequence unpacking* and works for any
483sequence on the right-hand side.  Sequence unpacking requires the list of
484variables on the left to have the same number of elements as the length of the
485sequence.  Note that multiple assignment is really just a combination of tuple
486packing and sequence unpacking.
487
488
489.. _tut-sets:
490
491Sets
492====
493
494Python also includes a data type for *sets*.  A set is an unordered collection
495with no duplicate elements.  Basic uses include membership testing and
496eliminating duplicate entries.  Set objects also support mathematical operations
497like union, intersection, difference, and symmetric difference.
498
499Curly braces or the :func:`set` function can be used to create sets.  Note: to
500create an empty set you have to use ``set()``, not ``{}``; the latter creates an
501empty dictionary, a data structure that we discuss in the next section.
502
503Here is a brief demonstration::
504
505   >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
506   >>> fruit = set(basket)               # create a set without duplicates
507   >>> fruit
508   set(['orange', 'pear', 'apple', 'banana'])
509   >>> 'orange' in fruit                 # fast membership testing
510   True
511   >>> 'crabgrass' in fruit
512   False
513
514   >>> # Demonstrate set operations on unique letters from two words
515   ...
516   >>> a = set('abracadabra')
517   >>> b = set('alacazam')
518   >>> a                                  # unique letters in a
519   set(['a', 'r', 'b', 'c', 'd'])
520   >>> a - b                              # letters in a but not in b
521   set(['r', 'd', 'b'])
522   >>> a | b                              # letters in either a or b
523   set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
524   >>> a & b                              # letters in both a and b
525   set(['a', 'c'])
526   >>> a ^ b                              # letters in a or b but not both
527   set(['r', 'd', 'b', 'm', 'z', 'l'])
528
529Similarly to :ref:`list comprehensions <tut-listcomps>`, set comprehensions
530are also supported::
531
532   >>> a = {x for x in 'abracadabra' if x not in 'abc'}
533   >>> a
534   set(['r', 'd'])
535
536
537.. _tut-dictionaries:
538
539Dictionaries
540============
541
542Another useful data type built into Python is the *dictionary* (see
543:ref:`typesmapping`). Dictionaries are sometimes found in other languages as
544"associative memories" or "associative arrays".  Unlike sequences, which are
545indexed by a range of numbers, dictionaries are indexed by *keys*, which can be
546any immutable type; strings and numbers can always be keys.  Tuples can be used
547as keys if they contain only strings, numbers, or tuples; if a tuple contains
548any mutable object either directly or indirectly, it cannot be used as a key.
549You can't use lists as keys, since lists can be modified in place using index
550assignments, slice assignments, or methods like :meth:`append` and
551:meth:`extend`.
552
553It is best to think of a dictionary as an unordered set of *key: value* pairs,
554with the requirement that the keys are unique (within one dictionary). A pair of
555braces creates an empty dictionary: ``{}``. Placing a comma-separated list of
556key:value pairs within the braces adds initial key:value pairs to the
557dictionary; this is also the way dictionaries are written on output.
558
559The main operations on a dictionary are storing a value with some key and
560extracting the value given the key.  It is also possible to delete a key:value
561pair with ``del``. If you store using a key that is already in use, the old
562value associated with that key is forgotten.  It is an error to extract a value
563using a non-existent key.
564
565The :meth:`keys` method of a dictionary object returns a list of all the keys
566used in the dictionary, in arbitrary order (if you want it sorted, just apply
567the :func:`sorted` function to it).  To check whether a single key is in the
568dictionary, use the :keyword:`in` keyword.
569
570Here is a small example using a dictionary::
571
572   >>> tel = {'jack': 4098, 'sape': 4139}
573   >>> tel['guido'] = 4127
574   >>> tel
575   {'sape': 4139, 'guido': 4127, 'jack': 4098}
576   >>> tel['jack']
577   4098
578   >>> del tel['sape']
579   >>> tel['irv'] = 4127
580   >>> tel
581   {'guido': 4127, 'irv': 4127, 'jack': 4098}
582   >>> tel.keys()
583   ['guido', 'irv', 'jack']
584   >>> 'guido' in tel
585   True
586
587The :func:`dict` constructor builds dictionaries directly from sequences of
588key-value pairs::
589
590   >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
591   {'sape': 4139, 'jack': 4098, 'guido': 4127}
592
593In addition, dict comprehensions can be used to create dictionaries from
594arbitrary key and value expressions::
595
596   >>> {x: x**2 for x in (2, 4, 6)}
597   {2: 4, 4: 16, 6: 36}
598
599When the keys are simple strings, it is sometimes easier to specify pairs using
600keyword arguments::
601
602   >>> dict(sape=4139, guido=4127, jack=4098)
603   {'sape': 4139, 'jack': 4098, 'guido': 4127}
604
605
606.. _tut-loopidioms:
607
608Looping Techniques
609==================
610
611When looping through a sequence, the position index and corresponding value can
612be retrieved at the same time using the :func:`enumerate` function. ::
613
614   >>> for i, v in enumerate(['tic', 'tac', 'toe']):
615   ...     print i, v
616   ...
617   0 tic
618   1 tac
619   2 toe
620
621To loop over two or more sequences at the same time, the entries can be paired
622with the :func:`zip` function. ::
623
624   >>> questions = ['name', 'quest', 'favorite color']
625   >>> answers = ['lancelot', 'the holy grail', 'blue']
626   >>> for q, a in zip(questions, answers):
627   ...     print 'What is your {0}?  It is {1}.'.format(q, a)
628   ...
629   What is your name?  It is lancelot.
630   What is your quest?  It is the holy grail.
631   What is your favorite color?  It is blue.
632
633To loop over a sequence in reverse, first specify the sequence in a forward
634direction and then call the :func:`reversed` function. ::
635
636   >>> for i in reversed(xrange(1,10,2)):
637   ...     print i
638   ...
639   9
640   7
641   5
642   3
643   1
644
645To loop over a sequence in sorted order, use the :func:`sorted` function which
646returns a new sorted list while leaving the source unaltered. ::
647
648   >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
649   >>> for f in sorted(set(basket)):
650   ...     print f
651   ...
652   apple
653   banana
654   orange
655   pear
656
657When looping through dictionaries, the key and corresponding value can be
658retrieved at the same time using the :meth:`iteritems` method. ::
659
660   >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
661   >>> for k, v in knights.iteritems():
662   ...     print k, v
663   ...
664   gallahad the pure
665   robin the brave
666
667It is sometimes tempting to change a list while you are looping over it;
668however, it is often simpler and safer to create a new list instead. ::
669
670   >>> import math
671   >>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
672   >>> filtered_data = []
673   >>> for value in raw_data:
674   ...     if not math.isnan(value):
675   ...         filtered_data.append(value)
676   ...
677   >>> filtered_data
678   [56.2, 51.7, 55.3, 52.5, 47.8]
679
680
681.. _tut-conditions:
682
683More on Conditions
684==================
685
686The conditions used in ``while`` and ``if`` statements can contain any
687operators, not just comparisons.
688
689The comparison operators ``in`` and ``not in`` check whether a value occurs
690(does not occur) in a sequence.  The operators ``is`` and ``is not`` compare
691whether two objects are really the same object; this only matters for mutable
692objects like lists.  All comparison operators have the same priority, which is
693lower than that of all numerical operators.
694
695Comparisons can be chained.  For example, ``a < b == c`` tests whether ``a`` is
696less than ``b`` and moreover ``b`` equals ``c``.
697
698Comparisons may be combined using the Boolean operators ``and`` and ``or``, and
699the outcome of a comparison (or of any other Boolean expression) may be negated
700with ``not``.  These have lower priorities than comparison operators; between
701them, ``not`` has the highest priority and ``or`` the lowest, so that ``A and
702not B or C`` is equivalent to ``(A and (not B)) or C``. As always, parentheses
703can be used to express the desired composition.
704
705The Boolean operators ``and`` and ``or`` are so-called *short-circuit*
706operators: their arguments are evaluated from left to right, and evaluation
707stops as soon as the outcome is determined.  For example, if ``A`` and ``C`` are
708true but ``B`` is false, ``A and B and C`` does not evaluate the expression
709``C``.  When used as a general value and not as a Boolean, the return value of a
710short-circuit operator is the last evaluated argument.
711
712It is possible to assign the result of a comparison or other Boolean expression
713to a variable.  For example, ::
714
715   >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
716   >>> non_null = string1 or string2 or string3
717   >>> non_null
718   'Trondheim'
719
720Note that in Python, unlike C, assignment cannot occur inside expressions. C
721programmers may grumble about this, but it avoids a common class of problems
722encountered in C programs: typing ``=`` in an expression when ``==`` was
723intended.
724
725
726.. _tut-comparing:
727
728Comparing Sequences and Other Types
729===================================
730
731Sequence objects may be compared to other objects with the same sequence type.
732The comparison uses *lexicographical* ordering: first the first two items are
733compared, and if they differ this determines the outcome of the comparison; if
734they are equal, the next two items are compared, and so on, until either
735sequence is exhausted. If two items to be compared are themselves sequences of
736the same type, the lexicographical comparison is carried out recursively.  If
737all items of two sequences compare equal, the sequences are considered equal.
738If one sequence is an initial sub-sequence of the other, the shorter sequence is
739the smaller (lesser) one.  Lexicographical ordering for strings uses the ASCII
740ordering for individual characters.  Some examples of comparisons between
741sequences of the same type::
742
743   (1, 2, 3)              < (1, 2, 4)
744   [1, 2, 3]              < [1, 2, 4]
745   'ABC' < 'C' < 'Pascal' < 'Python'
746   (1, 2, 3, 4)           < (1, 2, 4)
747   (1, 2)                 < (1, 2, -1)
748   (1, 2, 3)             == (1.0, 2.0, 3.0)
749   (1, 2, ('aa', 'ab'))   < (1, 2, ('abc', 'a'), 4)
750
751Note that comparing objects of different types is legal.  The outcome is
752deterministic but arbitrary: the types are ordered by their name. Thus, a list
753is always smaller than a string, a string is always smaller than a tuple, etc.
754[#]_ Mixed numeric types are compared according to their numeric value, so 0
755equals 0.0, etc.
756
757
758.. rubric:: Footnotes
759
760.. [#] The rules for comparing objects of different types should not be relied upon;
761   they may change in a future version of the language.
762
763