2.2. Array Range

2.2.1. SetUp

>>> import numpy as np

2.2.2. Array range

Array from Python range():

>>> np.array(range(5))
array([0, 1, 2, 3, 4])
>>>
>>> np.array(range(5), float)
array([0., 1., 2., 3., 4.])
>>>
>>> np.array(range(5, 10))
array([5, 6, 7, 8, 9])
>>>
>>> np.array(range(5, 10), float)
array([5., 6., 7., 8., 9.])
>>>
>>> np.array(range(5, 10, 2))
array([5, 7, 9])
>>>
>>> np.array(range(5, 10, 2), float)
array([5., 7., 9.])

Array from Python comprehension:

>>> np.array([x for x in range(5)])
array([0, 1, 2, 3, 4])
>>>
>>> np.array([x for x in range(5)], float)
array([0., 1., 2., 3., 4.])
>>>
>>> np.array([x for x in range(5, 10)])
array([5, 6, 7, 8, 9])
>>>
>>> np.array([x for x in range(5, 10)], float)
array([5., 6., 7., 8., 9.])
>>>
>>> np.array([x for x in range(5, 10, 2)])
array([5, 7, 9])
>>>
>>> np.array([x for x in range(5, 10, 2)], float)
array([5., 7., 9.])

Array from np.arange():

>>> np.arange(5)
array([0, 1, 2, 3, 4])
>>>
>>> np.arange(5, dtype=float)
array([0., 1., 2., 3., 4.])
>>>
>>> np.arange(5.0)
array([0., 1., 2., 3., 4.])
>>>
>>> np.arange(5, 10)
array([5, 6, 7, 8, 9])
>>>
>>> np.arange(5, 10, step=2)
array([5, 7, 9])
>>>
>>> np.arange(start=5, stop=10, step=2)
array([5, 7, 9])
>>>
>>> np.arange(start=5, stop=10, step=2, dtype=float)
array([5., 7., 9.])
>>>
>>> np.arange(0.0, 1.0, 0.1)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
>>>
>>> np.arange(0.0, 1.0, 0.2)
array([0. , 0.2, 0.4, 0.6, 0.8])
>>>
>>> np.arange(0.0, 1.0, 0.3)
array([0. , 0.3, 0.6, 0.9])

2.2.3. Linspace

>>> 
... def linspace(self,
...              start=...,
...              stop=...,
...              num=50,
...              endpoint=True,
...              retstep=False,
...              dtype=None
...              axis=0
... ) -> np.ndarray: ...

Return evenly spaced numbers over a specified interval.

>>> np.linspace(2.0, 3.0, num=5)
array([2.  , 2.25, 2.5 , 2.75, 3.  ])
>>>
>>> np.linspace(2.0, 3.0, num=5, endpoint=False)
array([2. , 2.2, 2.4, 2.6, 2.8])
>>> data, step = np.linspace(2.0, 3.0, num=5, retstep=True)
>>>
>>> data
array([2.  , 2.25, 2.5 , 2.75, 3.  ])
>>>
>>> step
0.25

2.2.4. Recap

>>> a = np.array(range(0, 10))
>>> b = np.arange(0, 10, 2)
>>> c = np.linspace(0, 10, 100)

2.2.5. Assignments

Code 2.35. Solution
"""
* Assignment: Numpy Create Array
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Define `result: np.ndarray`:
       a. dtype: do not change, leave default
       b. values: from 0 to 10 (without 10)
       c. use: `np.array()`
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: np.ndarray`:
       a. dtype: nie zmieniaj, pozostaw domyślny
       b. wartości: od 0 do 10 (bez 10)
       c. użyj: `np.array()`
    2. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is np.ndarray, \
    'Variable `result` has invalid type, expected: np.ndarray'

    >>> result
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
"""

import numpy as np


# dtype: do not change, leave default
# values: from 0 to 10 (without 10)
# use: `np.array()`
# type: np.ndarray
result = ...