10.2. Iterator Range

  • Return sequence of numbers

  • It is not a generator

  • Generator (lazy evaluated)

  • range([start], <stop>, [step])

  • optional start, inclusive, default: 0

  • required stop, exclusive,

  • optional step, default: 1

10.2.1. Problem

>>> i = 0
>>> result = []
>>>
>>> while i < 3:
...     result.append(i)
...     i += 1
>>>
>>> result
[0, 1, 2]

10.2.2. Solution

>>> result = range(0, 3)
>>>
>>> list(result)
[0, 1, 2]

10.2.3. Lazy Evaluation

Range is not an iterator:

>>> result = range(0,3)
>>>
>>> next(result)
Traceback (most recent call last):
TypeError: 'range' object is not an iterator

But it is an iterable, which can generate iterator:

>>> data = range(0,3)
>>> result = iter(data)
>>>
>>> next(result)
0
>>> next(result)
1
>>> next(result)
2
>>> next(result)
Traceback (most recent call last):
StopIteration

10.2.4. Instant Evaluation

>>> list(range(0, 3))
[0, 1, 2]

10.2.5. Iteration

>>> for i in range(0, 3):
...     print(i)
0
1
2

10.2.6. Rationale

  • for i in range(0, 9_999_999_999) and then if i == 3: break

  • Only 0, 1, 2 will be generated, not all 10 bilion numbers

>>> for i in range(0, 9_999_999_999):
...     if i == 3:
...         break
...     print(i)
0
1
2

10.2.7. Inspect

  • from inspect import isgeneratorfunction

  • from inspect import isgenerator

>>> from inspect import isgeneratorfunction, isgenerator
>>>
>>>
>>> isgeneratorfunction(range)
False
>>>
>>> result = range(0, 3)
>>> isgenerator(result)
False

10.2.8. Assignments

# %% About
# - Name: Iterator Range Impl
# - Difficulty: medium
# - Lines: 7
# - Minutes: 5

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Write own implementation of a built-in `range()` function
# 2. Define function `myrange` with parameters:
#    - parameter `start: int`
#    - parameter `stop: int`
#    - parameter `step: int`
# 3. Don't validate arguments and assume, that user will:
#    - always pass valid type of arguments
#    - never give only one argument
#    - arguments will be unsigned
# 4. Do not use built-in function `range()`
# 5. Use `yield` keyword
# 6. Run doctests - all must succeed

# %% Polish
# 1. Zaimplementuj własne rozwiązanie wbudowanej funkcji `range()`
# 2. Zdefiniuj funkcję `myrange` z parametrami:
#    - parameter `start: int`
#    - parameter `stop: int`
#    - parameter `step: int`
# 3. Nie waliduj argumentów i przyjmij, że użytkownik:
#    - zawsze poda argumenty poprawnych typów
#    - nigdy nie poda tylko jednego argumentu
#    - argumenty będą nieujemne
# 4. Nie używaj wbudowanej funkcji `range()`
# 5. Użyj słowa kluczowego `yield`
# 6. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# >>> list(myrange(0, 10, 2))
# [0, 2, 4, 6, 8]
#
# >>> list(myrange(0, 5))
# [0, 1, 2, 3, 4]

# %% Hints
# - https://github.com/python/cpython/blob/main/Objects/rangeobject.c

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from inspect import isfunction
>>> assert isfunction(myrange)

>>> from pprint import pprint

>>> result = myrange(0, 10, 2)
>>> list(result)
[0, 2, 4, 6, 8]

>>> result = myrange(0, 5)
>>> list(result)
[0, 1, 2, 3, 4]
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports

# %% Types
from typing import Callable
myrange: Callable[[int,int,int], list[int]]

# %% Data

# %% Result
def myrange(start=0, stop=None, step=1):
    ...