Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Range with floating point steps

import it

r = it.Range(1, 6, 2.2)
for n in r:
    print(n)

Output:

class Range():
    def __init__(self, start, end, step):
        self.current = start
        self.end = end
        self.step = step

    def __iter__(self):
        return self

    def __next__(self):
        if self.current >= self.end:
            raise StopIteration
        v = self.current
        self.current += self.step
        return v
1
3.2
5.4
import it

def test_range():
    r = it.Range(1, 6, 2.2)
    result = list(r)
    assert result == [1, 3.2, 5.4]