# How to implement a range of float numbers function in Python

8 views
asked May 12

## 1 Answer

0 votes
```def frange(start, stop=None, step=None):
if stop == None:
stop = start
start = 0.0
if step == None:
step = 1.0
while True:
if step > 0 and start >= stop:
break
elif step < 0 and start <= stop:
break
yield "%g" % start
start = start + step

print("\nfrange(3.14, 3.17, 0.01)")
for i in frange(3.14, 3.17, 0.01):
print(i, end=" ")

print("\n\nfrange(-1.5, 3.5, 0.5)")
for i in frange(-1.5, 3.5, 0.5):
print(i, end=" ")

print("\n\nfrange(3.14)")
for i in frange(3.14):
print(i, end=" ")

print("\n\nfrange(1.4, 3.6)")
for i in frange(1.4, 3.6):
print(i, end=" ")

print("\n\nfrange(0.5, 2.5, -0.5)")
for i in frange(0.5, 2.5, -0.5):
print(i, end=" ")

print("\n\nfrange(6.5, 2.5, -0.5)")
for i in frange(6.5, 2.5, -0.5):
print(i, end=" ")

print("\n\nfrange(5.5, 1.5)")
for i in frange(5.5, 1.5):
print(i, end=" ")

'''
run:

frange(3.14, 3.17, 0.01)
3.14 3.15 3.16 3.17

frange(-1.5, 3.5, 0.5)
-1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3

frange(3.14)
0 1 2 3

frange(1.4, 3.6)
1.4 2.4 3.4

frange(0.5, 2.5, -0.5)

frange(6.5, 2.5, -0.5)
6.5 6 5.5 5 4.5 4 3.5 3

frange(5.5, 1.5)

'''```

answered May 12 by
edited May 12 by avibootz