"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "docs/script_guide/data_structures.html" of archive TestMaker.zip:


<HTML>
<!--This file created 10/18/02 4:08 PM by Claris Home Page version 3.0 30 Day Trial-->
<HEAD>
   <TITLE>TestMaker Script Guide - Data Structures</TITLE>
   <META NAME=GENERATOR CONTENT="Claris Home Page 3.0 30 Day Trial">
   <X-CLARIS-WINDOW TOP=91 BOTTOM=768 LEFT=10 RIGHT=540>
   <X-CLARIS-TAGVIEW MODE=minimal>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<H1><FONT FACE="Arial"><TABLE BORDER=0 BGCOLOR="#0000CC" CELLSPACING=1 CELLPADDING=0 WIDTH="100%">
   <TR>
      <TD>
         <P><FONT FACE="Arial"><TABLE BORDER=0 BGCOLOR="#FFFFCC" CELLSPACING=0 CELLPADDING=0 WIDTH="100%">
            <TR>
               <TD WIDTH=10>
                  <P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=10 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
               </TD>
               <TD WIDTH=120>
                  <P><A HREF="http://www.pushtotest.com"><FONT FACE="Arial"><IMG SRC="../images/ptt_logo_120w.gif" WIDTH=120 HEIGHT=32 X-CLARIS-USEIMAGEWIDTH X-CLARIS-USEIMAGEHEIGHT BORDER=0 ALIGN=bottom></FONT></A></P>
               </TD>
               <TD WIDTH=30>
                  <P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=30 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
               </TD>
               <TD>
                  <P><FONT FACE="Arial">TestMaker Script
                  Guide</FONT></P>
               </TD>
               <TD VALIGN=top ROWSPAN=3>
                  <P><A HREF="../index.html"><FONT SIZE="-1" FACE="Arial">Table
                  of Contents</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
                  <BR>
                  TestMaker Script Guide Chapters<BR>
                  </FONT><A HREF="script_index.html"><FONT SIZE="-1" FACE="Arial">Introduction</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
                  </FONT><A HREF="flow_control.html"><FONT SIZE="-1" FACE="Arial">Flow
                  Control</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
                  </FONT><A HREF="data_structures.html"><FONT SIZE="-1" FACE="Arial">Data
                  Structures</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
                  </FONT><A HREF="modules.html"><FONT SIZE="-1" FACE="Arial">Modules</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
                  </FONT><A HREF="input_output.html"><FONT SIZE="-1" FACE="Arial">Input
                  and Output</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
                  </FONT><A HREF="exceptions.html"><FONT SIZE="-1" FACE="Arial">Errors
                  and Exceptions</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
                  </FONT><A HREF="classes.html"><FONT SIZE="-1" FACE="Arial">Classes</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
                  </FONT><A HREF="floating_point.html"><FONT SIZE="-1" FACE="Arial">Floating
                  Point Numbers</FONT></A></P>
                  
                  <P></P>
               </TD>
            </TR>
            <TR>
               <TD WIDTH=10>
                  <P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=10 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
               </TD>
               <TD WIDTH=120>
                  <P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=120 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
               </TD>
               <TD WIDTH=30>
                  <P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=30 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
               </TD>
               <TD VALIGN=top>
                  <P><FONT SIZE="-1" FACE="Arial">Published: October
                  21, 2002<BR>
                  Applies to: TestMaker 3.0</FONT></P>
               </TD>
            </TR>
            <TR>
               <TD WIDTH=10>
                  <P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=10 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
               </TD>
               <TD WIDTH=120>
                  <P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=120 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
               </TD>
               <TD WIDTH=30>
                  <P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=30 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
               </TD>
               <TD VALIGN=top>
                  <P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=30 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
               </TD>
            </TR>
         </TABLE>
          </FONT></P>
      </TD>
   </TR>
</TABLE>
<BR>
Data Structures</FONT></H1>

<P><FONT FACE="Arial">This chapter describes some things you've
learned about already in more detail, and adds some new things as
well.</FONT></P>

<P>&nbsp;</P>

<H3><FONT FACE="Arial">

<HR>

</FONT><FONT FACE="Arial" COLOR="#000099">More on Lists</FONT></H3>

<P><FONT FACE="Arial">The list data type has some more methods. Here
are all of the methods of list objects:</FONT></P>

<P><FONT FACE="Arial">&nbsp;</FONT></P>

<DL>
   <DT><FONT FACE="Arial"><STRONG><CODE>append(x)</CODE></STRONG></FONT></DT>
   
   <DD><FONT FACE="Arial">Add an item to the end of the list;
   equivalent to <CODE>a&#91;len(a):&#93; =
   &#91;x&#93;</CODE>.</FONT>
   
   <P><FONT FACE="Arial">&nbsp;</FONT></P></DD>
   
   <DT><FONT FACE="Arial"><STRONG><CODE>extend(L)</CODE></STRONG></FONT></DT>
   
   <DD><FONT FACE="Arial">Extend the list by appending all the items
   in the given list; equivalent to <CODE>a&#91;len(a):&#93; =
   L</CODE>.</FONT>
   
   <P><FONT FACE="Arial">&nbsp;</FONT></P></DD>
   
   <DT><FONT FACE="Arial"><STRONG><CODE>insert(i,
   x)</CODE></STRONG></FONT></DT>
   
   <DD><FONT FACE="Arial">Insert an item at a given position. The
   first argument is the index of the element before which to insert,
   so <CODE>a.insert(0, x)</CODE> inserts at the front of the list,
   and <CODE>a.insert(len(a), x)</CODE> is equivalent to
   <CODE>a.append(x)</CODE>.</FONT>
   
   <P><FONT FACE="Arial">&nbsp;</FONT></P></DD>
   
   <DT><FONT FACE="Arial"><STRONG><CODE>remove(x)</CODE></STRONG></FONT></DT>
   
   <DD><FONT FACE="Arial">Remove the first item from the list whose
   value is <CODE>x</CODE>. It is an error if there is no such
   item.</FONT>
   
   <P><FONT FACE="Arial">&nbsp;</FONT></P></DD>
   
   <DT><FONT FACE="Arial"><STRONG><CODE>pop(&#91;i&#93;)</CODE></STRONG></FONT></DT>
   
   <DD><FONT FACE="Arial">Remove the item at the given position in
   the list, and return it. If no index is specified,
   <CODE>a.pop()</CODE> returns the last item in the list. The item
   is also removed from the list.</FONT>
   
   <P><FONT FACE="Arial">&nbsp;</FONT></P></DD>
   
   <DT><FONT FACE="Arial"><STRONG><CODE>index(x)</CODE></STRONG></FONT></DT>
   
   <DD><FONT FACE="Arial">Return the index in the list of the first
   item whose value is <CODE>x</CODE>. It is an error if there is no
   such item.</FONT>
   
   <P><FONT FACE="Arial">&nbsp;</FONT></P></DD>
   
   <DT><FONT FACE="Arial"><STRONG><CODE>count(x)</CODE></STRONG></FONT></DT>
   
   <DD><FONT FACE="Arial">Return the number of times <CODE>x</CODE>
   appears in the list.</FONT>
   
   <P><FONT FACE="Arial">&nbsp;</FONT></P></DD>
   
   <DT><FONT FACE="Arial"><STRONG><CODE>sort()</CODE></STRONG></FONT></DT>
   
   <DD><FONT FACE="Arial">Sort the items of the list, in
   place.</FONT>
   
   <P><FONT FACE="Arial">&nbsp;</FONT></P></DD>
   
   <DT><FONT FACE="Arial"><STRONG><CODE>reverse()</CODE></STRONG></FONT></DT>
   
   <DD><FONT FACE="Arial">Reverse the elements of the list, in
   place.</FONT>
   
   <P><FONT FACE="Arial">&nbsp;</FONT></P></DD>
</DL>

<P><FONT FACE="Arial">An example that uses most of the list
methods:</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; a = &#91;66.6, 333, 333, 1, 1234.5&#93;
&gt;&gt;&gt; print a.count(333), a.count(66.6), a.count('x')
2 1 0
&gt;&gt;&gt; a.insert(2, -1)
&gt;&gt;&gt; a.append(333)
&gt;&gt;&gt; a
&#91;66.6, 333, -1, 333, 1, 1234.5, 333&#93;
&gt;&gt;&gt; a.index(333)
1
&gt;&gt;&gt; a.remove(333)
&gt;&gt;&gt; a
&#91;66.6, -1, 333, 1, 1234.5, 333&#93;
&gt;&gt;&gt; a.reverse()
&gt;&gt;&gt; a
&#91;333, 1234.5, 1, 333, -1, 66.6&#93;
&gt;&gt;&gt; a.sort()
&gt;&gt;&gt; a
&#91;-1, 1, 66.6, 333, 333, 1234.5&#93;
   </PRE></BLOCKQUOTE>

<P>&nbsp;</P>

<H3>

<HR>

<FONT FACE="Arial" COLOR="#000099">Using Lists as Stacks</FONT></H3>

<P><FONT FACE="Arial">The list methods make it very easy to use a
list as a stack, where the last element added is the first element
retrieved (``last-in, first-out''). To add an item to the top of the
stack, use <TT>append()</TT>. To retrieve an item from the top of the
stack, use <TT>pop()</TT> without an explicit index. For
example:</FONT></P>

<P><FONT FACE="Arial">&nbsp;</FONT></P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; stack = &#91;3, 4, 5&#93;
&gt;&gt;&gt; stack.append(6)
&gt;&gt;&gt; stack.append(7)
&gt;&gt;&gt; stack
&#91;3, 4, 5, 6, 7&#93;
&gt;&gt;&gt; stack.pop()
7
&gt;&gt;&gt; stack
&#91;3, 4, 5, 6&#93;
&gt;&gt;&gt; stack.pop()
6
&gt;&gt;&gt; stack.pop()
5
&gt;&gt;&gt; stack
&#91;3, 4&#93;
   </PRE></BLOCKQUOTE>

<P>&nbsp;</P>

<H3><FONT FACE="Arial" COLOR="#000099">Using Lists as
Queues</FONT></H3>

<P><FONT FACE="Arial">You can also use a list conveniently as a
queue, where the first element added is the first element retrieved
(``first-in, first-out''). To add an item to the back of the queue,
use <TT>append()</TT>. To retrieve an item from the front of the
queue, use <TT>pop()</TT> with <CODE>0</CODE> as the index. For
example:</FONT></P>

<P><FONT FACE="Arial">&nbsp;</FONT></P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; queue = &#91;"Eric", "John", "Michael"&#93;
&gt;&gt;&gt; queue.append("Terry")           # Terry arrives
&gt;&gt;&gt; queue.append("Graham")          # Graham arrives
&gt;&gt;&gt; queue.pop(0)
'Eric'
&gt;&gt;&gt; queue.pop(0)
'John'
&gt;&gt;&gt; queue
&#91;'Michael', 'Terry', 'Graham'&#93;
   </PRE></BLOCKQUOTE>

<P>&nbsp;</P>

<H3><FONT COLOR="#000099">

<HR>

</FONT><FONT FACE="Arial" COLOR="#000099">Functional Programming
Tools</FONT></H3>

<P><FONT FACE="Arial">There are three built-in functions that are
very useful when used with lists: <TT>filter()</TT>, <TT>map()</TT>,
and <TT>reduce()</TT>.</FONT></P>

<P><FONT FACE="Arial">"<TT>filter(<VAR>function</VAR>,
<VAR>sequence</VAR>)</TT>" returns a sequence (of the same type, if
possible) consisting of those items from the sequence for which
<CODE><VAR>function</VAR>(<VAR>item</VAR>)</CODE> is true. For
example, to compute some primes:</FONT></P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>
&gt;&gt;&gt; def f(x): return x % 2 != 0 and x % 3 != 0
...
&gt;&gt;&gt; filter(f, range(2, 25))
&#91;5, 7, 11, 13, 17, 19, 23&#93;
   </PRE></BLOCKQUOTE>

<P><FONT FACE="Arial">"<TT>map(<VAR>function</VAR>,
<VAR>sequence</VAR>)</TT>" calls
<CODE><VAR>function</VAR>(<VAR>item</VAR>)</CODE> for each of the
sequence's items and returns a list of the return values. For
example, to compute some cubes:</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; def cube(x): return x*x*x
...
&gt;&gt;&gt; map(cube, range(1, 11))
&#91;1, 8, 27, 64, 125, 216, 343, 512, 729, 1000&#93;
   </PRE></BLOCKQUOTE>

<P><FONT FACE="Arial">More than one sequence may be passed; the
function must then have as many arguments as there are sequences and
is called with the corresponding item from each sequence (or
<CODE>None</CODE> if some sequence is shorter than another). If
<CODE>None</CODE> is passed for the function, a function returning
its argument(s) is substituted.</FONT></P>

<P><FONT FACE="Arial">Combining these two special cases, we see that
"<TT>map(None, <VAR>list1</VAR>, <VAR>list2</VAR>)</TT>" is a
convenient way of turning a pair of lists into a list of pairs. For
example:</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; seq = range(8)
&gt;&gt;&gt; def square(x): return x*x
...
&gt;&gt;&gt; map(None, seq, map(square, seq))
&#91;(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)&#93;
   </PRE></BLOCKQUOTE>

<P><FONT FACE="Arial">"<TT>reduce(<VAR>func</VAR>,
<VAR>sequence</VAR>)</TT>" returns a single value constructed by
calling the binary function <VAR>func</VAR> on the first two items of
the sequence, then on the result and the next item, and so on. For
example, to compute the sum of the numbers 1 through 10:</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; def add(x,y): return x+y
...
&gt;&gt;&gt; reduce(add, range(1, 11))
55
   </PRE></BLOCKQUOTE>

<P><FONT FACE="Arial">If there's only one item in the sequence, its
value is returned; if the sequence is empty, an exception is
raised.</FONT></P>

<P><FONT FACE="Arial">A third argument can be passed to indicate the
starting value. In this case the starting value is returned for an
empty sequence, and the function is first applied to the starting
value and the first sequence item, then to the result and the next
item, and so on. For example,</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; def sum(seq):
...     def add(x,y): return x+y
...     return reduce(add, seq, 0)
... 
&gt;&gt;&gt; sum(range(1, 11))
55
&gt;&gt;&gt; sum(&#91;&#93;)
0
   </PRE></BLOCKQUOTE>

<P>&nbsp;</P>

<H3><FONT COLOR="#000099">

<HR>

</FONT><FONT FACE="Arial" COLOR="#000099">List
Comprehensions</FONT></H3>

<P><FONT FACE="Arial">List comprehensions provide a concise way to
create lists without resorting to use of <TT>map()</TT>,
<TT>filter()</TT> and/or <TT>lambda</TT>. The resulting list
definition tends often to be clearer than lists built using those
constructs. Each list comprehension consists of an expression
following by a <TT>for</TT> clause, then zero or more <TT>for</TT> or
<TT>if</TT> clauses. The result will be a list resulting from
evaluating the expression in the context of the <TT>for</TT> and
<TT>if</TT> clauses which follow it. If the expression would evaluate
to a tuple, it must be parenthesized.</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; freshfruit = &#91;'  banana', '  loganberry ', 'passion fruit  '&#93;
&gt;&gt;&gt; &#91;weapon.strip() for weapon in freshfruit&#93;
&#91;'banana', 'loganberry', 'passion fruit'&#93;
&gt;&gt;&gt; vec = &#91;2, 4, 6&#93;
&gt;&gt;&gt; &#91;3*x for x in vec&#93;
&#91;6, 12, 18&#93;
&gt;&gt;&gt; &#91;3*x for x in vec if x &gt; 3&#93;
&#91;12, 18&#93;
&gt;&gt;&gt; &#91;3*x for x in vec if x &lt; 2&#93;
&#91;&#93;
&gt;&gt;&gt; &#91;{x: x**2} for x in vec&#93;
&#91;{2: 4}, {4: 16}, {6: 36}&#93;
&gt;&gt;&gt; &#91;&#91;x,x**2&#93; for x in vec&#93;
&#91;&#91;2, 4&#93;, &#91;4, 16&#93;, &#91;6, 36&#93;&#93;
&gt;&gt;&gt; &#91;x, x**2 for x in vec&#93;	# error - parens required for tuples
  File "&lt;stdin&gt;", line 1, in ?
    &#91;x, x**2 for x in vec&#93;
               ^
SyntaxError: invalid syntax
&gt;&gt;&gt; &#91;(x, x**2) for x in vec&#93;
&#91;(2, 4), (4, 16), (6, 36)&#93;
&gt;&gt;&gt; vec1 = &#91;2, 4, 6&#93;
&gt;&gt;&gt; vec2 = &#91;4, 3, -9&#93;
&gt;&gt;&gt; &#91;x*y for x in vec1 for y in vec2&#93;
&#91;8, 6, -18, 16, 12, -36, 24, 18, -54&#93;
&gt;&gt;&gt; &#91;x+y for x in vec1 for y in vec2&#93;
&#91;6, 5, -7, 8, 7, -5, 10, 9, -3&#93;
&gt;&gt;&gt; &#91;vec1&#91;i&#93;*vec2&#91;i&#93; for i in range(len(vec1))&#93;
&#91;8, 12, -54&#93;
   </PRE></BLOCKQUOTE>

<P>&nbsp;</P>

<H3>

<HR>

<FONT FACE="Arial" COLOR="#000099"><TT>del</TT></FONT></H3>

<P><FONT FACE="Arial">There is a way to remove an item from a list
given its index instead of its value: the <TT>del</TT> statement.
This can also be used to remove slices from a list (which we did
earlier by assignment of an empty list to the slice). For
example:</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; a
&#91;-1, 1, 66.6, 333, 333, 1234.5&#93;
&gt;&gt;&gt; del a&#91;0&#93;
&gt;&gt;&gt; a
&#91;1, 66.6, 333, 333, 1234.5&#93;
&gt;&gt;&gt; del a&#91;2:4&#93;
&gt;&gt;&gt; a
&#91;1, 66.6, 1234.5&#93;
   </PRE></BLOCKQUOTE>

<P><FONT FACE="Arial"><TT>del</TT> can also be used to delete entire
variables:</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; del a
   </PRE></BLOCKQUOTE>

<P><FONT FACE="Arial">Referencing the name <CODE>a</CODE> hereafter
is an error (at least until another value is assigned to it). We'll
find other uses for <TT>del</TT> later.</FONT></P>

<P><FONT FACE="Arial">&nbsp;</FONT></P>

<H3><FONT FACE="Arial">

<HR>

</FONT><FONT FACE="Arial" COLOR="#000099">Tuples and
Sequences</FONT></H3>

<P><FONT FACE="Arial">We saw that lists and strings have many common
properties, such as indexing and slicing operations. They are two
examples of <I>sequence</I> data types. Since Python is an evolving
language, other sequence data types may be added. There is also
another standard sequence data type: the <I>tuple</I>.</FONT></P>

<P><FONT FACE="Arial">A tuple consists of a number of values
separated by commas, for instance:</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; t = 12345, 54321, 'hello!'
&gt;&gt;&gt; t&#91;0&#93;
12345
&gt;&gt;&gt; t
(12345, 54321, 'hello!')
&gt;&gt;&gt; # Tuples may be nested:
... u = t, (1, 2, 3, 4, 5)
&gt;&gt;&gt; u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
   </PRE></BLOCKQUOTE>

<P><FONT FACE="Arial">As you see, on output tuples are alway 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).</FONT></P>

<P><FONT FACE="Arial">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.</FONT></P>

<P><FONT FACE="Arial">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 by an empty pair of
parentheses; a tuple with one item is constructed by following a
value with a comma (it is not sufficient to enclose a single value in
parentheses). Ugly, but effective. For example:</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; empty = ()
&gt;&gt;&gt; singleton = 'hello',    # &lt;-- note trailing comma
&gt;&gt;&gt; len(empty)
0
&gt;&gt;&gt; len(singleton)
1
&gt;&gt;&gt; singleton
('hello',)
   </PRE></BLOCKQUOTE>

<P><FONT FACE="Arial">The statement <CODE>t = 12345, 54321,
'hello!'</CODE> is an example of <I>tuple packing</I>: the values
<CODE>12345</CODE>, <CODE>54321</CODE> and <CODE>'hello!'</CODE> are
packed together in a tuple. The reverse operation is also
possible:</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; x, y, z = t
   </PRE></BLOCKQUOTE>

<P><FONT FACE="Arial">This is called, appropriately enough,
<I>sequence unpacking</I>. Sequence unpacking requires that the list
of variables on the left have the same number of elements as the
length of the sequence. Note that multiple assignment is really just
a combination of tuple packing and sequence unpacking!</FONT></P>

<P><FONT FACE="Arial">There is a small bit of asymmetry here: packing
multiple values always creates a tuple, and unpacking works for any
sequence.</FONT></P>

<P>&nbsp;</P>

<H3>

<HR>

<FONT FACE="Arial" COLOR="#000099">Dictionaries</FONT></H3>

<P><FONT FACE="Arial">Another useful data type built into Python is
the <I>dictionary</I>. Dictionaries are sometimes found in other
languages as ``associative memories'' or ``associative arrays''.
Unlike sequences, which are indexed by a range of numbers,
dictionaries are indexed by <I>keys</I>, which can be any immutable
type; strings and numbers can always be keys. Tuples can be used as
keys if they contain only strings, numbers, or tuples; if a tuple
contains any mutable object either directly or indirectly, it cannot
be used as a key. You can't use lists as keys, since lists can be
modified in place using their <TT>append()</TT> and <TT>extend()</TT>
methods, as well as slice and indexed assignments.</FONT></P>

<P><FONT FACE="Arial">It is best to think of a dictionary as an
unordered set of <I>key: value</I> pairs, with the requirement that
the keys are unique (within one dictionary). A pair of braces creates
an empty dictionary: <CODE>{}</CODE>. Placing a comma-separated list
of key:value pairs within the braces adds initial key:value pairs to
the dictionary; this is also the way dictionaries are written on
output.</FONT></P>

<P><FONT FACE="Arial">The main operations on a dictionary are storing
a value with some key and extracting the value given the key. It is
also possible to delete a key:value pair with <CODE>del</CODE>. If
you store using a key that is already in use, the old value
associated with that key is forgotten. It is an error to extract a
value using a non-existent key.</FONT></P>

<P><FONT FACE="Arial">The <CODE>keys()</CODE> method of a dictionary
object returns a list of all the keys used in the dictionary, in
random order (if you want it sorted, just apply the
<CODE>sort()</CODE> method to the list of keys). To check whether a
single key is in the dictionary, use the <CODE>has_key()</CODE>
method of the dictionary.</FONT></P>

<P><FONT FACE="Arial">Here is a small example using a
dictionary:</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; tel = {'jack': 4098, 'sape': 4139}
&gt;&gt;&gt; tel&#91;'guido'&#93; = 4127
&gt;&gt;&gt; tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
&gt;&gt;&gt; tel&#91;'jack'&#93;
4098
&gt;&gt;&gt; del tel&#91;'sape'&#93;
&gt;&gt;&gt; tel&#91;'irv'&#93; = 4127
&gt;&gt;&gt; tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
&gt;&gt;&gt; tel.keys()
&#91;'guido', 'irv', 'jack'&#93;
&gt;&gt;&gt; tel.has_key('guido')
1
   </PRE></BLOCKQUOTE>

<P>&nbsp;</P>

<H3>

<HR>

<FONT FACE="Arial">More on Conditions</FONT></H3>

<P><FONT FACE="Arial">The conditions used in <CODE>while</CODE> and
<CODE>if</CODE> statements above can contain other operators besides
comparisons.</FONT></P>

<P><FONT FACE="Arial">The comparison operators <CODE>in</CODE> and
<CODE>not in</CODE> check whether a value occurs (does not occur) in
a sequence. The operators <CODE>is</CODE> and <CODE>is not</CODE>
compare whether two objects are really the same object; this only
matters for mutable objects like lists. All comparison operators have
the same priority, which is lower than that of all numerical
operators.</FONT></P>

<P><FONT FACE="Arial">Comparisons can be chained. For example,
<CODE>a &lt; b == c</CODE> tests whether <CODE>a</CODE> is less than
<CODE>b</CODE> and moreover <CODE>b</CODE> equals
<CODE>c</CODE>.</FONT></P>

<P><FONT FACE="Arial">Comparisons may be combined by the Boolean
operators <CODE>and</CODE> and <CODE>or</CODE>, and the outcome of a
comparison (or of any other Boolean expression) may be negated with
<CODE>not</CODE>. These all have lower priorities than comparison
operators again; between them, <CODE>not</CODE> has the highest
priority, and <CODE>or</CODE> the lowest, so that <CODE>A and not B
or C</CODE> is equivalent to <CODE>(A and (not B)) or C</CODE>. Of
course, parentheses can be used to express the desired
composition.</FONT></P>

<P><FONT FACE="Arial">The Boolean operators <CODE>and</CODE> and
<CODE>or</CODE> are so-called <I>shortcut</I> operators: their
arguments are evaluated from left to right, and evaluation stops as
soon as the outcome is determined. E.g., if <CODE>A</CODE> and
<CODE>C</CODE> are true but <CODE>B</CODE> is false, <CODE>A and B
and C</CODE> does not evaluate the expression C. In general, the
return value of a shortcut operator, when used as a general value and
not as a Boolean, is the last evaluated argument.</FONT></P>

<P><FONT FACE="Arial">It is possible to assign the result of a
comparison or other Boolean expression to a variable. For
example,</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>&gt;&gt;&gt; string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
&gt;&gt;&gt; non_null = string1 or string2 or string3
&gt;&gt;&gt; non_null
'Trondheim'
   </PRE></BLOCKQUOTE>

<P><FONT FACE="Arial">Note that in Python, unlike C, assignment
cannot occur inside expressions. C programmers may grumble about
this, but it avoids a common class of problems encountered in C
programs: typing <CODE>=</CODE> in an expression when <CODE>==</CODE>
was intended.</FONT></P>

<P><FONT FACE="Arial">&nbsp;</FONT></P>

<H3><FONT FACE="Arial" COLOR="#000099">Comparing Sequences and Other
Types</FONT></H3>

<P><FONT FACE="Arial">Sequence objects may be compared to other
objects with the same sequence type. The comparison uses
<I>lexicographical</I> ordering: first the first two items are
compared, and if they differ this determines the outcome of the
comparison; if they are equal, the next two items are compared, and
so on, until either sequence is exhausted. If two items to be
compared are themselves sequences of the same type, the
lexicographical comparison is carried out recursively. If all items
of two sequences compare equal, the sequences are considered equal.
If one sequence is an initial sub-sequence of the other, the shorter
sequence is the smaller (lesser) one. Lexicographical ordering for
strings uses the ASCII ordering for individual characters. Some
examples of comparisons between sequences with the same
types:</FONT></P>

<P>&nbsp;</P>

<BLOCKQUOTE class=verbatim><PRE class=verbatim>(1, 2, 3)              &lt; (1, 2, 4)
&#91;1, 2, 3&#93;              &lt; &#91;1, 2, 4&#93;
'ABC' &lt; 'C' &lt; 'Pascal' &lt; 'Python'
(1, 2, 3, 4)           &lt; (1, 2, 4)
(1, 2)                 &lt; (1, 2, -1)
(1, 2, 3)             == (1.0, 2.0, 3.0)
(1, 2, ('aa', 'ab'))   &lt; (1, 2, ('abc', 'a'), 4)
   </PRE></BLOCKQUOTE>

<P><FONT FACE="Arial">Note that comparing objects of different types
is legal. The outcome is deterministic but arbitrary: the types are
ordered by their name. Thus, a list is always smaller than a string,
a string is always smaller than a tuple, etc. Mixed numeric types are
compared according to their numeric value, so 0 equals 0.0,
etc</FONT></P>

<H2>&nbsp;</H2>

<H2><FONT FACE="Arial" COLOR="#0033FF">

<HR>

What's Next?</FONT></H2>

<P><FONT FACE="Arial">Choose one of the other chapters in the
Scripting Guide:</FONT></P>

<OL>
   <LI><A HREF="#Data"><FONT FACE="Arial">DataTypes</FONT></A></LI>
   
   <LI><A HREF="flow_control.html"><FONT FACE="Arial">Flow Control
   and Functions</FONT></A></LI>
   
   <LI><A HREF="data_structures.html"><FONT FACE="Arial">Data
   Structures</FONT></A></LI>
   
   <LI><A HREF="modules.html"><FONT FACE="Arial">Modules</FONT></A></LI>
   
   <LI><A HREF="input_output.html"><FONT FACE="Arial">Input and
   Output</FONT></A></LI>
   
   <LI><A HREF="exceptions.html"><FONT FACE="Arial">Errors and
   Exceptions</FONT></A></LI>
   
   <LI><A HREF="classes.html"><FONT FACE="Arial">Classes</FONT></A></LI>
   
   <LI><A HREF="floating_point.html"><FONT FACE="Arial">Floating
   Point Numbers</FONT></A></LI>
</OL>

<PRE>
<FONT FACE="Arial"> </FONT></PRE>

<P><FONT FACE="Arial"><TABLE BORDER=0 BGCOLOR="#0000CC" CELLSPACING=1 CELLPADDING=0 WIDTH="100%">
   <TR>
      <TD>
         <P><FONT FACE="Arial"><TABLE BORDER=0 BGCOLOR="#FFFFCC" CELLPADDING=0 WIDTH="100%">
            <TR>
               <TD WIDTH=10>
                  <P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=10 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
               </TD>
               <TD WIDTH=120>
                  <P><A HREF="http://www.pushtotest.com"><FONT FACE="Arial"><IMG SRC="../images/ptt_logo_120w.gif" WIDTH=120 HEIGHT=32 X-CLARIS-USEIMAGEWIDTH X-CLARIS-USEIMAGEHEIGHT BORDER=0 ALIGN=bottom></FONT></A></P>
               </TD>
               <TD WIDTH=30>
                  <P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=30 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
               </TD>
               <TD>
                  <P><FONT SIZE="-1" FACE="Arial">Additional
                  documentation, product downloads and updates are at
                  </FONT><A HREF="http://www.pushtotest.com"><FONT SIZE="-1" FACE="Arial">www.PushToTest.com</FONT></A><FONT SIZE="-1" FACE="Arial">.
                  While the TestMaker software is distributed under
                  an open-source license, the documentation remains
                  (c) 2002 PushToTest. All rights
                  reserved.</FONT></P>
               </TD>
            </TR>
         </TABLE>
          </FONT></P>
      </TD>
   </TR>
</TABLE>
 </FONT></P>
</BODY>
</HTML>