[newbie] troubles with tuples

Asaf Las roegltd at gmail.com
Mon Feb 3 17:59:50 CET 2014


On Monday, February 3, 2014 6:50:31 PM UTC+2, Jean Dupont wrote:
> I'm looking at the way to address tuples
> 
> e.g.
> tup2 = (1, 2, 3, 4, 5, 6, 7 );
> As I found out indices start with 0 in Python, so 
> tup2[0] gives me 1, the first element in the tuple as expected
> tup2[1] gives me 2, the second element in the tuple as expected
> now here comes what surprises me:
> tup2[0:1] does not give me the expected (1,2) but (2,)
> what is the reason for this and how then should one get the first and the second element of a tuple? Or the 3rd until the 5th?
> 
> thanks in advance and kind regards,
> 
> jean

Hi

from http://docs.python.org/3.3/library/stdtypes.html?highlight=tuple#tuple

" The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j. If i or j is greater than len(s), use len(s). If i is omitted or None, use 0. If j is omitted or None, use len(s). If i is greater than or equal to j, the slice is empty."

so in above  k < j but not equal so in your example slice will be of only one member. 

/Asaf




More information about the Python-list mailing list