[Edu-sig] beginner trouble, indexing starting w/"0"

Dustin James Mitchell djmitche@cs.uchicago.edu
Fri, 30 Jun 2000 09:16:34 -0500 (CDT)


On Thu, 29 Jun 2000, Matthew J. Moelter wrote:

> While he understood the idea of indexing fine he didn't
> understand the first element being indexed with "0".
> When he taught vectors or matrices to the students they usually
> indexed from 1 to N (vectors), or 1 to N by 1 to M (matrices).
> The upper left element in a matrix was A(1,1) or something.
> 
> If this is truly the way Python is configured it seems to me
> that this could be an important stumbling block.
> I do not know enough about Python myself to see if this is a
> necessary approach or if it can/could be changed.  Or could it be
> changed at the users option? "starting index=1" or something?

This is a common problem for those on the border between mathematics and
computers.  The reason that it's done this way is due to the way computers
index things internally.  If we want to start indexing at 1 (or (1,1)),
then the processor must do some extra work on every array indexing
operation, which slows things down.

It's possible to change this when you're writing your own class, e.g.

class Matrix:
	...
	def get(self,i,j):
		return self.data[i-1][j-1]

or the like.

I like your suggestion of making it a user-settable flag (perhaps a
command-line switch as well?).  Anyone else feel strongly on this issue of
modifying Python?

Dustin

---------------------------------------------------------------------
|                         Dustin Mitchell                )O(        |
---------------------------------------------------------------------