5.1. Indexing Getitem
>>> class ndarray:
... def __getitem__(self, item):
... if type(item) == int:
... ...
... if type(item) == list:
... ...
... if type(item) == tuple:
... ...
... if type(item) == slice:
... ...
5.1.1. Index int
int
, example:0
or1
5.1.2. Index slice
slice
, example:1:3
or:3
or1:3:2
5.1.3. Index list
list[int]
, example:[0, 1]
list[bool]
, example:[True, False, True]
5.1.4. Index tuple[int, T]
tuple[int, int]
, example:0, 0
tuple[int, list[int]]
, example:1, [1,2]
tuple[int, list[bool]]
, example:1, [True, False, True]
tuple[int, slice]
, example:1, 0:2
5.1.5. Index tuple[slice, T]
tuple[slice, int]
, example:1:3, 0
tuple[slice, list[int]]
, example:1:3, [1,2]
tuple[slice, list[bool]]
, example:1:3, [True, False, True]
tuple[slice, slice]
, example:1:3, 0:2
5.1.6. Index tuple[list[int], T]
tuple[list[int], int]
, example:[0,1], 0
tuple[list[int], list[int]]
, example:[0,1], [1,2]
tuple[list[int], list[bool]]
, example:[0,1], [True, False, True]
tuple[list[int], slice]
, example:[0,1], 0:2
5.1.7. Index tuple[list[bool], T]
tuple[list[bool], int]
, example:[True, False, True], 0
tuple[list[bool], list[int]]
, example:[True, False, True], [1,2]
tuple[list[bool], list[bool]]
, example:[True, False, True], [True, False, True]
tuple[list[bool], slice]
, example:[True, False, True], 0:2
5.1.8. SetUp
>>> import numpy as np
5.1.9. Example
>>> a = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>>
>>> a[ 0 ] # int
array([1, 2, 3])
>>>
>>> a[ [0,2] ] # list[int]
array([[1, 2, 3],
[7, 8, 9]])
>>>
>>> a[ [True,False,True] ] # list[bool]
array([[1, 2, 3],
[7, 8, 9]])
5.1.10. Index
>>> a = np.array([1, 2, 3])
>>>
>>>
>>> a.flat[0]
np.int64(1)
>>>
>>> a.flat[1]
np.int64(2)
>>>
>>> a.flat[2]
np.int64(3)
>>>
>>> a.flat[4]
Traceback (most recent call last):
IndexError: index 4 is out of bounds for axis 0 with size 3
Flat:
>>> a = np.array([[1, 2, 3],
... [4, 5, 6]])
>>>
>>>
>>> a.flat[0]
np.int64(1)
>>>
>>> a.flat[1]
np.int64(2)
>>>
>>> a.flat[2]
np.int64(3)
>>>
>>> a.flat[3]
np.int64(4)
>>>
>>> a.flat[4]
np.int64(5)
>>>
>>> a.flat[5]
np.int64(6)
Multidimensional:
>>> a = np.array([[1, 2, 3],
... [4, 5, 6]])
>>>
>>>
>>> a[0][0]
np.int64(1)
>>>
>>> a[0][1]
np.int64(2)
>>>
>>> a[0][2]
np.int64(3)
>>>
>>> a[1][0]
np.int64(4)
>>>
>>> a[1][1]
np.int64(5)
>>>
>>> a[1][2]
np.int64(6)
>>>
>>> a[2]
Traceback (most recent call last):
IndexError: index 2 is out of bounds for axis 0 with size 2
>>>
>>> a[-1][-1]
np.int64(6)
>>>
>>> a[-3]
Traceback (most recent call last):
IndexError: index -3 is out of bounds for axis 0 with size 2
>>>
>>> a[0,0]
np.int64(1)
>>>
>>> a[0,1]
np.int64(2)
>>>
>>> a[0,2]
np.int64(3)
>>>
>>> a[1,0]
np.int64(4)
>>>
>>> a[1,1]
np.int64(5)
>>>
>>> a[1,2]
np.int64(6)
5.1.11. Selecting items
1-dimensional Array:
>>> a = np.array([1, 2, 3])
>>>
>>>
>>> a[0]
np.int64(1)
>>>
>>> a[1]
np.int64(2)
>>>
>>> a[2]
np.int64(3)
>>>
>>> a[3]
Traceback (most recent call last):
IndexError: index 3 is out of bounds for axis 0 with size 3
>>>
>>> a[-1]
np.int64(3)
2-dimensional Array:
>>> a = np.array([[1, 2, 3],
... [4, 5, 6]])
>>>
>>>
>>> a[0]
array([1, 2, 3])
>>>
>>> a[1]
array([4, 5, 6])
>>>
>>> a[2]
Traceback (most recent call last):
IndexError: index 2 is out of bounds for axis 0 with size 2
>>>
>>> a[0,0]
np.int64(1)
>>>
>>> a[0,1]
np.int64(2)
>>>
>>> a[0,2]
np.int64(3)
>>>
>>>
>>> a[1,0]
np.int64(4)
>>>
>>> a[1,1]
np.int64(5)
>>>
>>> a[1,2]
np.int64(6)
>>>
>>>
>>> a[2,0]
Traceback (most recent call last):
IndexError: index 2 is out of bounds for axis 0 with size 2
>>> a = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>>
>>> a[0]
array([1, 2, 3])
>>>
>>> a[1]
array([4, 5, 6])
>>>
>>> a[2]
array([7, 8, 9])
>>>
>>>
>>> a[0,0]
np.int64(1)
>>>
>>> a[0,1]
np.int64(2)
>>>
>>> a[0,2]
np.int64(3)
>>>
>>>
>>> a[1,0]
np.int64(4)
>>>
>>> a[1,1]
np.int64(5)
>>>
>>> a[1,2]
np.int64(6)
>>>
>>>
>>> a[2,0]
np.int64(7)
>>>
>>> a[2,1]
np.int64(8)
>>>
>>> a[2,2]
np.int64(9)
3-dimensional Array:
>>> a = np.array([[[ 1, 2, 3],
... [ 4, 5, 6],
... [ 5, 6, 7]],
... [[11, 22, 33],
... [44, 55, 66],
... [77, 88, 99]]])
>>>
>>>
>>> a[0,0,0]
np.int64(1)
>>>
>>> a[0,0,1]
np.int64(2)
>>>
>>> a[0,0,2]
np.int64(3)
>>>
>>> a[0,0,3]
Traceback (most recent call last):
IndexError: index 3 is out of bounds for axis 2 with size 3
>>>
>>> a[0,1,2]
np.int64(6)
>>>
>>> a[0,2,1]
np.int64(6)
>>>
>>> a[2,1,0]
Traceback (most recent call last):
IndexError: index 2 is out of bounds for axis 0 with size 2
5.1.12. Substituting items
1-dimensional Array:
Will type cast values to
np.ndarray.dtype
>>> a = np.array([1, 2, 3])
>>>
>>>
>>> a[0] = 99
>>> a
array([99, 2, 3])
>>>
>>> a[-1] = 11
>>> a
array([99, 2, 11])
>>> a = np.array([1, 2, 3], float)
>>>
>>>
>>> a[0] = 99.9
>>> a
array([99.9, 2. , 3. ])
>>>
>>> a[-1] = 11.1
>>> a
array([99.9, 2. , 11.1])
>>> a = np.array([1, 2, 3], int)
>>>
>>>
>>> a[0] = 99.9
>>> a
array([99, 2, 3])
>>>
>>> a[-1] = 11.1
>>> a
array([99, 2, 11])
2-dimensional Array:
>>> a = np.array([[1, 2, 3],
... [4, 5, 6]])
>>>
>>>
>>> a[0,0] = 99
>>> a
array([[99, 2, 3],
[ 4, 5, 6]])
>>>
>>> a[1,2] = 11
>>> a
array([[99, 2, 3],
[ 4, 5, 11]])
5.1.13. Multi-indexing
>>> a = np.array([1, 2, 3])
>>>
>>>
>>> a[0], a[2], a[-1]
(np.int64(1), np.int64(3), np.int64(3))
>>>
>>> a[[0, 2, -1]]
array([1, 3, 3])
>>>
>>> a[[True, False, True]]
array([1, 3])
>>> a = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>>
>>> a[[0,1]]
array([[1, 2, 3],
[4, 5, 6]])
>>>
>>> a[[0,2,-1]]
array([[1, 2, 3],
[7, 8, 9],
[7, 8, 9]])
>>>
>>> a[[True, False, True]]
array([[1, 2, 3],
[7, 8, 9]])
5.1.14. 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 Indexing
# - Difficulty: easy
# - Lines: 5
# - Minutes: 5
# %% English
# 1. Create `result: np.ndarray`
# 2. Add to `result` elements from `DATA` at indexes:
# - row 0, column 2
# - row 2, column 2
# - row 0, column 0
# - row 1, column 0
# 3. `result` size must be 2x2
# 4. `result` type must be float
# 5. Run doctests - all must succeed
# %% Polish
# 1. Stwórz `result: np.ndarray`
# 2. Dodaj do `result` elementy z `DATA` o indeksach:
# - wiersz 0, kolumna 2
# - wiersz 2, kolumna 2
# - wiersz 0, kolumna 0
# - wiersz 1, kolumna 0
# 3. Rozmiar `result` musi być 2x2
# 4. Typ `result` musi być float
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `np.zeros(shape, dtype)`
# %% 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([[3., 9.],
[1., 4.]])
"""
import numpy as np
DATA = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
result = ...