3.2. Array Range — Python
3.2.1. SetUp
3.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])
3.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)
3.2.4. Recap
>>> a = np.array(range(0, 10)) >>> b = np.arange(0, 10, 2) >>> c = np.linspace(0, 10, 100)
3.2.5. Assignments
# %% About # - Name: Numpy Create Array # - Difficulty: easy # - Lines: 1 # - Minutes: 2 # %% 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 # %% 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ść # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is np.ndarray, \ 'Variable `result` has an invalid type; expected: `np.ndarray`.' >>> result array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import numpy as np # %% Types result: np.ndarray # %% Data # %% Result result = ...