Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65368

Re: [newbie] troubles with tuples

Newsgroups comp.lang.python
Date 2014-02-03 11:18 -0800
References <893ced1a-dd55-48d7-8849-b2e45da2740e@googlegroups.com> <6214e086-7616-41bc-8e82-665b01dddd25@googlegroups.com>
Message-ID <c058fc2e-7c0f-492f-a2fb-aa9941181930@googlegroups.com> (permalink)
Subject Re: [newbie] troubles with tuples
From Jean Dupont <jeandupont314@gmail.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[newbie] troubles with tuples Jean Dupont <jeandupont314@gmail.com> - 2014-02-03 08:50 -0800
  Re: [newbie] troubles with tuples Asaf Las <roegltd@gmail.com> - 2014-02-03 08:59 -0800
  Re: [newbie] troubles with tuples Larry Martell <larry.martell@gmail.com> - 2014-02-03 12:00 -0500
  Re: [newbie] troubles with tuples Rustom Mody <rustompmody@gmail.com> - 2014-02-03 09:06 -0800
    Re: [newbie] troubles with tuples Jean Dupont <jeandupont314@gmail.com> - 2014-02-03 11:18 -0800
  Re: [newbie] troubles with tuples Terry Reedy <tjreedy@udel.edu> - 2014-02-03 16:52 -0500
  Re: [newbie] troubles with tuples Denis McMahon <denismfmcmahon@gmail.com> - 2014-02-04 00:50 +0000

csiph-web