5.3. Range¶
5.3.1. Rationale¶
Return sequence of numbers
It is not a generator
Generator (lazy evaluated)
Built-in
5.3.2. Syntax¶
range([start], <stop>, [step])
optional
start
, inclusive, default:0
required
stop
, exclusive,optional
step
, default:1
5.3.3. Problem¶
>>> i = 0
>>> result = []
>>>
>>> while i < 3:
... result.append(i)
... i += 1
>>>
>>> result
[0, 1, 2]
5.3.4. Solution¶
>>> result = range(0,3)
>>>
>>> list(result)
[0, 1, 2]
5.3.5. Lazy Evaluation¶
>>> for i in range(0,3):
... print(i)
0
1
2
5.3.6. Itertools¶
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
5.3.7. Assignments¶
"""
* Assignment: Idioms Range Impl
* Complexity: medium
* Lines of code: 25 lines
* Time: 21 min
English:
1. Use data from "Given" section (see below)
2. Define function `myrange` with parameters: `start`, `stop`, `step`
3. Write own implementation of a built-in `myrange(start, stop, step)` function
4. How to implement passing only stop argument (`myrange(start=0, stop=???, step=1)`)?
5. All tests must pass
6. Compare result with "Tests" section (see below)
Polish:
1. Użyj danych z sekcji "Given" (patrz poniżej)
2. Zdefiniuj funkcję `myrange` z parametrami: `start`, `stop`, `step`
3. Zaimplementuj własne rozwiązanie wbudowanej funkcji `myrange(start, stop, step)`
4. Jak zaimplementować możliwość podawania tylko końca (`myrange(start=0, stop=???, step=1)`)?
5. Wszystkie testy muszą przejść
6. Porównaj wyniki z sekcją "Tests" (patrz poniżej)
Hint:
* use `*args` and `**kwargs`
* `if len(args) == ...`
Tests:
>>> from inspect import isfunction
>>> assert isfunction(myrange)
>>> myrange(0, 10, 2)
[0, 2, 4, 6, 8]
>>> myrange(0, 5)
[0, 1, 2, 3, 4]
>>> myrange(5)
[0, 1, 2, 3, 4]
>>> myrange()
Traceback (most recent call last):
ValueError: Invalid arguments
>>> myrange(1,2,3,4)
Traceback (most recent call last):
ValueError: Invalid arguments
>>> myrange(stop=2)
Traceback (most recent call last):
TypeError: myrange() takes no keyword arguments
>>> myrange(start=1, stop=2)
Traceback (most recent call last):
TypeError: myrange() takes no keyword arguments
>>> myrange(start=1, stop=2, step=2)
Traceback (most recent call last):
TypeError: myrange() takes no keyword arguments
"""