Python + strange == cool

Dave Brueck dave at pythonapocrypha.com
Tue Sep 28 19:35:19 EDT 2004


Maboroshi wrote:
> All fine and good <below>
> 
>  >>> x = ["list1", "list2", "list3", 4, 5, 6, 7]
>  >>> x[1:-1]
> ['list2', 'list3', 4, 5, 6] # All good here
> 
> this struck me as weird I had an idea to mess around with lists <below>
> 
>  >>> x[1:+1]
> []
> 
> now when I do this
>  >>> x[1:+2]
> ['list2']
> 
> Does this puzzle anyone else
> 
> of course I am a total novice so I have no idea what I am doing here if 
> anyone has any ideas please reply

Look at section 3.1.2 of the Python tutorial (look at the whole tutorial if you 
haven't already ;-) ). The part explaining the above starts like this:

"The best way to remember how slices work is to think of the indices as pointing 
between characters..."

(note that 3.1.2 is talking about strings, but lists, tuples, and strings are 
all sequences and share many properties. Also, working with strings might make 
it easier to grasp what's going on, and once you've got those down then applying 
the principles to lists and tuples will be a breeze)

-Dave




More information about the Python-list mailing list