+ 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? ο½ο½‚ο½ˆο½‰ο½Žο½ο½– [user id="9783982"]SΟ‰(α—’βœ§α•™β˜žAα•—βœ§α—•)ti[/user] [user id="6470703"]Jan Markus[/user] [user id="2549279"]Tibor Santa[/user] [user id="22811449"]β–¬β–¬β–¬[/user] [user id="12572725"]Rik Wittkopp[/user] [user id="23103406"]Calvin T[/user]

12th Sep 2021, 5:45 AM
Harsha S
Harsha S - avatar
12 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
Harsha S import numpy as np print(np.arange(1.5, 10.5, 0.1)) output : [ 1.5 1.6 1.7 1.8 1.9 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3. 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4. 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5. 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6. 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7. 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8. 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9. 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10. 10.1 10.2 10.3 10.4] [Program finished] I hope it helped ☺️
12th Sep 2021, 5:54 AM
Prince Kumar
Prince Kumar - 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
ο½ο½‚ο½ˆο½‰ο½Žο½ο½–
ο½ο½‚ο½ˆο½‰ο½Žο½ο½– - 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
ο½ο½‚ο½ˆο½‰ο½Žο½ο½–
ο½ο½‚ο½ˆο½‰ο½Žο½ο½– - avatar
+ 1
12th Sep 2021, 7:59 AM
Harsha S
Harsha S - avatar
+ 1
ο½ο½‚ο½ˆο½‰ο½Žο½ο½– 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
0
This representation gives a range of approximately 3.4E-38 to 3.4E+38 for type float. You can declare variables as float. https://www.tellpopeyes.biz/
13th Sep 2021, 6:03 AM
Gorczany
4
Hello
12th Sep 2021, 11:41 AM
Prem Chand Singh
Prem Chand Singh - avatar