In this blog, we will cover 3 logical operators in Python: AND, OR, and NOT. Python logical operators are used to evaluate the values to be either true or false. These are primarily used for evaluating logical operators with two or more conditions.
Table of Contents
Logical operators in Python are mainly used for conditional statements. There are three types of logical operators in Python: AND, OR, and NOT. These take two or more conditional statements and, after evaluation, return either true or false.
Let’s learn the operators, syntax, and examples in detail:
Operator Description Syntax Example AND Returns true if both the operands are true, otherwise false. X and Y 15 >10 and 2 < 5 OR Returns true if one of the operands is true, otherwise returns false X or Y 2 < 5 or 3>4 NOT It returns true if the operand is false, otherwise false. not X not( 4> 2)Example:
x, y, z = False, True, Falseif x and y:
print("Hello")if y or z:
print("Hii")if not z:
print("Namaste")
Output:
Hii
Namaste
Explanation: Here x, y, and z are three variables, and inside the if condition, x and y are both false, so ‘Hello’ is not printed. Inside the 2nd if, y is true, so ‘Hi’ is printed, and in the 3rd condition, not of z is equal to true. It will print ‘Namaste’.
Truth Table for Logical Operators in PythonHere is the following truth table of Logical Operators in Python:
Master Python for the Real World!
Industry-Ready Training for Future-Ready Professionals. Learn from Experts, Build Projects, and Advance Your Career!
AND Operator in PythonAND operator in Python is used to evaluate the value of two or more conditions. It returns true if both the statements are true and false if one of the conditions is false.
Logical AND operator ExamplesCode:
x = 10
y = 20
z = 30if x > y and y > z:
print("Hello")if x < y and y < z:
print("Hii")if x > z:
print("Namaste")
else:
print("Intellipaat")
Output:
Hii
Intellipaat
Python OR OperatorPython OR Operator is also used to evaluate conditional statements; it returns true if one of the statements is true and false if all the conditions return false.
Logical OR operator ExampleCode:
x = 10
y = 20
z = 30if x > y or y > z:
print("Hello")
else:
print("Bye")if x < y or y < z:
print("Hii")
else:
print("Good Bye!!")
Output:
Bye
Hii
Python NOT OperatorPython NOT operator takes one operand, and if the value of the operand is true, it returns false, otherwise, it returns true.
Logical NOT Operator examples
Code:
x = 10if not x:
print("Hii")
else:
print("Bye!!")
Example:
Bye!!
Order of Precedence of Logical OperatorsIf an expression contains two or more conditional statements, then the order of precedence of logical operators is as follows:
And if all the conditions consist of the same logical operators, then the precedence of the logical operator is from left to right.
Let’s verify this with the below example:
Code:
a = False
b = True
c = Trueresult = a or b and not c
print(result)
Output:
False
Explanation:
Master Python for Industry-Level Applications – Free Online Course!
Learn Python from scratch and gain skills to tackle real-world projects with Intellipaat Academy.
Real-life Examples of Python Logical OperatorsHere are some real-life examples of Python Logical Operators:
Code:
# Student information
attendance = 90
financial_income = 132000# Scholarship eligibility conditions
if attendance >= 75 and financial_income <= 500000:
print("The student is eligible for the scholarship.")
else:
print("The student is not eligible for the scholarship.")
Output:
The student is eligible for the scholarship.
Code:
age = 65
is_student = Falseif age >= 60 or is_student:
print("Eligible for movie discount.")
else:
print("Not eligible for movie discount.")
Output:
Eligible for movie discount.
Code:
overdue_books = 3
has_paid_fines = Falseif overdue_books > 2 and not has_paid_fines:
print("Late fee applies, and additional fine due to unpaid fees.")
else:
print("No late fee.")
Output:
Late fee applies, and additional fine due to unpaid fees.
To check the maximum number of three numbers, we have to use the AND operator.
Code:
# Find the maximum of three numbersa = 8
b = 4
c = 7if a >= b and a >= c:
print("The maximum number is: ", a)
elif b >= a and b >= c:
print("The maximum number is: ", b)
else:
print("The maximum number is: ", c)
Output:
The maximum number is: 8
Get 100% Hike!
Master Most in Demand Skills Now!
ConclusionIn this blog, we learned the three different types of logical operators AND, OR, and NOT in Python. We also learned the precedence order of these logical operators with real-life examples. If you want to learn more about Python, you can refer to our Python Course
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4