Python Logical Operators
Table of Contents + β
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 = 20has_ticket = True
can_enter = age >= 18 and has_ticketprint(can_enter)Output
True
Letβs read it line by line:
age >= 18checks if the person is old enough. Here that isTrue.has_ticketis alreadyTrue.andlooks at both. Both are true, so the final answer isTrue.
Now change one thing. Say the person has no ticket.
age = 20has_ticket = False
can_enter = age >= 18 and has_ticketprint(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 = Falseis_free_day = True
can_watch = has_ticket or is_free_dayprint(can_watch)Output
True
Reading it:
has_ticketisFalsethis time.is_free_dayisTrue.oronly needs one side to be true, and the free day is true, so the answer isTrue.
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_takenprint(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 = 16has_ticket = Trueis_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_dayruns first because it is in brackets. The person has a ticket, so this part isTrue.age >= 18isFalse, because the age is 16.andjoins them. One side is false, so the final answer isFalse.
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 isFalse, the whole thing must beFalse. So Python does not even look at the second side. - With
or, if the first side isTrue, the whole thing must beTrue. 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 ofandoror. Those single symbols are for bit-level work, not everyday true-or-false logic. Stick with the words.
# β Avoid: & is not the logical "and" hereif 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 thinkif 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 sidesif age > 18 and age < 60: print("Working age")- Reading
notwrong. Here==is checked beforenot, sonot a == bactually meansnot (a == b). That is often not what you meant. Add brackets to make your intent clear.
is_admin = False
# β Avoid: confusing precedenceif not is_admin == False: print("Hmm")
# β
Good: clear and readableif 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
andchain. Thanks to short-circuit, aFalsethere saves Python the work of the rest. - When a condition gets long, give it a clear name.
can_enter = age >= 18 and has_ticketreads far better than the same logic buried inside anif.
π§© 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
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
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
What is the value of `not False`?
Why: `not` flips the value, so `not False` becomes True.
- 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.