Python Logical Operators

In the last lesson, we learned comparison operators. Now let’s combine more than one condition using logical operators.

After learning Python Comparison Operators, we can create conditions that produce True or False. Now let’s combine and reverse those conditions. In this tutorial, we’ll learn about Python logical operators with easy examples.

🤔 Let’s Talk About Combining Conditions

Life is not just yes or no. Sometimes it is yes AND no. Or yes OR no. Or NOT yes.

Let me explain.

Imagine we want to go to a party. What are the conditions?

  • It must be Saturday
  • We must have money

Both must be true. That is “and.”

Or maybe:

  • It is Saturday
  • OR we have money

Just one needs to be true. That is “or.”

Or maybe:

  • We are NOT tired

That is “not.”

Python has these same three logical operators. And they let us combine conditions. Let’s learn them.

🧩 The Three Logical Operators in Python

Python has three logical operators.

OperatorMeaningExample
andBoth must be trueTrue and False
orAt least one must be trueTrue or False
notFlips the valuenot True

Let’s see each one.

🔧 Python and Operator

So what does and do? It gives True only if both things are True.

Let’s see all the possibilities.

print(True and True) # True
print(True and False) # False
print(False and True) # False
print(False and False) # False

See the pattern? Only when both are True, the answer is True.

Think of it like this. “I will go to the party if it is Saturday AND I have money.”

  • Saturday and money? Go.
  • Saturday but no money? Do not go.
  • No Saturday but money? Do not go.
  • No Saturday and no money? Do not go.

Only one case works. Both must be true.

🧪 Real-Life Example: Can Someone Vote?

Let’s check if someone can vote.

age = 20
is_citizen = True
can_vote = age >= 18 and is_citizen
print(can_vote)

Output

True

Because age is 20 (which is >= 18) AND is_citizen is True. Both are true. So the answer is True.

Now if the person was 16:

age = 16
is_citizen = True
can_vote = age >= 18 and is_citizen
print(can_vote)

Output

False

Because age is not >= 18. So the whole thing is False.

That is how and works. Both must be true.

🧭 Python or Operator

So what does or do? It gives True if at least one thing is True.

print(True or True) # True
print(True or False) # True
print(False or True) # True
print(False or False) # False

See the pattern? Only when both are False, the answer is False.

Think of it like this. “I will go to the party if it is Saturday OR I have money.”

  • Saturday and money? Go.
  • Saturday but no money? Go.
  • No Saturday but money? Go.
  • No Saturday and no money? Do not go.

Just one needs to be true.

💬 Real-Life Example: Discount for Students or Seniors

Let’s check if someone gets a discount.

The rules are:

  • They are a student
  • OR they are a senior citizen
is_student = True
is_senior = False
gets_discount = is_student or is_senior
print(gets_discount)

Output

True

Because is_student is True. So the answer is True. Even though is_senior is False.

Now if both were False:

is_student = False
is_senior = False
gets_discount = is_student or is_senior
print(gets_discount)

Output

False

Because both are False. So the answer is False.

That is how or works. At least one must be true.

🖥 Python not Operator

So what does not do? It flips the value. True becomes False. False becomes True.

print(not True) # False
print(not False) # True

Simple.

🐍 Real-Life Example: Not Banned

Let’s check if someone is allowed to enter.

The rule is: they are NOT banned.

is_banned = False
can_enter = not is_banned
print(can_enter)

Output

True

Because is_banned is False. And not False is True. So the answer is True.

Now if the person was banned:

is_banned = True
can_enter = not is_banned
print(can_enter)

Output

False

Because is_banned is True. And not True is False. So the answer is False.

That is how not works. It flips the value.

☕ Combining Logical Operators in Python

We can combine all three operators. And we can use them with comparisons.

age = 25
is_student = True
has_id = True
can_get_discount = age < 30 and is_student and has_id
print(can_get_discount)

Output

True

Because all three are true. So the answer is True.

We can also mix and and or.

is_student = True
is_senior = False
has_coupon = True
gets_discount = (is_student or is_senior) and has_coupon
print(gets_discount)

Output

True

First, the parentheses: is_student or is_senior is True or False, which gives True.

Then True and has_coupon is True and True, which gives True.

So the final answer is True.

The point is: let’s use parentheses when we mix operators. It makes things clear.

🌍 Order of Operations for Logical Operators

Just like math, logical operators have an order.

The order is:

  1. not (highest priority)
  2. and
  3. or (lowest priority)

So not happens first. Then and. Then or.

print(True or False and False)

What do we think this gives?

If we said True, we are right. Because and happens before or.

So False and False is False. Then True or False is True.

If we want to change the order, we use parentheses.

print((True or False) and False)

Now it gives False. Because True or False is True. Then True and False is False.

Let’s use parentheses when we are not sure. It makes our code clearer.

📋 Truth Tables for Logical Operators

Let’s see the truth tables. These show all possible outcomes.

🙂 and Truth Table

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

📌 or Truth Table

ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

🤝 not Truth Table

Anot A
TrueFalse
FalseTrue

We do not need to memorize these. Just understand the idea.

📖 Real-Life Example: Driver’s License Check

Let’s check if someone can get a driver’s license.

The rules are:

  • They must be at least 16 years old
  • They must have passed the written test
  • They must NOT have any unpaid fines
age = 18
passed_test = True
has_unpaid_fines = False
can_get_license = age >= 16 and passed_test and not has_unpaid_fines
print(can_get_license)

Output

True

Let’s break it down.

  • age >= 16 is True
  • passed_test is True
  • not has_unpaid_fines is not False which is True

So True and True and True is True.

Now if the person had unpaid fines:

age = 18
passed_test = True
has_unpaid_fines = True
can_get_license = age >= 16 and passed_test and not has_unpaid_fines
print(can_get_license)

Output

False

Because not has_unpaid_fines is not True which is False.

So True and True and False is False.

That is how it works.

⌨ Another Example: Checking a Range

Let’s check if a number is in a range.

number = 15
in_range = number > 10 and number < 20
print(in_range)

Output

True

Because 15 is greater than 10 AND less than 20. Both are true.

We can also write this with chained comparison:

number = 15
in_range = 10 < number < 20
print(in_range)

Output

True

Both work. The second one is shorter. But the first one is clearer for beginners.

⚠️ Common Mistakes with Python Logical Operators

👥 Mistake 1: Using & Instead of and

Here’s something. We might write True & False. And it works. But it’s not the same.

print(True & False) # This works, but it is not the same

Why? Because & is a bitwise operator. It works on numbers. Not on booleans in the way we think.

So what do we do? We always use and, or, not. Not &, |, !.

💼 Mistake 2: Forgetting Parentheses

Let’s say we write True or False and False. What do we get?

print(True or False and False) # True

We get True. Because and happens before or. So False and False is False. Then True or False is True.

But what if we wanted True or False to happen first? Then we use parentheses.

print((True or False) and False) # False

Now we get False. Because True or False is True. Then True and False is False.

Different answer. So let’s use parentheses to be clear.

🧰 Mistake 3: Using not with ==

We might write not x == 5. And it works. But it’s confusing.

x = 5
print(not x == 5) # False

Why is it False? Because x == 5 is True. And not True is False.

But this is confusing to read. So what do we do? We write it differently.

print(x != 5) # False

Both give the same result. But the second is clearer.

🔍 Mistake 4: Thinking or Means Both

Some people think or means “both.” It does not.

or means “at least one.” If both are true, that is fine too. But only one is needed.

Next, we will learn about Assignment Operators so we can continue step by step.