11.4. Loop For Range

  • range(start, stop, step)

  • start is inclusive, default: 0

  • stop is exclusive, required

  • step default: 1

11.4.1. Range Object

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

Loops with range:

11.4.2. Range with Loops

>>> for i in range(0, 3):
...     print(i)
0
1
2
>>> for number in range(4, 11, 2):
...     print(number)
4
6
8
10

11.4.3. Range of Length

>>> users = ['Mark', 'Melissa', 'Rick']
>>>
>>> for i in range(len(users)):
...     name = users[i]
...     print(name)
Mark
Melissa
Rick

But the following is more Pythonic:

>>> users = ['Mark', 'Melissa', 'Rick']
>>>
>>> for user in users:
...     print(user)
Mark
Melissa
Rick

The output is exactly the same. The second example is more Pythonic and easier to read.

11.4.4. Assignments

"""
* Assignment: Loop For Range
* Type: class assignment
* Complexity: easy
* Lines of code: 3 lines
* Time: 3 min

English:
    1. Generate `result: list[int]` with numbers from 0 (inclusive) to 5 (exclusive)
    2. You can use `range()` in a loop, but not conversion: `list(range())`
    3. Run doctests - all must succeed

Polish:
    1. Wygeneruj `result: list[int]` z liczbami od 0 (włącznie) do 5 (rozłącznie)
    2. Możesz użyć `range()` w pętli, ale nie konwersji: `list(range())`
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assign your result to variable `result`'
    >>> assert type(result) is list, \
    'Variable `result` has invalid type, should be list'
    >>> assert all(type(x) is int for x in result), \
    'All elements in `result` should be int'

    >>> result
    [0, 1, 2, 3, 4]
"""


# List with numbers from 0 to 5 (exclusive)
# type: list[int]
result = ...

"""
* Assignment: Loop Dict To List
* Type: class assignment
* Complexity: medium
* Lines of code: 4 lines
* Time: 5 min

English:
    1. Define `result: list[dict]`:
        a. key - name from the header
        b. value - measurement or species
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: list[dict]`:
        a. klucz - nazwa z nagłówka
        b. wartość - wyniki pomiarów lub gatunek
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from pprint import pprint

    >>> assert result is not Ellipsis, \
    'Assign your result to variable `result`'
    >>> assert type(result) is list, \
    'Variable `result` has invalid type, should be list'

    >>> assert all(type(x) is dict for x in result)

    >>> pprint(result, width=120, sort_dicts=False)
    [{'sepal_length': 5.8, 'sepal_width': 2.7, 'petal_length': 5.1, 'petal_width': 1.9, 'species': 'virginica'},
     {'sepal_length': 5.1, 'sepal_width': 3.5, 'petal_length': 1.4, 'petal_width': 0.2, 'species': 'setosa'},
     {'sepal_length': 5.7, 'sepal_width': 2.8, 'petal_length': 4.1, 'petal_width': 1.3, 'species': 'versicolor'},
     {'sepal_length': 6.3, 'sepal_width': 2.9, 'petal_length': 5.6, 'petal_width': 1.8, 'species': 'virginica'},
     {'sepal_length': 6.4, 'sepal_width': 3.2, 'petal_length': 4.5, 'petal_width': 1.5, 'species': 'versicolor'},
     {'sepal_length': 4.7, 'sepal_width': 3.2, 'petal_length': 1.3, 'petal_width': 0.2, 'species': 'setosa'}]
"""

DATA = [
    ('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
    (5.8, 2.7, 5.1, 1.9, 'virginica'),
    (5.1, 3.5, 1.4, 0.2, 'setosa'),
    (5.7, 2.8, 4.1, 1.3, 'versicolor'),
    (6.3, 2.9, 5.6, 1.8, 'virginica'),
    (6.4, 3.2, 4.5, 1.5, 'versicolor'),
    (4.7, 3.2, 1.3, 0.2, 'setosa'),
]

# Define variable `result` with converted DATA
# type: list[dict]
result = ...