[Tutor] Help on Arrays?

Kirby Urner urnerk@qwest.net
Fri, 19 Oct 2001 16:28:54 -0700


At 06:03 PM 10/19/2001 -0400, Roger G. Lewis wrote:
>Any reference to tutorial/examples on creation and use of *Arrays* in Python
>would be appreciated by this newbie. Have used them in BASIC so concepts are
>understood but details are not. Thanks.
>
>R. Lewis
>Ottawa, ON. Canada

Note also that in addition to lists, which are arrays
allowing any mix of objects, you have an 'array' module
which lets your create arrays all of the same kind of
thing.

   >>> from array import array
   >>> a = array('i',[1,2,3])
   >>> a
   array('i', [1, 2, 3])
   >>> a[0]
   1
   >>> type(a)
   <type 'array'>

They behave pretty much the same as lists, only faster.
If you have a huge size of array, and the members are
all integers, characters, floats or whatever, consider
the array type.

Also, if you augment Python with Numeric Python, an
optional package for handling large arrays efficiently,
you'll find yet another implementation of the array
concept.

In ordinary Python, using builtin features only, note
that you have two kinds of "arrays":  lists and tuples.
The main difference is that tuples aren't designed to
be appended to or to have their members changed --
they're what we call 'immutable'.  That can be useful,
if you're trying to prevent data from being overwritten
by mistake.

Many functions return tuples, vs. single values or
even lists.  From example, as part of the Standard
Library you have the 'time' module:

   >>> import time
   >>> time.localtime()
   (2001, 10, 19, 16, 23, 25, 4, 292, 1)

You can guess from the entries what these data give you.

Note:  if you want to format this tuple, the time module
has options for this e.g.:

   >>> time.strftime("%a, %d %b %Y",time.localtime())

(the 2nd argument is optional if all you want is indeed
the local time).

Kirby