473,410 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

for loop counting backward?

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
Jul 18 '05 #1
2 12219
Eric Wichterich wrote:
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

Jul 18 '05 #2
for i in xrange(100,2,-1):
do_something()

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

Regards,
Miklós

--
Prisznyák Miklós
---
Jegenye 2001 Bt. ( jegenye2001 at (NoSPAM)parkhosting dot com )
Egyedi szoftverkészítés, tanácsadás
Custom software development, consulting
http://jegenye2001.parkhosting.com
Eric Wichterich <er*************@gmx.de> wrote in message
news:ma************************************@python .org...
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

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
16
by: Joona I Palaste | last post by:
Reading Skybuck Flying's obvious troll message, I thought of how to properly do a for loop that would iterate over the complete range of an unsigned variable. As you all know, Skybuck's method...
22
by: Jan Richter | last post by:
Hi there, the Code below shows DJBs own implementation of strlen (str_len): unsigned int str_len(char *s) { register char *t; t = s; for (;;) { if (!*t) return t - s; ++t;
14
by: Bill Nguyen | last post by:
The Timer loop below consumes all available CPU time from my development PC. I would like to be able to restrict its use of CPU time to minimum since it's not actually doing anything but counting...
1
by: Bob | last post by:
The below regular express can sometimes cause an infinite loop. Anyone know what could cause this? string RegexSig = @"To:*\s*(?<address>\S+@\S+)*(\s|\S)*did not reach the following recipient"; ...
7
by: steve marchant | last post by:
trying to learn VB6. Simple counting loop which counts to 8 in 1 sec intervals, then starts from 1 again and repeats. Have two Command buttons on the form. Cmd1 starts the counting, and I need to...
15
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is...
15
by: Alex Snast | last post by:
Hello I'm new to python and i can't figure out how to write a reverse for loop in python e.g. the python equivalent to the c++ loop for (i = 10; i >= 0; --i)
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.