What are the 3 logical operators in Python?

What are the 3 logical operators in Python?

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

What are Python Logical Operators?

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:

OperatorDescriptionSyntaxExample
ANDReturns true if both the operands are true, otherwise false.X and Y15 >10 and 2 < 5
ORReturns true if one of the operands is true, otherwise returns falseX or Y2 < 5 or 3>4
NOTIt returns true if the operand is false, otherwise false.not Xnot( 4> 2)

Example:

x, y, z = False, True, False

if 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 Python

Here 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!
quiz-icon

AND Operator in Python

AND 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 Examples

Code:

x = 10
y = 20
z = 30

if 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 Operator

Python 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 Example

Code: 

x = 10
y = 20
z = 30

if 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 Operator

Python 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 = 10

if not x:
print("Hii")
else:
print("Bye!!")

Example:

Bye!!

Order of Precedence of Logical Operators

If an expression contains two or more conditional statements, then the order of precedence of logical operators is as follows:

  • NOT
  • AND
  • OR

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 = True

result = a or b and not c

print(result)

Output:

False

Explanation: 

  • NOT has the highest precedence, so not c is evaluated first:
    • not c is false now b and c mean true and false
  • AND operator will evaluated first, then true and false become false
  • Now, the OR operator will be evaluated, and then a or false, which means False or False, returns False, so the output is False.
Master Python for Industry-Level Applications – Free Online Course!
Learn Python from scratch and gain skills to tackle real-world projects with Intellipaat Academy.
quiz-icon

Real-life Examples of Python Logical Operators

Here are some real-life examples of Python Logical Operators:

  1. Check if the student is eligible for a scholarship or not. If his attendance is greater than 75 % and his family income is less than 5 lakh per annum, then he will be eligible for the scholarship.

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.

  1. Check whether a person is eligible for the movie discount or not, A person is eligible for the discount if a person is either a senior citizen or a student.

Code:

age = 65
is_student = False

if age >= 60 or is_student:
print("Eligible for movie discount.")
else:
print("Not eligible for movie discount.")

Output:

Eligible for movie discount.

  1. Check whether a student is eligible to borrow a book from the library or not.

Code:

overdue_books = 3
has_paid_fines = False

if 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.

  1. Check the maximum of three numbers.

To check the maximum number of three numbers, we have to use the AND operator.

Code:

# Find the maximum of three numbers

a = 8
b = 4
c = 7

if 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!

Conclusion

In 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

About the Author

Principal Data Scientist

Meet Akash, a Principal Data Scientist with expertise in advanced analytics, machine learning, and AI-driven solutions. With a master’s degree from IIT Kanpur, Aakash combines technical knowledge with industry insights to deliver impactful, scalable models for complex business challenges.

EPGC Data Science Artificial Intelligence