odd

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

odd

elis aeris
I ran this


for x in range(5,10):
    print x




and OP was

5
6
7
8
9



why is that? shouldn't it print


t
6
7
8
9
10?

_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: odd

wesley chun
> for x in range(5,10):
>     print x
>
> and OP was
>
> 5
> 6
> 7
> 8
> 9
>
> why is that? shouldn't it print
>
> t
> 6
> 7
> 8
> 9
> 10?


no.  the (well, one) syntax for range() is (start, stop) where it
counts starting from 'start' up to but not including 'stop'.  if
you're familiar with C/C++ (or PHP or Java), it's similar to the
counting loop, "for (int i=5; i < 10; i++)", which counts 5, 6, 7, 8,
9.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: odd

David Rock
In reply to this post by elis aeris
* elis aeris <[hidden email]> [2007-07-19 08:51]:

> I ran this
>
>
> for x in range(5,10):
>    print x
>
> and OP was
>
> 5
> 6
> 7
> 8
> 9
>
> why is that? shouldn't it print
>
> 5
> 6
> 7
> 8
> 9
> 10?

That is the expected behaviour, per the documentation:
http://docs.python.org/lib/built-in-funcs.html#l2h-58

--
David Rock
[hidden email]
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: odd

Ian Witham
In reply to this post by elis aeris
from Guido's tutorial:

The given end point is never part of the generated list; range(10) generates a list of 10 values, the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative; sometimes this is called the `step')

On 7/19/07, elis aeris <[hidden email]> wrote:
I ran this


for x in range(5,10):
    print x




and OP was

5
6
7
8
9



why is that? shouldn't it print


t
6
7
8
9
10?

_______________________________________________
Tutor maillist  -  [hidden email]
<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://mail.python.org/mailman/listinfo/tutor" target="_blank"> http://mail.python.org/mailman/listinfo/tutor



_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor