Float list in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Float list in Python

How to create a range list with float numbers: numbers = list(range(0, 10, 0.5))

21st May 2019, 9:29 AM
Leon KV
3 Answers
+ 9
# using numpy: import numpy as np l = list(np.arange(0, 10, .5)) # manually: l = [n/2 for n in range(20)]
21st May 2019, 10:19 AM
Anna
Anna - avatar
+ 3
There is no built-in function to create a range of floats. So you have to write a function to do this task for you. You can try this: new_lst = [] for i in range(0, 11): new_lst.append(float(i)) new_lst.append(float(i + 0.5)) print(new_lst)
21st May 2019, 9:35 AM
Lothar
Lothar - avatar
+ 2
An other version, more flexible than the first one. # creating range of floats. It does currently not work for negativ numbers. start = 0.0 stop = 100.0 step = 0.253 res_lst = [] res_buf = start while res_buf <= stop: res_lst.append(format(round(res_buf, 3),'.3f')) res_buf += step print(res_lst) https://code.sololearn.com/cbAe1UaBucxD/?ref=app
21st May 2019, 6:44 PM
Lothar
Lothar - avatar