What is numpy.logical_not() in Python?

Overview

logical_not() is used to compute the logical NOT of boolean argument values. The logical NOT performs a unary operation that inverts the input signal or values. Here, we have a truth table for the NOT gate.

Truth table

Syntax


numpy.logical_not(x,
/,
out=None,
*,
where=True,
casting='same_kind',
order='K',
dtype=None,
subok=True)

Parameters

  • x: It is an array-like object to which logical not will be applied.
  • out: It is a memory segment used to store results. It can be a simple ndarray, a tuple of ndarray, or None.
  • where: When a location becomes True, the out array is set to ufunc. In this case, ufunc is used to return program output.
  • casting: Its default is same_kind, which means object casting will be the same as float64 and float32.
  • **kwargs: These are the additional keyword arguments.

Return value

It either returns a boolean, scaler, or ndarray of boolean values.

Code example

In the given code snippet, we'll calculate the logical NOT on condition, scaler value, and logical array:

# importing numpy library as alias np
import numpy as np
# generating an array from 0 to 5
x = np.arange(5)
# calculate logical NOT on x < 3
print(np.logical_not(x<3))
# calculating NOT on 10
print(np.logical_not(10))
# calculating NOT on numpy array
print(np.logical_not([True, False, 0, 1, 10, 20, 30]))

    Free Resources