Home » Python Numpy logical operators

Python Numpy logical operators

Java SE 11 Programmer I [1Z0-815] Practice Tests
Spring Framework Basics Video Course
Java SE 11 Programmer II [1Z0-816] Practice Tests
1 Year Subscription
Oracle Java Certification
Java SE 11 Developer (Upgrade) [1Z0-817]

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.

This is the list

logical_and(x1, x2, /[, out, where, …]) Compute the truth value of x1 AND x2 element-wise.
logical_or(x1, x2, /[, out, where, casting, …]) Compute the truth value of x1 OR x2 element-wise.
logical_not(x, /[, out, where, casting, …]) Compute the truth value of NOT x element-wise.
logical_xor(x1, x2, /[, out, where, …]) Compute the truth value of x1 XOR x2, element-wise.

Examples

Numpy logical.and operator

This is a basic example showing logical.and

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))

This is the result of running this

>>> %Run numpylogical.py
---logical_and operator Example---
True logical_and True   =  True
True logical_and False  =  False
False logical_and True  =  False
False logical_and False =  False

Numpy logical.or operator

This is a basic example showing logical.or

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))

This is the result of running this

>>> %Run numpylogicalor.py
---logical_or operator Example---
True logical_or True   =  True
True logical_or False  =  True
False logical_or True  =  True
False logical_or False =  False

Numpy logical_not operator

This is a basic example showing the Numpy logical_not function

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))

This is the result of running this

>>> %Run numpylogicalnot.py
---logical_not operator Example---
logical_not True   =  False
logical_not False  =  True

Numpy logical_xor operator

This is a basic example showing the Numpy logical_xor function.

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))

This is the result of running this

>>> %Run numpylogicalxor.py
---logical_xor operator Example---
True logical_xor True   =  False
True logical_xor False  =  True
False logical_xor True  =  True
False logical_xor False =  False

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More