Python Logical Operators

In the last lesson you learned Comparison Operators. Those answer one yes-or-no question at a time. Like β€œis the age 18 or more?”. But real decisions are rarely just one question. Often you need to check two or three things together before you say yes. That is exactly what logical operators are for.

πŸ€” Why do we need logical operators?

Picture a cinema gate. To let someone in, the system has to be sure of two things at once. The person is old enough, AND they are holding a ticket. One check on its own is not enough. If you only check the age, someone without a ticket walks in for free. If you only check the ticket, a child gets into an adult film.

So the pain is this. A single comparison can only answer one question. You need a way to combine several true-or-false answers into one final decision.

That is what logical operators do. They take boolean values. Those are the True and False you get from comparisons. Then they join them into a single True or False answer.

🧩 The three logical operators

Python gives you three words for this. They read almost like plain English:

  • and β€” true only when both sides are true.
  • or β€” true when at least one side is true.
  • not β€” flips the answer. True becomes False, and False becomes True.

Think of them like everyday speech. β€œYou can enter if you are 18 and you have a ticket.” β€œDinner is ready if Riya cooks or Arjun orders food.” β€œYou are not done yet.”

πŸ”— The and operator

Use and when every condition has to be true. If even one side is false, the whole thing is false.

Here is the cinema gate written in code. We check the age and the ticket together.

age = 20
has_ticket = True
can_enter = age >= 18 and has_ticket
print(can_enter)

Output

True

Let’s read it line by line:

  • age >= 18 checks if the person is old enough. Here that is True.
  • has_ticket is already True.
  • and looks at both. Both are true, so the final answer is True.

Now change one thing. Say the person has no ticket.

age = 20
has_ticket = False
can_enter = age >= 18 and has_ticket
print(can_enter)

Output

False

The age check still passes, but has_ticket is False. Because and needs both sides true, the whole result drops to False. The gate stays shut.

πŸ”€ The or operator

Use or when any one condition being true is enough. The result is false only when every side is false.

Imagine free entry on a special day. A person can watch the movie if they have a ticket OR it is a free-entry day.

has_ticket = False
is_free_day = True
can_watch = has_ticket or is_free_day
print(can_watch)

Output

True

Reading it:

  • has_ticket is False this time.
  • is_free_day is True.
  • or only needs one side to be true, and the free day is true, so the answer is True.

The person still gets in, even without a ticket, because it is a free day.

🚫 The not operator

not is the odd one out. It takes just one value and flips it. True turns into False, and False turns into True.

This is handy when you store the opposite of what you want to check. Say you have a variable for whether the seat is taken, and you want to know if it is free.

seat_taken = False
seat_is_free = not seat_taken
print(seat_is_free)

Output

True

The seat is not taken, so not seat_taken gives True. That means the seat is free. If seat_taken were True, then not seat_taken would be False.

πŸ“Š The truth tables

It helps to see every possible outcome in one place. These small tables show what each operator gives for each mix of inputs. Read them like this. Pick a value for A, pick a value for B, and the last column is the result.

Here is and. Notice the result is True in only one row, when both are true.

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

Here is or. Now the result is False in only one row, when both are false.

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

And not, which has just one input.

A not A
True False
False True

🎬 A fuller real example

Let’s put the pieces together. A cinema lets someone in if they are old enough AND they either have a ticket OR it is a free-entry day. We can write that whole rule in one line.

age = 16
has_ticket = True
is_free_day = False
can_enter = age >= 18 and (has_ticket or is_free_day)
print("Can the person enter?", can_enter)

Output

Can the person enter? False

Here is what happens:

  • has_ticket or is_free_day runs first because it is in brackets. The person has a ticket, so this part is True.
  • age >= 18 is False, because the age is 16.
  • and joins them. One side is false, so the final answer is False.

The age fails, so it does not matter that the ticket is fine. The gate stays shut. Brackets make the order clear, just like in maths.

⚑ Short-circuit: Python stops early

Python is a little lazy here, and that helps you. Once it knows the final answer, it stops checking the rest. This is called short-circuit evaluation.

Here is the simple idea, no code needed yet:

  • With and, if the first side is False, the whole thing must be False. So Python does not even look at the second side.
  • With or, if the first side is True, the whole thing must be True. So again Python skips the rest.

You can see it matters when the second side is something slow or risky. Imagine checking that a name is not empty before you read its first letter.

name = ""
if name != "" and name[0] == "A":
print("Starts with A")
else:
print("No match")

Output

No match

Because name != "" is False (the name is empty), Python never runs name[0]. That is good, because reading the first letter of an empty text would cause an error. The short-circuit quietly protects you here.

⚠️ Common Mistakes

A few things confuse people when they first combine conditions:

  • Using & or | instead of and or or. Those single symbols are for bit-level work, not everyday true-or-false logic. Stick with the words.
# ❌ Avoid: & is not the logical "and" here
if age >= 18 & has_ticket:
print("Enter")
# βœ… Good: use the word "and"
if age >= 18 and has_ticket:
print("Enter")
  • Forgetting to repeat the variable. You cannot shorten β€œage is over 18 and under 60” the way you say it out loud.
# ❌ Avoid: this does not check the range you think
if 18 < age < 60 or age == 0:
pass
# ❌ Avoid: this is invalid, you must compare age each time
# if age > 18 and < 60:
# βœ… Good: write the full comparison on both sides
if age > 18 and age < 60:
print("Working age")
  • Reading not wrong. Here == is checked before not, so not a == b actually means not (a == b). That is often not what you meant. Add brackets to make your intent clear.
is_admin = False
# ❌ Avoid: confusing precedence
if not is_admin == False:
print("Hmm")
# βœ… Good: clear and readable
if not is_admin:
print("Not an admin")

βœ… Best Practices

  • Use the plain words and, or, not. They read like English and keep your conditions clear.
  • Add brackets around grouped conditions, like (has_ticket or is_free_day). Even when Python does not need them, they make your intent obvious to the next reader.
  • Put the cheap or most likely check first in an and chain. Thanks to short-circuit, a False there saves Python the work of the rest.
  • When a condition gets long, give it a clear name. can_enter = age >= 18 and has_ticket reads far better than the same logic buried inside an if.

🧩 What You’ve Learned

βœ… Logical operators combine true-or-false answers into one final decision.

βœ… and is true only when both sides are true.

βœ… or is true when at least one side is true.

βœ… not flips a single value: True becomes False, and False becomes True.

βœ… Brackets control the order when you mix operators, just like in maths.

βœ… Python short-circuits: it stops checking once the answer is already certain.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does `age >= 18 and has_ticket` return when age is 20 and has_ticket is False?

    Why: `and` needs both sides true. The ticket is False, so the whole result is False.

  2. 2

    When is an `or` expression False?

    Why: `or` is False only when every side is False; one True is enough to make it True.

  3. 3

    What is the value of `not False`?

    Why: `not` flips the value, so `not False` becomes True.

  4. 4

    In `name != "" and name[0] == "A"`, why is `name[0]` safe even when name is empty?

    Why: With `and`, a False first side makes the result False, so Python never runs the second side.

πŸš€ What’s Next?

You can now combine conditions to make real decisions. Next we will look at the operators that store and update values as your program runs.

Assignment Operators

Share & Connect