9.3. 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
9.3.1. Problem
>>> i = 0
>>> result = []
>>>
>>> while i < 3:
... result.append(i)
... i += 1
>>>
>>> result
[0, 1, 2]
9.3.2. Solution
>>> result = range(0,3)
>>>
>>> list(result)
[0, 1, 2]
9.3.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
9.3.4. Iteration
>>> for i in range(0,3):
... print(i)
0
1
2
9.3.5. Count
Learn more at https://docs.python.org/3/library/itertools.html
More information in Itertools
itertools.count(start=0, step=1)
>>> from itertools import count
>>>
>>>
>>> result = count(3, 2)
>>>
>>> next(result)
3
>>> next(result)
5
>>> next(result)
7
9.3.6. Assignments
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Iterator Range Impl
# - Difficulty: medium
# - Lines: 7
# - Minutes: 5
# %% 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. 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. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - https://github.com/python/cpython/blob/main/Objects/rangeobject.c
# %% Tests
"""
>>> 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)
>>> pprint(result, width=72, sort_dicts=False)
[0, 2, 4, 6, 8]
>>> result = myrange(0, 5)
>>> pprint(result, width=72, sort_dicts=False)
[0, 1, 2, 3, 4]
"""
# Write own implementation of a built-in `range()` function
# Define function `myrange` with parameters: `start`, `stop`, `step`
# type: Callable[[int,int,int], list[int]]
def myrange(start=0, stop=None, step=1):
...