6.1. Operations Iteration
6.1.1. 1-dimensional Array
>>> import numpy as np
>>>
>>>
>>> data = np.array([1, 2, 3])
>>>
>>> for value in data:
... print(f'{value=}')
...
value=np.int64(1)
value=np.int64(2)
value=np.int64(3)
6.1.2. 2-dimensional Array
>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>> for value in data:
... print(f'{value=}')
...
value=array([1, 2, 3])
value=array([4, 5, 6])
value=array([7, 8, 9])
>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>> for row in data:
... for value in row:
... print(f'{value=}')
...
value=np.int64(1)
value=np.int64(2)
value=np.int64(3)
value=np.int64(4)
value=np.int64(5)
value=np.int64(6)
value=np.int64(7)
value=np.int64(8)
value=np.int64(9)
6.1.3. Flat
Flatten:
>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>> for value in data.flatten():
... print(f'{value=}')
...
value=np.int64(1)
value=np.int64(2)
value=np.int64(3)
value=np.int64(4)
value=np.int64(5)
value=np.int64(6)
value=np.int64(7)
value=np.int64(8)
value=np.int64(9)
Ravel:
>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>> for value in data.ravel():
... print(f'{value=}')
...
value=np.int64(1)
value=np.int64(2)
value=np.int64(3)
value=np.int64(4)
value=np.int64(5)
value=np.int64(6)
value=np.int64(7)
value=np.int64(8)
value=np.int64(9)
6.1.4. Enumerate
>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>> for i, value in enumerate(data):
... print(f'{i=}, {value=}')
...
i=0, value=array([1, 2, 3])
i=1, value=array([4, 5, 6])
i=2, value=array([7, 8, 9])
>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>> for i, value in enumerate(data.ravel()):
... print(f'{i=}, {value=}')
...
i=0, value=np.int64(1)
i=1, value=np.int64(2)
i=2, value=np.int64(3)
i=3, value=np.int64(4)
i=4, value=np.int64(5)
i=5, value=np.int64(6)
i=6, value=np.int64(7)
i=7, value=np.int64(8)
i=8, value=np.int64(9)
>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>> for i, row in enumerate(data):
... for j, value in enumerate(row):
... print(f'{i=}, {j=}, {value=}')
...
i=0, j=0, value=np.int64(1)
i=0, j=1, value=np.int64(2)
i=0, j=2, value=np.int64(3)
i=1, j=0, value=np.int64(4)
i=1, j=1, value=np.int64(5)
i=1, j=2, value=np.int64(6)
i=2, j=0, value=np.int64(7)
i=2, j=1, value=np.int64(8)
i=2, j=2, value=np.int64(9)
6.1.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 Iteration
# - Difficulty: easy
# - Lines: 3
# - Minutes: 5
# %% English
# 1. Use `for` to iterate over `DATA`
# 2. Define `result: list[int]` with even numbers from `DATA`
# 3. Run doctests - all must succeed
# %% Polish
# 1. Używając `for` iteruj po `DATA`
# 2. Zdefiniuj `result: list[int]` z liczbami parzystymi z `DATA`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `number % 2 == 0`
# %% 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 list, \
'Variable `result` has invalid type, expected: list'
>>> assert all(type(x) is np.int64 for x in result), \
'All values in `result` must be type int'
>>> result
[np.int64(2), np.int64(4), np.int64(6), np.int64(8)]
"""
import numpy as np
DATA = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
result = ...