[Tutor] Loop over floating point values

Amit Saha amitsaha.in at gmail.com
Sun Dec 1 10:03:15 CET 2013


Hello,

Much to my disbelief, I realized I hadn't written a program in Python
as far as I can recall which required me to do something like this, in
psuedocode:

x = 0.1

for i = 0 to x step 0.01
# do something with i
end i

Simply stated, I want to start from say a value, 0 and go upto 0.1 in
increments of 0.01.  I don't want to create a list with the values
hard-coded and then iterate over it, and hence I would use a while
loop instead:

x = 0.1
while i < x:
   # do something with i
   i += 0.01

I think this is one case, where you definitely cannot do this with a
for loop assuming the following restrictions:

- Do not create a list of the floating point values as i=[0.01, 0.02,
0.03..] - either like that or by using a suitable mathematical formula
combined with a list comprehension
- Use numpy's linspace() to create the list for you


Thoughts?

Thanks,
Amit.

-- 
http://echorand.me


More information about the Tutor mailing list