Robert Kern
2/3/2008 10:56:00 AM
James Matthews wrote:
> Because 0 is counted therefore i only have to do it 99 times
No, Gabriel is correct. range(n) creates a list of integers starting at 0 and
going to n-1 (inclusive), not n.
In [1]: range(9)
Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
In [2]: len(range(9))
Out[2]: 9
In [3]: len(range(1, 9))
Out[3]: 8
In [4]: range(10)
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [5]: len(range(10))
Out[5]: 10
> On Feb 3, 2008 4:38 AM, Gabriel Genellina <gagsl-py2@yahoo.com.ar
> <mailto:gagsl-py2@yahoo.com.ar>> wrote:
>
> En Sun, 03 Feb 2008 01:03:43 -0200, James Matthews
> <nytrokiss@gmail.com <mailto:nytrokiss@gmail.com>>
> escribió:
>
> Sorry to be nitpicking, but people coming from other languages may get
> confused by the wrong examples:
>
> > What i do is a simple range call. for i in range(number of times
> i want
> > to repeat something)
> > I guess it comes from my C days for(i=0;i<100;i++) { or in python
> for i
> > in range(99):
>
> Should be `for i in range(100)` to match exactly the C loop. Both
> iterate
> 100 times, with i varying from 0 to 99 inclusive.
>
> >> miller.paul.w@gmail.com <mailto:miller.paul.w@gmail.com> wrote:
> >>
> >> > Ruby has a neat little convenience when writing loops where
> you don't
> >> > care about the loop index: you just do n.times do { ... some
> >> > code ... } where n is an integer representing how many times
> you want
> >> > to execute "some code."
> >> >
> >> > In Python, the direct translation of this is a for loop. When the
> >> > index doesn't matter to me, I tend to write it as:
> >> >
> >> > for _ in xrange (1,n):
> >> > some code
>
> Should be `for _ in xrange(n)` to match the Ruby example. Both iterate n
> times.
>
> > On Feb 3, 2008 3:34 AM, Roy Smith <roy@panix.com
> <mailto:roy@panix.com>> wrote:
> >
> >> But, more to the point, I'd try to find variable name which
> described
> >> why I was looping, even if I didn't actually use the value in
> theloop
> >> body:
>
> Me too. Government don't collect taxes by the number of variable names
> used (yet).
>
> --
> Gabriel Genellina
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco