5.5. Indexing Logic

5.5.1. SetUp

>>> import numpy as np

5.5.2. Contains

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> 2 in a
True
>>>
>>> 0 in a
False
>>>
>>> [1, 2, 3] in a
True

5.5.3. Is In

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> b = np.array([1, 5, 9])
>>>
>>> np.isin(a, b)
array([[ True, False, False],
       [False,  True, False]])

5.5.4. Scalar Comparison

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> a == 2
array([[False,  True, False],
       [False, False, False]])
>>>
>>> a != 2
array([[ True, False,  True],
       [ True,  True,  True]])
>>>
>>> a > 2
array([[False, False,  True],
       [ True,  True,  True]])
>>>
>>> a >= 2
array([[False,  True,  True],
       [ True,  True,  True]])
>>>
>>> a < 2
array([[ True, False, False],
       [False, False, False]])
>>>
>>> a <= 2
array([[ True,  True, False],
       [False, False, False]])

5.5.5. Broadcasting Comparison

>>> a = np.array([1, 2, 3])
>>> b = np.array([3, 2, 1])
>>>
>>> a == b
array([False,  True, False])
>>>
>>> a != b
array([ True, False,  True])
>>>
>>> a > b
array([False, False,  True])
>>>
>>> a >= b
array([False,  True,  True])
>>>
>>> a < b
array([ True, False, False])
>>>
>>> a <= b
array([ True,  True, False])

5.5.6. Any

>>> a = np.array([True, False, False])
>>>
>>> a.any()
np.True_
>>> a = np.array([[True, False, False],
...               [True, True, True]])
>>>
>>> a.any()
np.True_
>>>
>>> a.any(axis=0)
array([ True,  True,  True])
>>>
>>> a.any(axis=1)
array([ True,  True])

5.5.7. All

>>> a = np.array([True, False, False])
>>>
>>> a.all()
np.False_
>>> a = np.array([[True, False, False],
...               [True, True, True]])
>>>
>>> a.all()
np.False_
>>>
>>> a.all(axis=0)
array([ True, False, False])
>>>
>>> a.all(axis=1)
array([False,  True])

5.5.8. Logical NOT

  • np.logical_not(...)

  • ~(...)

>>> a = np.array([[True, False, False],
...               [True, True, True]])
>>>
>>> np.logical_not(a)
array([[False,  True,  True],
       [False, False, False]])
>>>
>>> ~a
array([[False,  True,  True],
       [False, False, False]])
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> np.logical_not(a > 2)
array([[ True,  True, False],
       [False, False, False]])
>>>
>>> ~(a > 2)
array([[ True,  True, False],
       [False, False, False]])

5.5.9. Logical AND

  • Meets first and second condition at the same time

  • np.logical_and(..., ...)

  • (...) & (...)

>>> a = np.array([True, False, False])
>>> b = np.array([True, True, False])
>>>
>>> np.logical_and(a, b)
array([ True, False, False])
>>>
>>> a & b
array([ True, False, False])
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> np.logical_and(a > 2, a < 5)
array([[False, False,  True],
       [ True, False, False]])
>>>
>>> (a > 2) & (a < 5)
array([[False, False,  True],
       [ True, False, False]])

5.5.10. Logical OR

  • Meets first or second condition at the same time

  • np.logical_or(..., ...)

  • (...) | (...)

>>> a = np.array([True, False, False])
>>> b = np.array([True, True, False])
>>>
>>> np.logical_or(a, b)
array([ True,  True, False])
>>>
>>> a | b
array([ True,  True, False])
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> np.logical_or(a < 2, a > 4)
array([[ True, False, False],
       [False,  True,  True]])
>>>
>>> (a < 2) | (a > 4)
array([[ True, False, False],
       [False,  True,  True]])

5.5.11. Logical XOR

  • Meets first or second condition, but not both at the same time

  • np.logical_xor(..., ...)

  • (...) ^ (...)

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> np.logical_xor(a < 2, a > 4)
array([[ True, False, False],
       [False,  True,  True]])
>>>
>>> (a < 2) ^ (a > 4)
array([[ True, False, False],
       [False,  True,  True]])

5.5.12. Good Practices

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>>
>>> (a < 2) & (a > 4) | (a == 3)
array([[False, False,  True],
       [False, False, False]])
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]])
>>>
>>> lower = (a > 2)
>>> upper = (a < 6)
>>> nine = (a == 9)
>>> range = lower & upper
>>>
>>> lower & upper
array([[False, False,  True],
       [ True,  True, False],
       [False, False, False]])
>>>
>>> range | nine
array([[False, False,  True],
       [ True,  True, False],
       [False, False,  True]])
>>>
>>> lower & upper | nine
array([[False, False,  True],
       [ True,  True, False],
       [False, False,  True]])

5.5.13. Assignments

# %% About
# - Name: Numpy Logic Even
# - Difficulty: easy
# - Lines: 3
# - Minutes: 5

# %% 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. Set random seed to zero
# 3. Check for even numbers of `DATA` which are less than 50 and save result to `data`
# 4. Check if all `data` matches this condition, result assign to `result_all`
# 5. Check if any `data` matches this condition, result assign to `result_any`
# 6. Run doctests - all must succeed

# %% Polish
# 1. Ustaw ziarno losowości na zero
# 3. Sprawdź parzyste elementy `DATA`, które są mniejsze od 50 i wynik zapisz do `data`
# 4. Sprawdź czy wszystkie `result` spełniają ten warunek, wynik zapisz do `result_all`
# 5. Sprawdź czy jakakolwiek `result` spełnia ten warunek, wynik zapisz do `result_any`
# 6. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert data is not Ellipsis, \
'Assign result to variable: `data`'
>>> assert type(data) is np.ndarray, \
'Variable `data` has invalid type, expected np.bool'

>>> assert result_a is not Ellipsis, \
'Assign result to variable: `result_a`'
>>> assert type(result_a) is np.bool, \
'Variable `result_a` has invalid type, expected np.bool'

>>> assert result_b is not Ellipsis, \
'Assign result to variable: `result_b`'
>>> assert type(result_b) is np.bool, \
'Variable `result_b` has invalid type, expected np.bool'

>>> data
array([ True, False, False, False, False, False, False, False,  True])

>>> result_a
np.False_

>>> result_b
np.True_
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports
import numpy as np

# %% Types
data: np.ndarray
result_a: np.ndarray
result_b: np.ndarray

# %% Data
np.random.seed(0)

DATA = np.random.randint(0, 100, size=9)

# %% Result
data = ...
result_a = ...
result_b = ...

# %% About
# - Name: Numpy Logic Isin
# - Difficulty: easy
# - Lines: 1
# - Minutes: 5

# %% 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. Check which elements from `a` are present in `b`
# 2. Result assign to `result`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Sprawdź, które elementy z `a` są obecne w `b`
# 2. Wynik przypisz do `result`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> 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([False, False,  True, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False,  True, False, False, False, False,
       False, False, False, False, False,  True, False, False, False,
        True, False, False, False, False])
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports
import numpy as np

# %% Types
result: np.ndarray

# %% Data
np.random.seed(0)

a = np.random.randint(0, 100, size=50)
b = 2 ** np.arange(7)

# %% Result
result = ...