Python Logical Operators

1. What are Logical Operators?

Logical operators are used to combine conditions.

They help Python decide:

  • Yes or No

  • True or False

Python has three logical operators:

  • and

  • or

  • not

2. Logical AND (and)

and means both conditions must be True.

Example

age = 20
has_id = True

print(age >= 18 and has_id)

Output:

True

Real-Life Example

marks = 75
attendance = 80

if marks >= 40 and attendance >= 75:
print("You are allowed to sit in exam")

3. Logical OR (or)

or means at least one condition must be True.

Example

is_weekend = False
is_holiday = True

print(is_weekend or is_holiday)

Output:

True

Real-Life Example

day = "Sunday"

if day == "Saturday" or day == "Sunday":
print("Today is a holiday")

4. Logical NOT (not)

not means reverse the result.

Example

is_raining = False
print(not is_raining)

Output:

True

Real-Life Example

is_logged_in = False

if not is_logged_in:
print("Please login first")

5. Logical Operators with Comparison

Logical operators are often used with comparison operators.

Example

age = 25

print(age > 18 and age < 30)

6. Logical Operators with User Input

Example

username = "admin"
password = "1234"

user = input("Enter username: ")
pwd = input("Enter password: ")

if user == username and pwd == password:
print("Login successful")
else:
print("Login failed")

7. Truth Table (Easy)

AND

  • True and True → True

  • True and False → False

OR

  • False or True → True

  • False or False → False

NOT

  • not True → False

  • not False → True

8. Common Beginner Mistakes

❌ Using Symbols Instead of Words

if a && b:

✔ Correct:

if a and b:

❌ Forgetting Comparison

if age and 18:

✔ Correct:

if age >= 18:

9. Simple Practice Examples

Example 1: Age Check

age = 17

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

Example 2: Free Entry

is_child = False
is_senior = True

print(is_child or is_senior)

Example 3: Login Status

logged_in = False

print(not logged_in)

10. Summary (Logical Operators)

✔ Used to combine conditions
and needs all True
or needs one True
not reverses result
✔ Used in real-life decisions

📘 Perfect for Beginner eBook

This chapter is ideal for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • if–else statements

  • Logical Operator Exercises

  • Loops (for, while)

  • MCQs with answers

Just tell me 😊

You may also like...