Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cycle "for" in python like in C [duplicate]

Tags:

python

Possible Duplicate:
Python decimal range() step value

I have a cycle in C:

for (float i = 0; i < 2 * CONST; i += 0.01) {
    // ... do something
}

I need the same cycle in python, but:

for x in xxx

not the same.

How can i make it in python?

like image 215
MrSil Avatar asked Oct 27 '12 08:10

MrSil


2 Answers

You are almost on the line. This is how you do it on a list: -

your_list = [1, 2, 3]
for eachItem in your_list:
    print eachItem

If you want to iterate over a certain range: -

for i in xrange(10):
    print i

To give a step value you can use a third parameter, so in your case it would be: -

for i in xrange(2 * CONST, 1):
    print i

But you can only give an integer value as step value.

If you want to use float increment, you would have to modify your range a little bit:-

for i in xrange(200 * CONST):
    print i / 100.0
like image 159
Rohit Jain Avatar answered Oct 09 '22 01:10

Rohit Jain


for i in xrange(200*CONST):
    i = i/100.0
like image 33
ajon Avatar answered Oct 08 '22 23:10

ajon