Python Logical Operators
Table of Contents
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.
| Operator | Meaning | Example |
|---|---|---|
and | Both must be true | True and False |
or | At least one must be true | True or False |
not | Flips the value | not 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) # Trueprint(True and False) # Falseprint(False and True) # Falseprint(False and False) # FalseSee 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 = 20is_citizen = True
can_vote = age >= 18 and is_citizenprint(can_vote)Output
TrueBecause 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 = 16is_citizen = True
can_vote = age >= 18 and is_citizenprint(can_vote)Output
FalseBecause 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) # Trueprint(True or False) # Trueprint(False or True) # Trueprint(False or False) # FalseSee 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 = Trueis_senior = False
gets_discount = is_student or is_seniorprint(gets_discount)Output
TrueBecause is_student is True. So the answer is True. Even though is_senior is False.
Now if both were False:
is_student = Falseis_senior = False
gets_discount = is_student or is_seniorprint(gets_discount)Output
FalseBecause 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) # Falseprint(not False) # TrueSimple.
🐍 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_bannedprint(can_enter)Output
TrueBecause 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_bannedprint(can_enter)Output
FalseBecause 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 = 25is_student = Truehas_id = True
can_get_discount = age < 30 and is_student and has_idprint(can_get_discount)Output
TrueBecause all three are true. So the answer is True.
We can also mix and and or.
is_student = Trueis_senior = Falsehas_coupon = True
gets_discount = (is_student or is_senior) and has_couponprint(gets_discount)Output
TrueFirst, 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:
not(highest priority)andor(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
| 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 |
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 = 18passed_test = Truehas_unpaid_fines = False
can_get_license = age >= 16 and passed_test and not has_unpaid_finesprint(can_get_license)Output
TrueLet’s break it down.
age >= 16is Truepassed_testis Truenot has_unpaid_finesisnot Falsewhich is True
So True and True and True is True.
Now if the person had unpaid fines:
age = 18passed_test = Truehas_unpaid_fines = True
can_get_license = age >= 16 and passed_test and not has_unpaid_finesprint(can_get_license)Output
FalseBecause 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 < 20print(in_range)Output
TrueBecause 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 < 20print(in_range)Output
TrueBoth 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 sameWhy? 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) # TrueWe 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) # FalseNow 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 = 5print(not x == 5) # FalseWhy 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) # FalseBoth 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.