np.logical_not: What is Numpy logical_not() in Python
Numpy logical_not() method computes the truth value of NOT x element-wise.
np.logical_not
The np.logical_not() is a numpy library function that calculates the result of NOT ai for every element of an array 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)
- arr1: Input array_like containing elements ai.
- out: (ndarray, None, or tuple of ndarray) [Optional parameter] It specifies an alternate output array in which the resulting product is placed. This must have the same or broadcastable shape as the expected output.
- where: (array_like) [Optional parameter] Where True, the positions where the operator is applied. Where False, these are the positions to be left alone in the output array.
- 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:
- If the input array is blank, this method returns an empty array.
- This method can print the truth table of the logical, not operator.
- 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)
Output
[]
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)
Output
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, anyone who scores a zero must be detained. Then, given the students’ marks 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]