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
np.float64(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

# %% 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: Numpy Create Array
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

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

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

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> 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 = ...