Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do get more control over loop increments in Python?

Tags:

python

syntax

I'm trying to loop from 0 to 1 using step sizes of 0.01 (for example). How would I go about doing this? The for i in range(start, stop, step) only takes integer arguments so floats won't work.

like image 358
efficiencyIsBliss Avatar asked Feb 08 '11 05:02

efficiencyIsBliss


People also ask

How do you increment a loop in Python?

To iterate through an iterable in steps, using for loop, you can use range() function. range() function allows to increment the “loop index” in required amount of steps.

How do you add increments in Python?

This time around I thought it would be fun to look at a few different ways to increment a number in Python. x += 1 . In addition, there are a few less conventional options like using the add method of the operator module or using generator expressions.

How do you increment a loop by 2 in Python?

Python For Loop Increment By 2 Using the range() Function We use the range() function to implement a for loop in python. The range() function is first used to create a sequence and then we execute the for loop using the sequence.

Does Python for loop automatically increment?

The for statement specifies the counter variable x and its start and end values. Python will automatically increments the counter (x) variable by 1 after coming to end of the execution block. Python can use any iterable method as a the for loop counter.


1 Answers

for i in [float(j) / 100 for j in range(0, 100, 1)]:     print i 
like image 88
Santa Avatar answered Oct 08 '22 03:10

Santa