# Python Numpy logical operators

The Python Numpy logical operators and logical functions are to compute truth value using the Truth table, i.,e Boolean True or false. Python numpy logical functions are logical_and, logical_or, logical_not, and logical_xor. Like any other programming, numpy has regular logical operators like and, or, not and xor. The following examples helps you understand these Python Numpy logical functions and logical operators.

## Python numpy logical_and, and operator

The below shown example show the Python numpy logical_and function and Numpy logical and operator Truth table.

```import numpy as np

print('---logical_and operator Example---')
print('True logical_and True   = ', np.logical_and(True, True))
print('True logical_and False  = ', np.logical_and(True, False))
print('False logical_and True  = ', np.logical_and(False, True))
print('False logical_and False = ', np.logical_and(False, False))

print('\n---and operator Example---')
print('True logical and True   = ', (True and True))
print('True logical and False  = ', (True and False))
print('False logical and True  = ', (False and True))
print('False logical and False = ', (False and False))```

In this example, we declared a single dimension, two dimensional, and three-dimensional arrays of random values. To generate random arrays, we used Python randn and randint. Next, testing each array element against the given condition to compute the truth value using Python numpy logical_and function.

• np.logical_and(x > 3, x < 10) – returns True, if values in x are greater than 3 and less than 10 otherwise, False.
• np.logical_and(y > 0,  y < 0.5) – if values in y are greater than 0 and less than 0.5, then True else False.
• np.logical_and(z > 5,  z < 15) – True, if z is greater than 5 and less than 15
```import numpy as np

x = np.array([5, 0, 8, 2, 22, 7, 4, 11, 9, 1, 6])
print(x)
print('---logical_and result  of an array---\n', np.logical_and(x > 3, x < 10))
print()

y = np.random.randn(3, 6)
print(y)
print('\n---logical_and result of Two Dimensional Array--- \n',
np.logical_and(y > 0,  y < 0.5))
print()

z = np.random.randint(1, 20, size = (2, 3, 5))
print(z)
print('\n---logical_and result of Three Dimensional Array---\n',
np.logical_and(z > 5,  z < 15))```

## Python numpy logical_or, or operator

Let us see the Python numpy logical_or function and logical or operator Truth table.

```import numpy as np

print('---logical_or operator Example---')
print('True logical_or True   = ', np.logical_or(True, True))
print('True logical_or False  = ', np.logical_or(True, False))
print('False logical_or True  = ', np.logical_or(False, True))
print('False logical_or False = ', np.logical_or(False, False))

print('\n---or operator Example---')
print('True logical or True   = ', (True or True))
print('True logical or False  = ', (True or False))
print('False logical or True  = ', (False or True))
print('False logical or False = ', (False or False))```

We use the Python numpy logical_or function on 1D, 2D, and three-dimensional arrays.

• np.logical_or(x > 8, x < 3) – returns True, if elements in Numpy x are either greater than 8 or less than 3 otherwise, False.
• np.logical_or(y < 0,  y > 1) – if elements in y are either less than 0 or greater than 1, then True else False.
• np.logical_or(z < 10,  z > 15) – True, if z elements are either less than 10 or greater than 15
```import numpy as np

x = np.array([5, 0, 8, 2, 22, 7, 4, 11, 9, 1, 6])
print(x)
print('---logical_or result  of an array---\n', np.logical_or(x > 8, x < 3))
print()

y = np.random.randn(3, 4)
print(y)
print('\n---logical_or result of Two Dimensional Array--- \n',
np.logical_or(y < 0,  y > 1))
print()

z = np.random.randint(1, 20, size = (2, 3, 5))
print(z)
print('\n---logical_or result of Three Dimensional Array---\n',
np.logical_or(z < 10,  z > 15))```

## Python Numpy logical_not and not operator

The following example show the Python Numpy logical_not function and the Python logical not operator Truth table.

```import numpy as np

print('---logical_not operator Example---')
print('logical_not True   = ', np.logical_not(True))
print('logical_not False  = ', np.logical_not(False))

print('\n---not operator Example---')
print('logical not True   = ', not(True))
print('logical not False  = ', not(False))```

Here, we use the Python numpy logical_not function on 1D, 2D, and 3D arrays.

• np.logical_not(x > 8) – returns True, if elements in x are not greater than 8 otherwise, False.
• np.logical_not(y > 1) – if elements in y are not greater than 1, then True else False.
• np.logical_not(z < 10) – True, if each element in z 3D array is not less than 10
```import numpy as np

x = np.array([5, 0, 8, 2, 22, 7, 4, 11, 9, 1, 6])
print(x)
print('---logical_not result  of an array---\n', np.logical_not(x > 8))
print()

y = np.random.randn(3, 4)
print(y)
print('\n---logical_not result of Two Dimensional Array--- \n',
np.logical_not(y > 1))
print()

z = np.random.randint(1, 20, size = (2, 3, 5))
print(z)
print('\n---logical_not result of Three Dimensional Array---\n',
np.logical_not(z < 10))```

The Python numpy logical and, or and not operators have to use on single values. If you try to use numpy logical operators on multiple values, then it throws a ValueError.

```import numpy as np

x = np.array([5, 0, 8, 2, 22, 7, 4, 11, 9, 1, 6])
print(x)
print('\nlogical and  = ', ((x > 3) and (x < 10)))
print('\nlogical or  = ', ((x > 3) or (x < 10)))
print('\nlogical not  = ', not(x > 6))```

## Python numpy logical_xor operator

It shows the Python numpy logical_xor function Truth table.

```import numpy as np

print('---logical_xor operator Example---')
print('True logical_xor True   = ', np.logical_xor(True, True))
print('True logical_xor False  = ', np.logical_xor(True, False))
print('False logical_xor True  = ', np.logical_xor(False, True))
print('False logical_xor False = ', np.logical_xor(False, False))```

It is another example of Python numpy logical_xor function. Here, we used this Python numpy logical_xor function on multi-dimensional arrays.

```import numpy as np

x = np.array([5, 0, 8, 2, 22, 7, 4, 11, 9, 1, 6])
print(x)
print('---logical_xor result  of an array---\n', np.logical_xor(x < 4, x > 8))
print()

y = np.random.randn(3, 4)
print(y)
print('\n---logical_xor result of Two Dimensional Array--- \n',
np.logical_xor(y < 0,  y > 1))
print()

z = np.random.randint(1, 20, size = (2, 3, 5))
print(z)
print('\n---logical_xor result of Three Dimensional Array---\n',
np.logical_xor(z < 8,  z > 15))```