Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python Numpy logical operators

by suresh

The Python Numpy logical operators and logical functions are to compute truth value using the Truth table, i.,e Boolean True or false. 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.

The following examples helps you understand these Python Numpy logical functions and logical operators.

Python numpy logical_and, and operator

The below shown example show the Python numpy logical_and function and Numpy logical and operator Truth table.

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))
 
print('\n---and operator Example---')
print('True logical and True   = ', (True and True))
print('True logical and False  = ', (True and False))
print('False logical and True  = ', (False and True))
print('False logical and False = ', (False and False))
Python Numpy logical and Operator 1

In this example, we declared a single dimension, two dimensional, and three-dimensional arrays of random values. To generate random arrays, we used Python randn and randint. Next, testing each array element against the given condition to compute the truth value using Python numpy logical_and function.

  • np.logical_and(x > 3, x < 10) – returns True, if values in x are greater than 3 and less than 10 otherwise, False.
  • np.logical_and(y > 0,  y < 0.5) – if values in y are greater than 0 and less than 0.5, then True else False.
  • np.logical_and(z > 5,  z < 15) – True, if z is greater than 5 and less than 15
import numpy as np
 
x = np.array([5, 0, 8, 2, 22, 7, 4, 11, 9, 1, 6])
print(x)
print('---logical_and result  of an array---\n', np.logical_and(x > 3, x < 10))
print()
 
y = np.random.randn(3, 6)
print(y)
print('\n---logical_and result of Two Dimensional Array--- \n',
      np.logical_and(y > 0,  y < 0.5))
print()
 
z = np.random.randint(1, 20, size = (2, 3, 5))
print(z)
print('\n---logical_and result of Three Dimensional Array---\n',
      np.logical_and(z > 5,  z < 15))
Python Numpy logical and Operator 2

Python numpy logical_or, or operator

Let us see the Python numpy logical_or function and logical or operator Truth table.

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))
 
print('\n---or operator Example---')
print('True logical or True   = ', (True or True))
print('True logical or False  = ', (True or False))
print('False logical or True  = ', (False or True))
print('False logical or False = ', (False or False))
Python Numpy logical or Operator 1

We use the Python numpy logical_or function on 1D, 2D, and three-dimensional arrays.

  • np.logical_or(x > 8, x < 3) – returns True, if elements in Numpy x are either greater than 8 or less than 3 otherwise, False.
  • np.logical_or(y < 0,  y > 1) – if elements in y are either less than 0 or greater than 1, then True else False.
  • np.logical_or(z < 10,  z > 15) – True, if z elements are either less than 10 or greater than 15
import numpy as np
 
x = np.array([5, 0, 8, 2, 22, 7, 4, 11, 9, 1, 6])
print(x)
print('---logical_or result  of an array---\n', np.logical_or(x > 8, x < 3))
print()
 
y = np.random.randn(3, 4)
print(y)
print('\n---logical_or result of Two Dimensional Array--- \n',
      np.logical_or(y < 0,  y > 1))
print()
 
z = np.random.randint(1, 20, size = (2, 3, 5))
print(z)
print('\n---logical_or result of Three Dimensional Array---\n',
      np.logical_or(z < 10,  z > 15))
Python Numpy logical or Operator 2

Python Numpy logical_not and not operator

The following example show the Python Numpy logical_not function and the Python logical not operator Truth table.

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))
 
print('\n---not operator Example---')
print('logical not True   = ', not(True))
print('logical not False  = ', not(False))
Python Numpy logical not Operator 1

Here, we use the Python numpy logical_not function on 1D, 2D, and 3D arrays. 

  • np.logical_not(x > 8) – returns True, if elements in x are not greater than 8 otherwise, False.
  • np.logical_not(y > 1) – if elements in y are not greater than 1, then True else False.
  • np.logical_not(z < 10) – True, if each element in z 3D array is not less than 10
import numpy as np
 
x = np.array([5, 0, 8, 2, 22, 7, 4, 11, 9, 1, 6])
print(x)
print('---logical_not result  of an array---\n', np.logical_not(x > 8))
print()
 
y = np.random.randn(3, 4)
print(y)
print('\n---logical_not result of Two Dimensional Array--- \n',
      np.logical_not(y > 1))
print()
 
z = np.random.randint(1, 20, size = (2, 3, 5))
print(z)
print('\n---logical_not result of Three Dimensional Array---\n',
      np.logical_not(z < 10))
Python Numpy logical not Operator 2

The Python numpy logical and, or and not operators have to use on single values. If you try to use numpy logical operators on multiple values, then it throws a ValueError.

import numpy as np
 
x = np.array([5, 0, 8, 2, 22, 7, 4, 11, 9, 1, 6])
print(x)
print('\nlogical and  = ', ((x > 3) and (x < 10)))
print('\nlogical or  = ', ((x > 3) or (x < 10)))
print('\nlogical not  = ', not(x > 6))
Python Numpy logical Operators

Python numpy logical_xor operator

It shows the Python numpy logical_xor function Truth table.

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))
Python Numpy logical xor Operator 1

It is another example of Python numpy logical_xor function. Here, we used this Python numpy logical_xor function on multi-dimensional arrays.

import numpy as np
 
x = np.array([5, 0, 8, 2, 22, 7, 4, 11, 9, 1, 6])
print(x)
print('---logical_xor result  of an array---\n', np.logical_xor(x < 4, x > 8))
print()
 
y = np.random.randn(3, 4)
print(y)
print('\n---logical_xor result of Two Dimensional Array--- \n',
      np.logical_xor(y < 0,  y > 1))
print()
 
z = np.random.randint(1, 20, size = (2, 3, 5))
print(z)
print('\n---logical_xor result of Three Dimensional Array---\n',
      np.logical_xor(z < 8,  z > 15))
Python Numpy logical xor Operator 2

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy