+ 2

float range

range(1.5, 10.5) does not work. So, how do I get all float values in the range(1.5, 10.5, 0.1)? any suggestions? abhinav Sω(ᗒ✧ᕙ☞Aᕗ✧ᗕ)ti Jan Markus Tibor Santa ▬▬▬ Rik Wittkopp Calvin T

12th Sep 2021, 5:45 AM
Harsha S
Harsha S - avatar
10 Answers
+ 9
Harsha S Python range can only generate a set of integer numbers from a given band. Neither does it allow a float type parameter nor it can produce a float range of numbers. You can use arange function from NumPy library https://www.techbeamers.com/python-float-range/
12th Sep 2021, 5:49 AM
A͢J
A͢J - avatar
+ 7
Harsha S You can't get float value of a range because a range creates a number of iterations. How can you get half an iteration? You could assign a float to your code with each iteration IE: lst = [] for i in range(5): lst.append(float(input())
12th Sep 2021, 5:50 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 5
For numbers with only one dezimal, create a range from 15 to 105 by step 1. Then divide every number by 10.
12th Sep 2021, 6:30 AM
Coding Cat
Coding Cat - avatar
+ 3
Harsha S we are programmers def frange(a, b, c): while a < b: yield a a += c for i in frange(0.1, 10.5, 0.1): print(i)
12th Sep 2021, 7:50 AM
abhinav
abhinav - avatar
+ 1
Thank you all
12th Sep 2021, 7:18 AM
Harsha S
Harsha S - avatar
+ 1
Rik Wittkopp what is half an iteration? range(a, b, c) simply means (a, a+b, a+2b, ... upto a+nb < c ). It is a limitation of the range function that it supports only integer values and I believe it's due to floating point approximation.
12th Sep 2021, 7:44 AM
abhinav
abhinav - avatar
+ 1
abhinav superb 👏 👏
12th Sep 2021, 7:59 AM
Harsha S
Harsha S - avatar
+ 1
abhinav a small change: def frange(a,b,c): while a<b: yield float("%.1f"%a) a += c print(list(frange(1.5, 10.5, 0.1)))
12th Sep 2021, 8:03 AM
Harsha S
Harsha S - avatar
- 4
Hello
12th Sep 2021, 11:41 AM
Prem Chand Singh
Prem Chand Singh - avatar