[Edu-sig] beginner trouble, indexing starting w/"0"
Thu, 29 Jun 2000 19:45:33 -0600
* Matthew J. Moelter <email@example.com>:
> I have been following the Python edu-sig list for a while and
> in the spirit of observing a "beginner" I decided to show my
> dad some Python. (He is a long-ago-retired high school math teacher
> with virtually no programming experience.)
> Below is how far our session got before we had "trouble":
> Python 1.6a2 (#54, May 6 2000, 00:26:29) [CW PPC w/GUSI2 w/THREADS]
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> s='Matthew'
> >>> print s
> >>> s
> >>> len(s)
> >>> s
> >>> s
> While he understood the idea of indexing fine he didn't
> understand the first element being indexed with "0".
It helped me to visualize it like this:
0 1 2 3 4 5 6 7
s[1:3] makes a slice at the numbers, returning 'at'. For s it does the
same, just with one item. Also s[-1], which returns 'w'.
> 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?
What if it started at 1?
1 2 3 4 5 6 7 8
-1 0 1
Now, this is even more confusing when trying to use negative indexes. s
returns 'M', but s returns 'w', and s[-1] returns 'e'. That is even more
confusing to me.
While it is confusing at first, once it is visualized as slices and indexes,
as above, it seems to me to be the best approach.