Open In App

Python Logical Operators

Last Updated : 06 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 look at some Python logical operators programs, to completely grasp the concept.

Logical Operators in Python

In Python, Logical operators are used on conditional statements (either True or False). They perform Logical AND, Logical OR, and Logical NOT operations.

OperatorDescriptionSyntaxExample
andReturns True if both the operands are truex and yx>7 and x>10
orReturns True if either of the operands is truex or yx<7 or x>15
notReturns True if the operand is falsenot xnot(x>7 and x> 10)

Truth Table for Logical Operators in Python

Truth Table for Python Logical Operators

AND Operator in Python

The Boolean AND operator returns True if both the operands are True else it returns False. AND Operator in Python

Logical AND operator in Python Examples

Let’s look at some Python AND operator programs, and understand the workings of AND operator.

Example 1: The code initializes variables a, b, and c, then checks if a and b are greater than 0, and prints “The numbers are greater than 0” if true; it also checks if all three variables are greater than 0, printing the same message, otherwise, it prints “At least one number is not greater than 0”.

Python
a = 10
b = 10
c = -10
if a > 0 and b > 0:
    print("The numbers are greater than 0")
if a > 0 and b > 0 and c > 0:
    print("The numbers are greater than 0")
else:
    print("Atleast one number is not greater than 0")

Output

The numbers are greater than 0
Atleast one number is not greater than 0

Example 2: The code checks if all variables a, b, and c evaluate to True, printing a message accordingly.

Python
a = 10
b = 12
c = 0
if a and b and c:
    print("All the numbers have boolean value as True")
else:
    print("Atleast one number has boolean value as False")

Output

Atleast one number has boolean value as False

Note: If the first expression is evaluated to be false while using the AND operator, then the further expressions are not evaluated.

Python OR Operator

The Boolean OR operator returns True if either of the operands is True.

Python OR Operator

Logical OR operator in Python Examples

Let’s look at some Python OR operator program to understand it’s workings 

Example 1: The code checks if either ‘a’ or ‘b’ is greater than 0 and prints a corresponding message, then checks if either ‘b’ or ‘c’ is greater than 0 and prints a message accordingly.

Python
a = 10
b = -10
c = 0
if a > 0 or b > 0:
    print("Either of the number is greater than 0")
else:
    print("No number is greater than 0")
if b > 0 or c > 0:
    print("Either of the number is greater than 0")
else:
    print("No number is greater than 0")

Output

Either of the number is greater than 0
No number is greater than 0

Example 2: The code checks if any of the variables a, b, or c has a boolean value as True; if so, it prints “At least one number has boolean value as True”, otherwise, it prints “All the numbers have boolean value as False”.

Python
a = 10
b = 12
c = 0
if a or b or c:
    print("Atleast one number has boolean value as True")
else:
    print("All the numbers have boolean value as False")

Output

Atleast one number has boolean value as True

Note: If the first expression is evaluated to be True while using or operator, then the further expressions are not evaluated.

Python NOT Operator

The Boolean NOT operator works with a single boolean value. If the boolean value is True it returns False and vice-versa.

Python NOT Operator

Logical NOT Operator in Python Examples

The code checks if a is divisible by either 3 or 5, otherwise, it prints a message indicating that it is not. Let’s look at this Python NOT operator program to understand its working.

Python
a = 10

if not a:
    print("Boolean value of a is True")
if not (a % 3 == 0 or a % 5 == 0):
    print("10 is not divisible by either 3 or 5")
else:
    print("10 is divisible by either 3 or 5")

Output

10 is divisible by either 3 or 5

Order of Precedence of Logical Operators

In the case of multiple operators, Python always evaluates the expression from left to right. We can verify Python logical operators precedence by the below example. 

Python
def order(x):
    print("Method called for value:", x)
    return True if x > 0 else False


a = order
b = order
c = order
if a(-1) or b(5) or c(10):
    print("Atleast one of the number is positive")

Output

Method called for value: -1
Method called for value: 5
Atleast one of the number is positive


Previous Article
Next Article

Similar Reads

Python 3 - Logical Operators
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
4 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