Open In App

Python 3 – Logical Operators

Last Updated : 10 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Logical Operators are used to perform certain logical operations on values and variables. These are the special reserved keywords that carry out some logical computations. The value the operator operates on is known as Operand. In Python, they are used on conditional statements (either True or False), and as a result, they return boolean only (True or False). They are used to combine conditional statements

There are following logical operators supported by Python language:

  • Logical AND
  • Logical OR
  • Logical NOT

Logical AND

Logical operator AND returns True only if both the operands are True else it returns False. It is a binary operator, which means to return some value, it has to be operated between two operators (i.e, two operators are required)

Truth Table:

Operator A Operator B Logical AND result
True True True
True False False
False True False
False False False

Example 1:

Python3




   
a = 12
b = 26
c = 4
  
if a > b and a > c: 
    print("Number a is larger"
  
if b > a and b > c: 
    print("Number b is larger"
  
if c > a and c > b:    
    print("Number c is larger"


Output:

Number b is larger

Example 2:

Python3




a = 10
    
if (a == 0 and "Hello"):
    print("a has value zero(0)")
else:
    print("a is not equal to zero")


Output:

a is not equal to zero

If the first expression evaluated to be false while using and operator, then further expressions are not evaluated. Also, any string is always considered a true statement. In the above example, the first condition is false and hence it will not check the second condition and hence, it will not check for another condition and it will go to else statement.

Logical OR

The logical operator OR returns False only if both the operands are False else it returns True. It is a binary operator, which means to return some value, it has to be operated between two operators (i.e, two operators are required)

Truth Table:

Operator A Operator B Logical OR Result
True True True
True False True
False True True
False False False

Example 1:

Python3




a = 10
b = -5
  
if a < 0 or b < 0:
  print("Their product will be negative")
else:
  print("Their product will be positive")


Output:

Their product will be negative

Example 2:

Python3




a = 10
  
if (a == 0 or "GeeksForGeeks"):
  print("Is Awesome")
else:
  ("Try Again!")


Output:

Is Awesome

Here, in the OR Logical operator, even if the first expression evaluated is false while using and operator, then also the further expressions are evaluated. Also, any string is always considered a true statement. In the above example, the first statement is false but then too, it will evaluate the second statement because it returns False only if both the operands are False and since the string is considered as True statement, thus, it will be evaluated and the below print statement will be printed.

Logical NOT

Logical NOT operator works with the single boolean value and returns the value as True if the boolean value is False and vice-versa (that is the opposite of it).  It is a unary operator, which means to return some value, it has to be operated on one operator only. (i.e, only operator is required)

Truth Table:

Operator A Logical NOT Result
True False
False True

Example 1:

Python3




a = 10
  
if not a == 10:
  print ("a not equals 10")
else:
  print("a equals 10")


Output:

a equals 10

Here, a is equal to 10 the boolean a == 10 return the value True. Hence, the boolean not a == 10 will return the value as False and since the if condition is not satisfied, it will jump to else statement. 

Example 2:

Python3




a = 10
  
if not a%5 == 0:
  print("a is not perfectly divisible by 5")
else:
  print("a is perfectly divisible by 5")


Output:

a is perfectly divisible by 5


Similar Reads

Python Logical Operators
Python logical operators are used to combine conditional statements, allowing you to perform operations based on multiple conditions. These Python operators, alongside arithmetic operators, are special symbols used to carry out computations on values and variables. In this article, we will discuss logical operators in Python definition and also loo
5 min read
G-Fact 19 (Logical and Bitwise Not Operators on Boolean)
Most of the languages including C, C++, Java, and Python provide a boolean type that can be either set to False or True. In this article, we will see about logical and bitwise not operators on boolean. Logical Not (or !) Operator in PythonIn Python, the logical not operator is used to invert the truth value of a Boolean expression, returning True i
4 min read
Logical Operations on String in Python
For strings in python, boolean operators (and, or, not) work. Let us consider the two strings namely str1 and str2 and try boolean operators on them: C/C++ Code str1 = '' str2 = 'geeks' # repr is used to print the string along with the quotes # Returns str1 print(repr(str1 and str2)) # Returns str1 print(repr(str2 and str1)) # Returns str2 print(re
2 min read
PyQt5 QSpinBox - Getting Horizontal Logical DPI value
In this article we will see how we can get the horizontal resolution of the device in dots per inch(DPI) of the spin box. Logical dots per inch are used to convert font and user interface elements from point sizes to pixel sizes, and might be different from the physical dots per inch. Note : If the horizontal logical DPI doesn't equal to the physic
2 min read
PyQt5 QSpinBox - Getting Vertical Logical DPI value
In this article we will see how we can get the vertical resolution of the device in dots per inch(DPI) of the spin box. Logical dots per inch are used to convert font and user interface elements from point sizes to pixel sizes, and might be different from the physical dots per inch. Note : If the vertical logical DPI doesn't equal to the physical v
2 min read
NumPy Array - Logical Operations
Logical operations are used to find the logical relation between two arrays or lists or variables. We can perform logical operations using NumPy between two data. Below are the various logical operations we can perform on Numpy arrays: AND The numpy module supports the logical_and operator. It is used to relate between two variables. If two variabl
4 min read
What are the Logical Expressions in Sympy?
SymPy is a symbolic mathematics Python package. Its goal is to develop into a completely featured computer algebra system while keeping the code as basic as possible to make it understandable and extendable. The package is entirely written in python language. Logical expressions in sympy are expressed by using boolean functions. sympy.basic.boolean
7 min read
Compute element-wise logical AND, OR and NOT of tensors in PyTorch
In this article, we are going to see how to compute element-wise logical AND, OR, and NOT of given tensors in PyTorch. We can compute this by using the torch.logical_and(), torch.logical_or(), and torch.logical_not() methods. Let's discuss all of them one by one. Compute element-wise with logical AND torch.logical_and() - This method is used to com
4 min read
What is new in Data Interpretation and Logical Reasoning in CAT 2019 ?
The CAT exam is a computer-based exam in India for admission into the MBA programs of various management institutes of the country such as IIM’s, FMS, DMS, , etc. CAT is organized by one of the IIM’s on a rotational basis and consequently, it is notorious for changing the exam schedule or pattern on a regular basis. Currently, CAT is divided into t
6 min read
Division Operators in Python
Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. Division Operators in PythonThere are two types of division operators: Float divisionInteger division( Floor division)When an integer is divided, the
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg