[Edu-sig] Re: In defense of 0, was Re: None potato, one potato, two potato, more..

Kirby Urner urnerk at qwest.net
Mon Jan 5 12:55:28 EST 2004


> > > [2] would indicate the item sitting 2cm from the beginning (c)
> > >
> > > [0:3] would indicate the items from the beginning (0) to 3 cm from the
> > > beginning (i.e. a,b,c)
> > >
> >
> > From 0 to 2 cm from the beginning, no?
> >
> 
> No, though 'c' lies 2cm from the beginning, it does not fall between 0
> and 2cm because if you stop at 2cm you haven't yet passed it (since
> we're imagining each item being 1cm wide and it lies between 2cm and
> 3cm).
> 
> 0 a 1 b 2 c 3 d 4 e 5
> 

I think of it somewhat differently then.  I think of [0:3] as similar to
range(0,3) which means [0,1,2] i.e. the 3 is not included.

If we go  [mylist(i) for i in range(0,3)], we get the same as mylist[0:3].

With the negative indexes, I think of len(mylist) as an implicit argument
i.e. if you go:

  >>> mylist = ['a','b','c','d']

then mylist[len(mylist)] is out of range, because len() returns the actual
number of members, whereas indexing is 0-based.

However, mylist[len(mylist) - 1] would get you the last element, and so I
see mylist[-1] as shorthand for that.

mylist[:-1] is likewise non-inclusive of the last element, is similar to
[mylist[i] for i in range(0,len(mylist)-1)]

Kirby





More information about the Edu-sig mailing list