2.1. Array Create

2.1.1. SetUp

>>> import numpy as np

2.1.2. Example

  • ndarray - n-dimensional array

>>> a = np.array([1, 2, 3])
>>>
>>> type(a)
<class 'numpy.ndarray'>

2.1.3. From List

>>> data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>> np.array(data)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

2.1.4. From Range

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

data range(0, 10) list(data) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

2.1.5. Declare

1-dimensional Array:

>>> np.array([1, 2, 3])
array([1, 2, 3])
>>>
>>> np.array([1.0, 2.0, 3.0])
array([1., 2., 3.])
>>>
>>> np.array([1.1, 2.2, 3.3])
array([1.1, 2.2, 3.3])
>>>
>>> np.array([1, 2, 3], float)
array([1., 2., 3.])
>>>
>>> np.array([1, 2, 3], dtype=float)
array([1., 2., 3.])

2-dimensional Array:

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

3-dimensional Array:

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

       [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]])
../../_images/numpy-create-cake.png

Figure 2.16. Multi layer cake as an analog for n-dim array [1]

2.1.6. Stringify

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]])
>>>
>>> str(a)
'[[1 2 3]\n [4 5 6]\n [7 8 9]]'
>>>
>>> print(a)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
>>>
>>> repr(a)
'array([[1, 2, 3],\n       [4, 5, 6],\n       [7, 8, 9]])'
>>>
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>>
>>> print(repr(a))
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

2.1.7. Performance

  • Python 3.11.4

Pure Python:

>>> 
... %%timeit -n 1000 -r 1000
... data = range(0, 10)
... result = list(data)
279 ns ± 102 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> 
... %%timeit -n 1000 -r 1000
... result = [x for x in range(0, 10)]
520 ns ± 201 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

Python + Numpy:

>>> 
... %%timeit -n 1000 -r 1000
... data = range(0, 10)
... result = np.array(data)
2.34 µs ± 249 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> 
... %%timeit -n 1000 -r 1000
... result = np.array(range(0, 10))
2.46 µs ± 359 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

Pure Numpy:

>>> 
... %%timeit -n 1000 -r 1000
... result = np.arange(0, 10)
559 ns ± 189 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

2.1.8. Recap

>>> a = np.array([1, 2, 3])
>>> b = np.array(range(0, 10))

2.1.9. References

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


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

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

# %% Polish
# 1. Zdefiniuj `result: np.ndarray`:
#    - dtype: float
#    - 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: float
# values: from 0 to 10 (without 10)
# use: `np.array()`
# type: np.ndarray
result = ...


# %% 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 Arange
# - 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.arange()`
# 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.arange()`
# 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.arange()`
# type: np.ndarray
result = ...


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

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

# %% Polish
# 1. Zdefiniuj `result: np.ndarray`:
#    - dtype: nie zmieniaj, pozostaw domyślny
#    - wartości: od 0 do 10 krok 2 (liczby parzyste)
#    - użyj: `np.arange()`
# 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, 2, 4, 6, 8])
"""

import numpy as np


# dtype: do not change, leave default
# values: from 0 to 10 step 2 (even numbers)
# use: `np.arange()`
# type: np.ndarray
result = ...


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

# %% English
# 1. Define `result: np.ndarray`:
#    - dtype: float
#    - values: from 0 to 10 step 2 (even numbers)
#    - use: `np.arange()`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: np.ndarray`:
#    - dtype: float
#    - wartości: od 0 do 10 krok 2 (liczby parzyste)
#    - użyj: `np.arange()`
# 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., 2., 4., 6., 8.])
"""

import numpy as np


# dtype: float
# values: from 0 to 10 step 2 (even numbers)
# use: `np.arange()`
# type: np.ndarray
result = ...


# %% 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 Linspace
# - 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.linspace()`
# 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.linspace()`
# 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.        ,  0.20408163,  0.40816327,  0.6122449 ,  0.81632653,
        1.02040816,  1.2244898 ,  1.42857143,  1.63265306,  1.83673469,
        2.04081633,  2.24489796,  2.44897959,  2.65306122,  2.85714286,
        3.06122449,  3.26530612,  3.46938776,  3.67346939,  3.87755102,
        4.08163265,  4.28571429,  4.48979592,  4.69387755,  4.89795918,
        5.10204082,  5.30612245,  5.51020408,  5.71428571,  5.91836735,
        6.12244898,  6.32653061,  6.53061224,  6.73469388,  6.93877551,
        7.14285714,  7.34693878,  7.55102041,  7.75510204,  7.95918367,
        8.16326531,  8.36734694,  8.57142857,  8.7755102 ,  8.97959184,
        9.18367347,  9.3877551 ,  9.59183673,  9.79591837, 10.        ])
"""

import numpy as np


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


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

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

# %% Polish
# 1. Zdefiniuj `result: np.ndarray`:
#    - dtype: nie zmieniaj, pozostaw domyślny
#    - wartości: od 0 do 10 liczba elementów 11
#    - użyj: `np.linspace()`
# 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., 10.])
"""

import numpy as np


# dtype: do not change, leave default
# values: from 0 to 10 number of elements 11
# use: `np.linspace()`
# type: np.ndarray
result = ...