5.2. Indexing Slice

>>> 
... a[ 0 ]                              # int
... a[ [0,1] ]                          # list[int]
... a[ [True,False] ]                   # list[bool]
... a[ [[True,False], [True,False]] ]   # list[list[bool]]
... a[ 0:1 ]                            # slice(start,stop)
... a[ 0:1:2 ]                          # slice(start,stop,step)
...
... a[ 0,1 ]                            # tuple[int]
... a[ (0,1) ]                          # tuple[int]
... a[ [0,1], [2,3] ]                   # tuple[list[int]]
... a[ :,: ]                            # tuple[slice]
... a[ [True,False], [False,True] ]     # tuple[list[bool]]

1-dimensional Array:

  • int

  • list[int]

  • list[bool]

  • list[list[bool]]

  • slice(start,stop)

  • slice(start,stop,step)

2-dimensional Array:

  • tuple[int]

  • tuple[list[int]]

  • tuple[slice]

  • tuple[list[bool]]

5.2.1. SetUp

>>> import numpy as np

5.2.2. Recap

  • slice(start:stop:step)

  • data[start:stop:step]

  • By default start=0

  • By default stop=len(data)

  • By default step=1

>>> data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> data[1:5:2]  # slice(start=1, stop=5, step=2)
array([2, 4])
>>> data[:5:2]  # slice(start=0, stop=5, step=2)
array([1, 3, 5])
>>> data[1::2]  # slice(start=1, stop=len(data), step = 2)
array([2, 4, 6, 8])
>>> data[1:5]  # slice(start=1, stop=5, step=1)
array([2, 3, 4, 5])
>>> data[::2]  # slice(start=0, stop=len(data), step=2)
array([1, 3, 5, 7, 9])
>>> data[:]  # slice(start=0, stop=len(data), step=1)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

5.2.3. 1-dimensional Array

>>> import numpy as np

1-dimensional Array:

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

5.2.4. 2-dimensional Array

>>> import numpy as np

Rows:

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

Columns:

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

Rows and Columns:

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

5.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 Slice 1
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3

# %% English
# 1. Print inner 2x2 elements
# 2. Run doctests - all must succeed

# %% Polish
# 1. Wybierz wewnętrzne 2x2 elementy
# 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([[8, 4],
       [5, 2]])
"""

import numpy as np


DATA = np.array([
    [2, 8, 1, 5],
    [8, 8, 4, 4],
    [5, 5, 2, 5],
    [1, 0, 6, 0],
])


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 Slice 2
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3

# %% English
# 1. Print inner 4x4 elements
# 2. Inner matrix is exactly in the middle of outer
# 3. Run doctests - all must succeed

# %% Polish
# 1. Wypisz środkowe 4x4 elementy
# 2. Środkowa macierz jest dokładnie w środku większej
# 3. 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([[2, 0, 7, 5],
       [1, 2, 9, 1],
       [8, 8, 8, 2],
       [4, 3, 6, 9]])
"""

import numpy as np

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

result = ...


../../_images/random-inner-sum1.png

Figure 5.13. Inner 4x4 elements