[Tutor] Thanks re: [::-1]

Chris Calloway cbc at unc.edu
Thu Jul 26 21:18:23 CEST 2007


Charles Cuell wrote:
> The one odd thing about Python's slice notation is that the -1 means to
> start from the end and work backwards.  My first inclination would have
> been to assume that -1 means to start at i and go to j by steps of -1
> (only nonempy if j < i).

A negative step attribute does not change the semantics of the start and 
stop (read only) attributes of slice objects:

 >>> m = range(10)
 >>> m[2:7:-1]
[]
 >>> m[7:2:-1]
[7, 6, 5, 4, 3]
 >>> m[-3:-8:-1]
[7, 6, 5, 4, 3]
 >>>

So your first inclination was correct! :)

i does go to j by steps of k.

-- 
Sincerely,

Chris Calloway
http://www.seacoos.org
office: 332 Chapman Hall   phone: (919) 962-4323
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599





More information about the Tutor mailing list