[newbie] troubles with tuples

2014-02-03 Thread Jean Dupont
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

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] troubles with tuples

2014-02-03 Thread Larry Martell
On Mon, Feb 3, 2014 at 11:50 AM, Jean Dupont jeandupont...@gmail.com 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,

Some examples:

a[start:end]  # items start through end-1
a[start:]   # items start through the rest of the array
a[:end]# items from the beginning through end-1
a[:] # a copy of the whole array
a[-1]   # last item in the array
a[-2:]  # last two items in the array
a[:-2]  # everything except the last two items

HTH,
-larry
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] troubles with tuples

2014-02-03 Thread Asaf Las
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

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] troubles with tuples

2014-02-03 Thread Rustom Mody
On Monday, February 3, 2014 10:20:31 PM UTC+5:30, 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,)


Python 2.7.6 (default, Jan 11 2014, 17:06:02) 
[GCC 4.8.2] on linux2
Type help, copyright, credits or license for more information.
 tup2=(1,2,3,4,5,6,7)
 tup2[0:1]
(1,)
 

So assuming you meant (1,) and wrote (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?

Generally ranges in python are lower-inclusive upper-exclusive
What some math texts write as [lo, hi)

So if you want from index 1 to 2-inclusive it is 1 to 3 exclusive
tup2[0:2]


See for motivations
http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html

And one more surprising thing to note is that negatives count from the end


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] troubles with tuples

2014-02-03 Thread Jean Dupont
Op maandag 3 februari 2014 18:06:46 UTC+1 schreef Rustom Mody:
 On Monday, February 3, 2014 10:20:31 PM UTC+5:30, 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,)

 Python 2.7.6 (default, Jan 11 2014, 17:06:02) 
 [GCC 4.8.2] on linux2
 Type help, copyright, credits or license for more information.
  tup2=(1,2,3,4,5,6,7)
  tup2[0:1]
 (1,)
  
 So assuming you meant (1,) and wrote (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?
 Generally ranges in python are lower-inclusive upper-exclusive
 What some math texts write as [lo, hi)
 So if you want from index 1 to 2-inclusive it is 1 to 3 exclusive
 tup2[0:2]

 See for motivations
 http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html
 And one more surprising thing to note is that negatives count from the end
Thank you (and the others) for making this logical

kind regards,
jean
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] troubles with tuples

2014-02-03 Thread Terry Reedy

On 2/3/2014 11:50 AM, 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,


This should be covered in the tutorial, which you should read if you 
have not already.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] troubles with tuples

2014-02-03 Thread Denis McMahon
On Mon, 03 Feb 2014 08:50:31 -0800, 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

 tup = (0,1,2,3,4,5,6,7)
 tup[0:1]
(0,)
 tup[1:2]
(1,)
 tup[2:3]
(2,)
 tup[0:2]
(0, 1)
 tup[2:5]
(2, 3, 4)

This is as I'd expect, as the notation [0:1] means:

starting from the 0th element, up to and including the element preceding 
the first element

Or [m:n] means starting from the mth element, everything up to and 
including the element preceding the nth element

Are you sure you got (2,) for [0:1] and not for [2:3]? Are you sure your 
initial tuple is what you thought it was?

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list