Paul's Python Intro 03

Paul Dickson plug-devel@lists.PLUG.phoenix.az.us
Thu Sep 9 13:43:01 2004


Quick Interlude: Python has several numeric data types: integer, floating
point, complex, and unlimited precision long integers.  The floating point
are equivalent to doubles in C.  Python 2.4 will have a new Decimal type,
similar to the unlimited precision "long", but for floating point math.


One of the oldest computer languages is Lisp.  I recently saw a time-line
(http://www.levenez.com/lang/) that shows that it is 46 years old.  It's
based on lists.  Everything is a list in Lisp, even the program.  The name
is from the contraction of "LISt Processing".  Lisp is a very advanced,
high-level language, especially so considering it's based just on lists.


Python doesn't go nearly as far with lists.  In Python, lists have the
ease of use of arrays and the flexibility of a linked-list.  Lists use the
same indexing and slicing that are used with strings.

    >>> a='help'
    >>> b='Testing'
    >>> [a,b,3]
    ['help', 'Testing', 3]	# 2 strings and an integer
    >>> c=[a,b,3]
    >>> c[0]
    'help'			# First element of list
    >>> c[1:]
    ['Testing', 3]		# Other elements of list
    >>> c[1][0]
    'T'				# First character of the second element

The last example, c[1] gets the second item (a string) in the list and the
[0] gets the first character of that string.

When I finally get around to functions, you may find a need to return more
than one value.  Usually in C, this means having to create a structure,
allocating memory for it, passing a pointer to the structure back to the
caller, and the caller having to unallocate the now unused structure.  In
Python, you can now just enclose these values in a list (or tuple) and
return that.


While the indexing of lists is the same as strings, they have one major
difference:  strings can't be changed.  You can make copies of strings,
replace strings with new strings, but you can't change a string.  This is
what you'd get with just about any other language, including BASIC:

    >>> str1="hello"
    >>> str2=str1
    >>> str1="hi"
    >>> print str1, str2
    hi hello

Lists are different:

    >>> c
    ['help', 'Testing', 3]
    >>> d = c
    >>> d
    ['help', 'Testing', 3]
    >>> c[1] = "untested"
    >>> c
    ['help', 'untested', 3]
    >>> d
    ['help', 'untested', 3]

In computer science jargon, lists are mutable (changeable) while strings
are immutable.

Python has a immutable version of lists called tuples.  They are similar
to lists in every other way, they just can't be changed.

    >>> c=("a", b, 5)
    >>> c
    ('a', 'Testing', 5)
    >>> d = c
    >>> print d
    ('a', 'Testing', 5)
    >>> c[1]="untested"
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: object doesn't support item assignment

Using help(list) will list other functions you can perform on lists (try
help(tuple) too).  Try using addition (+) on strings (str1+str2) and lists
(list1+list2).


Before I end this message, I wanted to show a neat trick.  Often you'll
want to swap two values.  This is called positional assignment:

    >>> (yes, no) = (True, False)
    >>> print yes, no
    True False
    >>> (no, yes) = (yes, no)
    >>> print yes, no
    False True

The same assignment also works with lists, or even without the parenthesis
or brackets.  No more needing a third variable to swap two values.

True and False are boolean values introduced in Python 2.3.  

	-Paul