Numpy Logical Functions - OR | AND | NOT | XOR with examples
In this numpy tutorial, we will discuss about different logical functions performed on the numpy array:
-
numpy logical or & np.logical_or examples,
-
numpy logical and & numpy logical and example,
-
numpy logical xor & numpy.logical xor example,
-
numpy logical not
Introduction to Numpy
numpy stands for numeric python which is used to perform mathematical operations on arrays.
It is a module in which we have to import from the python.
Syntax to import:
import numpy
We can also use alias for the module
For example,
import numpy as np
We can directly use np to call the numpy module.
Numpy Array
An array is an one dimensional data structure used to store single data type data.
I.E It will only store all integer data or all string type data.or all float type data.
We can create an numpy array by using array() function.
Syntax:
numpy.array(elements)
where, elements are the input data elements.
numpy logical or → logical_or()
numpy logical or is performed on the two numpy arrays in which element wise computation is performed.
So, it will check the two elements from the both the arrays and return the boolean value - True/False.
Let's see the Internal working of this function - logical_or().
Scenario:
array1=[1,2,3,0]
array2=[0,1,2,0]
Working:
It will return True if any element is greater than 0, otherwise False
[ 1 logical_or 0] - True
[ 2 logical_or 1] - True
[ 3 logical_or 2] - True
[ 0 logical_or 0] - False
Result:
[ True True True False]
Example: np.logical_or examples
In this numpy logical or example, we are creating two numpy arrays with 5 integer elements and perform logical_or() function.
#importing numpy module
import numpy
#creating array1
arraydata1=numpy.array([1,2,3,4,0])
#creating array2
arraydata2=numpy.array([1,2,10,4,0])
#display two arrays
print(arraydata1,arraydata2)
#perform logical_or() logical operation
print(numpy.logical_or(arraydata1,arraydata2))
Output: numpy logical or
[1 2 3 4 0] [ 1 2 10 4 0]
[ True True True True False]
numpy logical and → logical_and()
numpy logical and is performed on the two numpy arrays in which element wise computation is performed.
So, it will check the two elements from the both the arrays and return the boolean value - True/False.
Let's see the Internal working of this function - numpy logical_and().
Scenario:
array1=[1,2,3,0]
array2=[0,1,2,0]
Working:
It will return True if both the elements are greater than 0, otherwise False
[ 1 logical_or 0] - False
[ 2 logical_or 1] - True
[ 3 logical_or 2] - True
[ 0 logical_or 0] - False
Result:
[ False True True False]
Example: numpy logical and example
In this example, we are creating two numpy arrays with 5 integer elements and perform logical_and() function.
#importing numpy module
import numpy
#creating array1
arraydata1=numpy.array([1,2,3,4,0])
#creating array2
arraydata2=numpy.array([1,2,10,4,0])
#display two arrays
print(arraydata1,arraydata2)
#perform logical_and() logical operation
print(numpy.logical_and(arraydata1,arraydata2))
Output: numpy logical and
[1 2 3 4 0] [ 1 2 10 4 0]
[ True True True True False]
Lets see about numpy logical xor.
numpy logical xor → logical_xor()
numpy logical xor is performed on the two numpy arrays in which element wise computation is performed.
So, it will check the two elements from the both the arrays and return the boolean value - True/False.
Let's see the Internal working of this function.
Scenario:
array1=[1,2,3,0]
array2=[0,1,2,0]
Working:
It will return True if one element is greater than 0 and other is 0, otherwise False
[ 1 logical_or 0] - True
[ 2 logical_or 1] - False
[ 3 logical_or 2] - False
[ 0 logical_or 0] - False
Result:
[ True False False False]
Example: numpy.logical xor example
In this numpy.logical xor example, we are creating two numpy arrays with 5 integer elements and perform logical_xor() function.
#importing numpy module
import numpy
#creating array1
arraydata1=numpy.array([0,2,3,4,0])
#creating array2
arraydata2=numpy.array([1,2,10,4,0])
#display two arrays
print(arraydata1,arraydata2)
#perform logical_xor() logical operation
print(numpy.logical_xor(arraydata1,arraydata2))
Output: numpy.logical xor example
[0 2 3 4 0] [ 1 2 10 4 0]
[ True False False False False]
Lets learn about numpy logical not.
numpy logical not →logical_not()
numpy logical not is performed on the one numpy array in which element wise computation is performed.
So, it will check the each element in the array and return the boolean value - True/False.
Let's see the Internal working of this function - numpy logical not.
Scenario:
array=[1,2,3,0]
Working:
It will return True if the element is equal to 0, otherwise False
[ logical_not 1] - False
[ logical_not 2] - False
[ logical_not 3] - False
[ logical_not 0] - True
Result:
[ False False False True]
Example: numpy logical not operator
In this numpy logical not operator example, we are creating two numpy arrays with 5 integer elements and perform logical_not() function on two arrays separately.
#importing numpy module
import numpy
#creating array1
arraydata1=numpy.array([0,2,3,4,0])
#creating array2
arraydata2=numpy.array([1,2,10,4,0])
#display two arrays
print(arraydata1,arraydata2)
#perform logical_not() logical operation on first array
print(numpy.logical_not(arraydata1))
#perform logical_not() logical operation on second array
print(numpy.logical_not(arraydata2))
Output: numpy logical not operator
[0 2 3 4 0] [ 1 2 10 4 0]
[ True False False False True]
[False False False False True]
This concludes our session numpy logical or, np.logical_or examples, numpy logical and, numpy logical and example, numpy logical xor, numpy.logical xor example, numpy logical not, numpy logical not operator.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Feb 27,2023