[sac-forum] Paul's Programming 3

The last message got away before I could spell-check or proofread, sorry.

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.


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 on lists.

[I actually wrote the above before Stan's message.]

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 element of that string.

When I finally get around to functions, you may 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.  In fact, it's also possible to pass sequences of
undetermined length using iterators or generators (while I might touch on
these in far future messages, you'll get far more details from the book I
mentioned or the on-line documentation).

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 links 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 also works with lists.  No more needing a third variable to swap
values.

        -Paul

Other related posts: