AppDividend
Latest Code Tutorials

# Numpy logical_not Example | np logical_not() in Python

Numpy logical_not() function calculates the result of NOT ai, for every element ai of array1 and returns the result in the form of an array. The logical_not() function computes the truth value of NOT arr element-wise.

### Syntax

```numpy.logical_not(arr1, out=None, where=True, dtype=None)
```

### Parameter(s)

1. arr1: Input array_like containing elements ai.
2. out: (ndarray, None, or tuple of ndarray) [Optional parameter] It specifies an alternate output array in which the resulting product is to be placed. This must have the same or broadcastable shape as the expected output.
3. where: (array_like) [Optional parameter] Where True, these are the positions where the operator is to be applied. Where False, these are the positions to be left alone in the output array.
4. dtype: [Optional parameter] It specifies the type of the returned array.

### Return Value

The element-wise logical NOT result in the form of an array.

See the following figure.

#### Note:

1. If the input array is blank, then this method returns an empty array.
2. This method can be used to print the truth table of the logical not operator.
3. This method can be used with complex numbers as well.

Consider the following examples:

#### Example 1

The following example demonstrates the use of this method and establishes the truth table for the logical not operator.

```import numpy as np

arr1 = [0, 1]
arr2 = np.logical_not(arr1)
print(arr2)

arr3 = [False, True]
arr4 = np.logical_not(arr3)
print(arr4)
```

#### Output

```[ True False]
[ True False]```

#### Example 2

The following example demonstrates the case where an array element is a complex number.

```import numpy as np

arr1 = [3+4j, 0]
arr2 = np.logical_not(arr1)
print(arr2)
```

#### Output

`[False  True]`

#### Example 3

The following example demonstrates the case where an empty array is passed.

```import numpy as np

arr1 = []
arr2 = np.logical_not(arr1)
print(arr2)
```

```[]
```

#### Example 4

The following example demonstrates the use of the where parameter.

```import numpy as np

arr1 = [0, 1, 0, 1, 1]
arr2 = np.logical_not(arr1, where=[True, False, True, False, True])
print(arr2)
```

#### Output

`[ True False  True False False]`

#### Example 5

The following example demonstrates the case where dtype is to specify the data type of the elements.

```import numpy as np

arr1 = [0, 0, 1, 1]
arr2 = np.logical_not(arr1, dtype=np.bool)
print(arr2.dtype == np.bool)
print(arr2.dtype == np.int)
```

```True
False```

#### Example 6

The following example demonstrates the application of this method in a simple programming context.

In a test with a maximum of 5 marks to be scored, anyone who scores a zero has to be detained. Given the marks of the students in the form of an array, find which students are detained.

```import numpy as np

n = int(input("Count: "))
numbers = []

for i in range(n):
numbers.append(int(input()))

detained_arr = np.logical_not(numbers)

print("Detained: ", detained_arr)
```

#### Output

```Count: 5
0
1
2
0
1
Detained:  [ True False False  True False]

Test Case 2:
Count: 4
0
0
0
0
Detained:  [ True  True  True  True]```