0

I'm trying to print the numbers between 0.00001 and 0.1, where i tried:

for i in range(0.00001,0.1):
    print(i)

But python throws an error saying it cannot be interpreted as integers. Is it possible to do something like this in python?

Maxxx
  • 3,688
  • 6
  • 28
  • 55

2 Answers2

0

range only supports integers. Lets define our own

def frange(start, stop, step):
        while start < stop:
            yield start
            start += step

    for i in frange(0.5, 1.0, 0.1):
            print(i)
Jibin Mathews
  • 1,129
  • 1
  • 10
  • 23
-1

From the range() method documentation we can read the following:

The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method).

You also didn't specify the step you want to have. Because there are infinitely many real numbers between any 2 unique real numbers, you can't really get all of them.

What you can do is multiply both sides of the range by the same number until they are both integers and then when doing stuff with them divide them by that number again. I'm going to assume you wanted to have a step of 0.00001

mult = 100000
for i in range(1, 10000):
    print(i/mult)

If you want any different step, simply supply a third argument to the range() method.

Ehllie
  • 1
  • While your answer might be right, I'd like to ask you to read the [answer] section of the [help]. This question is a duplicate and therefore shouldn't be answered. – Vulpex Mar 31 '19 at 11:33