for loop

H

houbahop

Hello,
I have seen a lot of way to use the for statement, but I still don't know
how to do:

for i=morethanzero to n
or
for i= 5 to len(var)

of course I kno wthat I can use a while loop to achieve that but if this is
possible whith a for one, my preference goes to the for.

regards,
Dominique.
 
S

Steven Bethard

houbahop said:
for i=morethanzero to n

for i in range(morethanzero, n):
...
for i= 5 to len(var)

for i in range(5, len(var)):
...

although range(len(var)) is usually not what you want, because you can
just do:

for item in itertools.islice(var, 5, None):
...

or if you really do need the index too:

for i, item in itertools.islice(enumerate(var), 5, None):
...

Steve
 
D

Diez B. Roggisch

for i in range(morethanzero, n):
...


for i in range(5, len(var)):
...

Better use xrange - it doesn't create an actual list, but instead an
iterator enumerating the elements. That way more memory and cpu efficient.
 
S

Steven Bethard

Diez said:
Better use xrange - it doesn't create an actual list, but instead an
iterator enumerating the elements. That way more memory and cpu efficient.

I could've sworn I read somewhere in the past that xrange was supposed
to be slower than range in some situation, but at least for some simple
cases in Python 2.4, it seems to be about as fast or faster:
> python -m timeit "for i in range(100): a = i*2"
10000 loops, best of 3: 21.8 usec per loop
> python -m timeit "for i in xrange(100): a = i*2"
10000 loops, best of 3: 19.4 usec per loop
> python -m timeit "for i in range(10000): a = i*2"
100 loops, best of 3: 2.18 msec per loop
> python -m timeit "for i in xrange(10000): a = i*2"
100 loops, best of 3: 2.29 msec per loop
> python -m timeit "for i in range(100000): a = i*2"
10 loops, best of 3: 25.5 msec per loop
> python -m timeit "for i in xrange(100000): a = i*2"
10 loops, best of 3: 20.7 msec per loop

I hardly ever use (x)range for anything (because you can almost always
use a for loop to loop over items instead), but I'll file this info away
mentally for the next time I do. Thanks!

Steve
 
L

loritsch

houbahop said:
Hello,
I have seen a lot of way to use the for statement, but I still don't know
how to do:

for i=morethanzero to n
or
for i= 5 to len(var)

of course I kno wthat I can use a while loop to achieve that but if this is
possible whith a for one, my preference goes to the for.

regards,
Dominique.

I think the key point that hasn't been made here is what a for
statement is really doing in python...
From the online documentation:

"The for statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object"

Neither of the statements:
for i=morethanzero to n
for i= 5 to len(var)
create a sequence or iterable object, and that is why they don't work.

That is why previous posts in this thread have suggested using range(),
xrange(), etc. Because they create a sequence or iterable object.
When first using python, I recall that this distinction was not clear
to me, as I was used to a more traditional for statement (as in
C/C++):

for ( initialise ; test ; update )

Essentially, python's for statement is more like a foreach statement in
many other languages (Perl, C#, etc). These statements essentially
reduce the traditional form above to what many consider a more readable
form:

foreach item in collection:

In order to transform the tradition for statement into this more
readable form, each language requires that their collections being
iterated over satisfy a precondition defined by the language (in python
this precondition is that the collection is iterable).

While this restriction may seem a little strange to people who are used
to the more traditional for statement form, the result of this approach
is often a more readable and clear statement.
Regards,

Michael Loritsch
 
H

houbahop

thank you,
Whith python, It seems that all that I have learn in other languages is not
directly usable :) but looks like python is more efficient.
dominique.
 
D

Diez B. Roggisch

I hardly ever use (x)range for anything (because you can almost always
use a for loop to loop over items instead), but I'll file this info away
mentally for the next time I do. Thanks!

Thats true for me too - and since enumerate was introduced, the number of
applications of xrange have decreased further.
 
B

Binu K S

If you are used to the C kind of for loops, avoid mistakes like this one:
.... print i
.... i+=2
....
1
2
3
4
5

Obvious if you know you are iterating over a sequence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,214
Messages
2,571,111
Members
47,703
Latest member
robert.marryson

Latest Threads

Top