# 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. Use Cases¶

>>> list(range(0,3))
[0, 1, 2]
>>> tuple(range(0,3))
(0, 1, 2)
>>> set(range(0,3))
{0, 1, 2}
>>> list(range(4,11,2))
[4, 6, 8, 10]


## 5.3.7. Itertools¶

>>> from itertools import count
>>>
>>>
>>> result = count(3, 2)
>>>
>>> next(result)
3
>>> next(result)
5
>>> next(result)
7


## 5.3.8. Assignments¶

"""
* Assignment: Designaptterns Idiom Range
* Filename: designpatterns_idioms_range.py
* Complexity: medium
* Lines of code: 25 lines
* Time: 21 min

English:
1. Use data from "Given" section (see below)
2. Define class Range with parameters: start, stop, step
3. Write own implementation of a built-in range(start, stop, step) function
4. How to implement passing only stop argument (range(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 klasę Range z parametrami: start, stop, step
3. Zaimplementuj własne rozwiązanie wbudowanej funkcji range(start, stop, step)
4. Jak zaimplementować możliwość podawania tylko końca (range(start=0, stop=???, step=1))?
5. Wszystkie testy muszą przejść
6. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

Hint:
* use *args
* if len(args) == ...

Tests:
>>> from inspect import isclass, ismethod
>>> assert isclass(Range)

>>> r = Range(0, 0, 0)
>>> assert hasattr(r, '__iter__')
>>> assert hasattr(r, '__next__')
>>> assert ismethod(r.__iter__)
>>> assert ismethod(r.__next__)

>>> list(Range(0, 10, 2))
[0, 2, 4, 6, 8]

>>> list(Range(0, 5))
[0, 1, 2, 3, 4]

>>> list(Range(5))
[0, 1, 2, 3, 4]

>>> list(Range())
Traceback (most recent call last):
ValueError: Invalid arguments

>>> list(Range(1,2,3,4))
Traceback (most recent call last):
ValueError: Invalid arguments

>>> Range(stop=2)
Traceback (most recent call last):
TypeError: Range() takes no keyword arguments

>>> Range(start=1, stop=2)
Traceback (most recent call last):
TypeError: Range() takes no keyword arguments

>>> Range(start=1, stop=2, step=2)
Traceback (most recent call last):
TypeError: Range() takes no keyword arguments
"""