Python Logical Operators
Learn how Python combines multiple conditions
using and, or, and
not to build smarter and more
flexible programs.
Learning Objectives
By the end of this lesson, you should be able to:
- Understand what logical operators are.
-
Use the
andoperator correctly. -
Use the
oroperator correctly. -
Use the
notoperator correctly. - Understand logical operator truth tables.
- Combine comparison and logical operators.
- Understand logical operator precedence.
- Apply logical operators to real-world problems.
What Are Logical Operators?
Logical operators are used to combine or modify conditions. They are particularly useful when a program needs to evaluate more than one condition at the same time.
Python provides three main logical operators:
and
Combines conditions
All conditions must be true
or
Combines conditions
At least one must be true
not
Reverses a result
True becomes False
They are commonly used with expressions
that produce True or
False.
The and Operator
The and operator returns
True only when both conditions are
true.
print(True and True)
print(True and False)
print(False and True)
print(False and False)
True False False False
Think of and as saying:
"Both conditions must be satisfied."
age = 25
print(age >= 18 and age <= 60)
True
Both comparisons are true, so the complete expression is true.
The or Operator
The or operator returns
True when at least one condition
is true.
print(True or True)
print(True or False)
print(False or True)
print(False or False)
True True True False
Think of or as saying:
"At least one condition must be satisfied."
day = "Saturday"
print(day == "Saturday" or day == "Sunday")
True
The not Operator
The not operator reverses a Boolean
result.
print(not True)
print(not False)
False True
It can also be applied to a comparison.
age = 15
print(not age >= 18)
True
The comparison age >= 18 is
False. Applying not
changes it to True.
Truth Tables
A truth table shows the result of a logical operation for every possible combination of Boolean values.
and Truth Table
or Truth Table
not Truth Table
Practical and Examples
Suppose a website requires a user to be at least 18 years old and have a valid membership.
age = 25
member = True
print(age >= 18 and member == True)
True
If either condition becomes false, the complete
and expression becomes false.
age = 16
member = True
print(age >= 18 and member == True)
False
Practical or Examples
Suppose a customer can receive a discount if they are either a premium member or their purchase exceeds a certain amount.
premium_member = False
purchase = 8000
print(premium_member == True or purchase >= 5000)
True
The first condition is false, but the second
condition is true. Because or needs
only one true condition, the final result is
True.
Practical not Examples
The not operator is useful when you
want to check the opposite of a condition.
logged_in = False
print(not logged_in)
True
Since logged_in is false,
not logged_in becomes true.
Combining Logical Operators
You can use more than one logical operator in the same expression.
age = 25
student = True
result = age >= 18 and student == True
print(result)
True
More complex conditions can also use parentheses to make the intended logic clear.
age = 25
student = True
employee = False
result = age >= 18 and (student == True or employee == True)
print(result)
True
Logical Operator Precedence
When multiple logical operators appear in the same expression, Python follows a specific order.
The logical precedence order is:
-
not -
and -
or
result = True or False and False
print(result)
True
Python evaluates False and False
first, producing False. It then
evaluates True or False, producing
True.
Parentheses make your intention explicit and make code easier for other developers to understand.
Logical + Comparison Operators
One of the most common patterns in Python is to combine comparison operators with logical operators.
age = 30
salary = 75000
eligible = age >= 18 and salary >= 50000
print(eligible)
True
This pattern will become extremely important when
you start writing if statements.
Using Variables with Logical Operators
Boolean variables can be combined directly with logical operators.
has_id = True
has_ticket = True
allowed = has_id and has_ticket
print(allowed)
True
A cleaner Python style is often to use Boolean
variables directly instead of writing comparisons
such as has_id == True.
has_id = True
has_ticket = True
allowed = has_id and has_ticket
print(allowed)
Logical Operators in Real Programs
Online Store Eligibility
Imagine an online store that gives free delivery when a customer is either a premium member or spends at least Rs. 5,000.
premium = False
cart_total = 6500
free_delivery = premium or cart_total >= 5000
print(free_delivery)
True
Student Eligibility
A student may qualify for a program if they meet both an age requirement and a marks requirement.
age = 21
marks = 78
eligible = age >= 18 and marks >= 60
print(eligible)
True
Login Verification
A system may require both a valid username and password.
username_correct = True
password_correct = True
login_successful = username_correct and password_correct
print(login_successful)
True
Common Mistakes
1. Confusing and with or
Remember:
-
andrequires every condition to be true. -
orrequires at least one condition to be true.
2. Using && instead of
and
Python uses English-style logical keywords.
age >= 18 and age <= 60
3. Forgetting parentheses in complex logic
Although Python has defined precedence rules, parentheses often make complicated expressions much easier to understand.
age >= 18 and (student or employee)
4. Comparing Boolean variables unnecessarily
Instead of:
if logged_in == True:
You can usually write:
if logged_in:
Practice Exercises
Try each exercise yourself before looking for a solution.
Exercise 1 — Using and
Create two Boolean variables:
has_id and
has_ticket.
Print whether both are true.
Exercise 2 — Using or
Create two variables representing Saturday and Sunday. Check whether a given day is either Saturday or Sunday.
Exercise 3 — Using not
Create a Boolean variable called
logged_in and print its opposite
using not.
Exercise 4 — Age and Marks
Check whether a student is at least 18 years old and has scored at least 50 marks.
Exercise 5 — Shopping Discount
A customer receives a discount if they are a premium member or their purchase amount is at least Rs. 10,000.
Exercise 6 — Range and Status
Check whether a person's age is between 18 and 60 and whether they are an active member. Both conditions must be true.
Mini Challenge — Student Eligibility Checker
Build a program that asks the user for their age, marks, and student status.
The student should be considered eligible when:
- Their age is at least 18.
- Their marks are at least 60.
- They are a registered student.
Use input(),
int(), comparison operators,
and the and operator.
You do not need an if
statement yet.
age = int(input("Enter your age: "))
marks = int(input("Enter your marks: "))
student = input("Are you a registered student? ")
is_student = student.lower() == "yes"
eligible = age >= 18 and marks >= 60 and is_student
print("Eligible:", eligible)
Modify the program so that the student can
qualify through an alternative condition,
such as being a premium scholarship member.
Use both and and or.
Quick Knowledge Check
1. Which logical operator requires all conditions to be true?
2. What is the result of
True and False?
3. What is the result of
False or True?
4. What does not True produce?
5. Which logical operator requires only one condition to be true?
6. What is Python's logical operator precedence from highest to lowest?
7. What is the result of
10 > 5 and 20 < 30?
Lesson Summary
Logical operators allow Python programs to combine multiple conditions and make more sophisticated decisions.
and
All conditions must be true
A and B
or
At least one must be true
A or B
not
Reverses a Boolean result
not A
Comparison operators let Python ask questions, while logical operators let Python combine those questions. Together, they form the foundation of complex decision-making.
In the next lesson, we will continue with another important group of operators: Membership and Identity Operators.
Ready to continue?
Complete this lesson after practicing
and, or, and
not.