[Tutor] array

Liam Clarke cyresse at gmail.com
Sun Oct 31 22:48:16 CET 2004


Yeah, the terminology is confusing. 'Array' tends to be a hangover
from the days of BASIC.

An array in the days of BASIC could be 1 dimensional or two
dimensional (Or 3 if you were really keen)

A 1 dimensional array would be something along these lines:

      Index #  0   1  2   3  4   5 6 7 8 9... 
a = Value   'a'   'b' 'c' 'd'  55....

so a(0) would be 'a', a(4) would be 55.


2 dimensional arrays were like this - 

a ={
    0  1 2  3  4  5  6 7...
0 'a' 'b' c' 'd' 55
1
2
3
4
}

and a(0,0) would be 'a', and a(0,4) would be 55.

Python doesn't do two dimensional arrays out of the box, so forget about those.

An 1 dimensional array in Python is called a list. 

A list is denoted by a group of values surrounded by [ ] brackets.
a=['a','b','c','d',55]

A list is accessed by index number, same as an array.

a[0]='a', a[4]=55.

(Sidenote, a list can also contain sublists, which isn't really
relevant to your current problem, but keep it in mind, as it will come
in useful for future Python work.

A=['a',['b','c','d'],55] ... A[0]='a', A[1]=['b','c','d'] , A[2]=55.
A[1][0]='b', A[1][2]='d')

So to conclude, 4 basic hints for you - 

1. An array in Python is a list.
2. Computers start counting at 0....
3. ...which is why a 'for x in range(y)'  statement counts up to, but
not including y. I.E., if y was 10, it would count 0-9. It works like
this for a reason, which you'll understand the more you work with
list.

4. Lastly, and I can't stress how useful this site is,
http://www.freenetpages.co.uk/hp/alan.gauld/tutor2/index.htm
This is one of the best tutorials I found. I only started using Python
4 weeks ago, I worked through Alan Gauld's above tutorial, and yeah, I
just finished creating my first GUI based programme.

Good luck,

Liam Clarke
On Sun, 31 Oct 2004 19:31:06 +0100, tanja pislar <tanja.pislar at gmail.com> wrote:
> maybe just a hint: try and read how to use lists in Python.. that will
> give you everything you'll need for your assignment
> 
> some links:
> http://www.developer.com/lang/other/article.php/628971
> http://diveintopython.org/native_data_types/lists.html
> 
> 
> 
> 
> On Sun, 31 Oct 2004 12:31:34 -0500, Diana Furr <dleigh0 at carolina.rr.com> wrote:
> >
> > I am taking a programming class that uses the book Programming Logic and
> > Design and we program with python. We are starting on a chapter about arrays
> > and I have a few questions. I understand from reading the chapter what an
> > array is and why they are used, but I don't know how to make an array in
> > python. I have read from different links on python.org but I still don't get
> > it.
> >
> > This is the assignment:
> >
> > Write a Python code for a program that reads numbers entered from the
> > keyboard into an array1, stores their doubles in a second array2, and prints
> > out both arrays. For the purpose of this assignment assume that there will
> > be no more than 10 numbers. Have the program prompt at the beginning of the
> > program and ask how many numbers will be entered. The program should loop
> > and ask if another set of numbers will be entered, program termination is
> > dependent on user input. Suggested output is shown below:
> >
> > Array 1    Array2
> >
> > 10            20
> >
> > 11            22
> >
> > 15            30
> >
> > 33            66
> >
> > I don't want someone to give me the answer, but maybe show me what an array
> > that is dependant on user input looks like in Python. I understand if no one
> > wants to help but please at least point me in the right direction.
> >
> > Thank you,
> >
> > Diana
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
> 
> 
> --
> www.klaustrofobik.org
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list