for loop counting backward?

E

Eric Wichterich

Hello,

I am converting a perl-script to python and have problems with this
simple perl-statement:

for ( $i = 100; $i > 2; $i-- ) {do something }

Does anyone know how to write this in python? I did not find any
documentation about counting backward in a for loop...

By the way:
I converted following perl - statement:

for $i ( 2 .. 100 ) { do something}

into python:

for counter in range(2,100)
do something

Thank you very much,
Eric
 
A

Alex Martelli

Eric said:
Hello,

I am converting a perl-script to python and have problems with this
simple perl-statement:

for ( $i = 100; $i > 2; $i-- ) {do something }

Does anyone know how to write this in python? I did not find any
documentation about counting backward in a for loop...

for i in range(100, 2, -1):
dosomething(i)


Alex
 
J

Jegenye 2001 Bt

for i in xrange(100,2,-1):
do_something()

Mind the "fence rule" in Pythonic slices, i.e. xrange

Regards,
Miklós
 

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

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top