Home Course Roadmap
Home Python Foundations Logical Operators
Lesson 12 · Operators

Python Logical Operators

Learn how Python combines multiple conditions using and, or, and not to build smarter and more flexible programs.

30–40 minutes Beginner Python Foundations
Reading Progress 0%
01
Before You Begin

Learning Objectives

By the end of this lesson, you should be able to:

  • Understand what logical operators are.
  • Use the and operator correctly.
  • Use the or operator correctly.
  • Use the not operator correctly.
  • Understand logical operator truth tables.
  • Combine comparison and logical operators.
  • Understand logical operator precedence.
  • Apply logical operators to real-world problems.
02
Core Concept

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:

Operator Purpose Basic Meaning
and Combines conditions All conditions must be true
or Combines conditions At least one must be true
not Reverses a result True becomes False
Logical operators work with Boolean logic

They are commonly used with expressions that produce True or False.

03
Operator 1

The and Operator

The and operator returns True only when both conditions are true.

Basic and example
print(True and True)
print(True and False)
print(False and True)
print(False and False)
Output
True
False
False
False

Think of and as saying: "Both conditions must be satisfied."

Using comparisons
age = 25

print(age >= 18 and age <= 60)
Output
True

Both comparisons are true, so the complete expression is true.

04
Operator 2

The or Operator

The or operator returns True when at least one condition is true.

Basic or example
print(True or True)
print(True or False)
print(False or True)
print(False or False)
Output
True
True
True
False

Think of or as saying: "At least one condition must be satisfied."

Multiple possibilities
day = "Saturday"

print(day == "Saturday" or day == "Sunday")
Output
True
05
Operator 3

The not Operator

The not operator reverses a Boolean result.

Reversing Boolean values
print(not True)
print(not False)
Output
False
True

It can also be applied to a comparison.

Using not with comparison
age = 15

print(not age >= 18)
Output
True

The comparison age >= 18 is False. Applying not changes it to True.

06
Logic

Truth Tables

A truth table shows the result of a logical operation for every possible combination of Boolean values.

and Truth Table

A B A and B
True True True
True False False
False True False
False False False

or Truth Table

A B A or B
True True True
True False True
False True True
False False False

not Truth Table

A not A
True False
False True
07
Practical Example

Practical and Examples

Suppose a website requires a user to be at least 18 years old and have a valid membership.

Two requirements
age = 25
member = True

print(age >= 18 and member == True)
Output
True

If either condition becomes false, the complete and expression becomes false.

One condition fails
age = 16
member = True

print(age >= 18 and member == True)
Output
False
08
Practical Example

Practical or Examples

Suppose a customer can receive a discount if they are either a premium member or their purchase exceeds a certain amount.

Discount eligibility
premium_member = False
purchase = 8000

print(premium_member == True or purchase >= 5000)
Output
True

The first condition is false, but the second condition is true. Because or needs only one true condition, the final result is True.

09
Practical Example

Practical not Examples

The not operator is useful when you want to check the opposite of a condition.

Account status
logged_in = False

print(not logged_in)
Output
True

Since logged_in is false, not logged_in becomes true.

10
Advanced Logic

Combining Logical Operators

You can use more than one logical operator in the same expression.

Combining and with or
age = 25
student = True

result = age >= 18 and student == True

print(result)
Output
True

More complex conditions can also use parentheses to make the intended logic clear.

Parentheses improve clarity
age = 25
student = True
employee = False

result = age >= 18 and (student == True or employee == True)

print(result)
Output
True
11
Important Rule

Logical Operator Precedence

When multiple logical operators appear in the same expression, Python follows a specific order.

The logical precedence order is:

  1. not
  2. and
  3. or
Operator precedence
result = True or False and False

print(result)
Output
True

Python evaluates False and False first, producing False. It then evaluates True or False, producing True.

Use parentheses when logic gets complicated

Parentheses make your intention explicit and make code easier for other developers to understand.

12
Essential Combination

Logical + Comparison Operators

One of the most common patterns in Python is to combine comparison operators with logical operators.

Multiple conditions
age = 30
salary = 75000

eligible = age >= 18 and salary >= 50000

print(eligible)
Output
True

This pattern will become extremely important when you start writing if statements.

13
Practical Python

Using Variables with Logical Operators

Boolean variables can be combined directly with logical operators.

Boolean variables
has_id = True
has_ticket = True

allowed = has_id and has_ticket

print(allowed)
Output
True

A cleaner Python style is often to use Boolean variables directly instead of writing comparisons such as has_id == True.

Pythonic style
has_id = True
has_ticket = True

allowed = has_id and has_ticket

print(allowed)
14
Real-World Applications

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.

Free delivery condition
premium = False
cart_total = 6500

free_delivery = premium or cart_total >= 5000

print(free_delivery)
Output
True

Student Eligibility

A student may qualify for a program if they meet both an age requirement and a marks requirement.

Student eligibility
age = 21
marks = 78

eligible = age >= 18 and marks >= 60

print(eligible)
Output
True

Login Verification

A system may require both a valid username and password.

Login conditions
username_correct = True
password_correct = True

login_successful = username_correct and password_correct

print(login_successful)
Output
True
15
Troubleshooting

Common Mistakes

1. Confusing and with or

Remember:

  • and requires every condition to be true.
  • or requires at least one condition to be true.

2. Using && instead of and

Python uses English-style logical keywords.

Correct Python syntax
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.

Clearer condition
age >= 18 and (student or employee)

4. Comparing Boolean variables unnecessarily

Instead of:

Works, but unnecessary
if logged_in == True:

You can usually write:

Cleaner style
if logged_in:
16
Practice

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.

17
Build Something

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.
Challenge requirements

Use input(), int(), comparison operators, and the and operator. You do not need an if statement yet.

One possible solution
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)
Challenge extension

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.

18
Test Yourself

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?

19
Wrap Up

Lesson Summary

Logical operators allow Python programs to combine multiple conditions and make more sophisticated decisions.

Operator Meaning Example
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
Key takeaway

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.