livepython.net
CHAPTER 08If Statements
8.3  AND, OR, NOT

Python Logical Operators: and, or, not

and: Both Must Be True

Sometimes one condition isn't enough. and joins two conditions, and the result is True only when both are True:

PYTHONedit me
age = 25
has_licence = True
if age >= 18 and has_licence:
    print("You can drive.")
else:
    print("You can't drive yet.")
OUTPUT
You can drive.

Change has_licence to False: now it says you can't drive, even though the age is fine.

The and table

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Think of a door with two locks: it only opens when both keys turn.

Checking a range

and is perfect for "between" questions:

PYTHONedit me
marks = int(input())
if marks >= 0 and marks <= 100:
    print("Valid marks")
else:
    print("Marks must be 0 to 100")
OUTPUT
Marks must be 0 to 100

Python also lets you write this range the way you would in maths: 0 <= marks <= 100. It means exactly the same thing, and it's easy to read.

Write both sides in full

A common mistake is if marks >= 0 and <= 100:. Python needs a full comparison on each side of and: marks >= 0 and marks <= 100.

and is True only when both conditions are True.

your turnPredict the output

What will this print?

x = 7 print(x > 5 and x < 10)
Your turn! What will this code show on the screen?
your turnFill the gap

Only teenagers count: make it print False for a 40-year-old

age = 40 print(age >= 13 ? age <= 19)

It should print: False

Which piece makes this code work?

or: At Least One Must Be True

or joins two conditions, and the result is True when at least one of them is True:

PYTHONedit me
day = input()
if day == "Saturday" or day == "Sunday":
    print("It's the weekend!")
else:
    print("It's a school day.")
OUTPUT
It's the weekend!

The or table

ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

or is only False when both are False.

Think of a house with two doors: you can get in if either one is open.

A classic mistake

PYTHONedit me
day = "Monday"
if day == "Saturday" or "Sunday":
    print("Weekend!")
OUTPUT
Weekend!

Monday is not the weekend, yet it prints Weekend!. Why? Python reads it as two separate parts: day == "Saturday", or "Sunday". A non-empty string like "Sunday" counts as true all by itself, so the whole condition is always True.

The fix: write the full comparison on both sides: day == "Saturday" or day == "Sunday".

and or or?

  • "You need a ticket and an ID" → both: and.
  • "Pay by cash or card" → either one: or.

Common beginner doubt: "Does or mean one or the other, but not both?" No. In Python, or is also True when both sides are True. In everyday English, "or" sometimes means "only one", but not in code.

or is True when at least one condition is True. Write both comparisons in full.

your turnType the output

Type what this prints

a = 3 print(a > 5 or a == 3)
Read the code line by line, then type the output exactly.
your turnFix the code

Make it print Holiday only for Saturday or Sunday

It should print: Work

The user types: Monday

day = input()
if day == "Saturday" or "Sunday":
    print("Holiday")
else:
    print("Work")
This code does not work yet. Fix it, then press Run.

not: Flip True and False

not turns True into False, and False into True:

PYTHONedit me
is_raining = False
if not is_raining:
    print("Let's go to the park!")
OUTPUT
Let's go to the park!

is_raining is False, so not is_raining is True, and the block runs. Read it out loud: "if not raining".

Anot A
TrueFalse
FalseTrue

Think of a light switch's "flip" button: whatever it is now, press it and it becomes the opposite.

When not reads well

not is clearest with boolean variables that have good names (chapter 4):

PYTHONedit me
has_paid = False
if not has_paid:
    print("Please pay first.")
OUTPUT
Please pay first.

When not is harder to read

With comparisons, there's usually a clearer way:

Harder to readClearer
not age >= 18age < 18
not name == "Sunny"name != "Sunny"

Both versions work. Pick the one that reads like a normal sentence. A double negative, like not not is_open, is legal Python, but nobody wants to read it.

not with an empty string

An empty string "" counts as false, so not can check if a text is empty, for example when a user typed nothing:

PYTHONedit me
name = ""
if not name:
    print("The name is empty.")
OUTPUT
The name is empty.

not flips True and False. Use it where it reads like a sentence.

your turnPredict the output

What will this print?

logged_in = True print(not logged_in)
Your turn! What will this code show on the screen?
your turnPredict the output

What will this print?

n = int(input()) print(n + n)

The user types: 4

Your turn! What will this code show on the screen?

Combining Conditions

You can use and, or and not together in one condition:

PYTHONedit me
age = 70
is_student = False
if age < 18 or age >= 60 or is_student:
    print("You get a discount.")
else:
    print("Full price.")
OUTPUT
You get a discount.

Python's order

Like maths operators (chapter 5), logical operators have an order:

  1. not first,
  2. then and,
  3. then or.

That can surprise you:

PYTHONedit me
is_member = False
age = 70
has_coupon = True
print(is_member and age > 60 or has_coupon)
OUTPUT
True

Python reads this as (is_member and age > 60) or has_coupon. The and part is False, but has_coupon is True, so the answer is True.

Use brackets to be clear

When you mix and with or, always add brackets. They show which parts belong together, and they let you choose the order:

PYTHONedit me
is_member = False
age = 70
has_coupon = True
print(is_member and (age > 60 or has_coupon))
OUTPUT
False

Same values, different brackets, different answer. Now the member check must be true, and it isn't.

Think of brackets as boxes: Python opens the inner boxes first.

Keep it readable

If a condition gets long, store parts in well-named variables first:

PYTHONedit me
age = 15
is_child = age < 13
is_teen = 13 <= age <= 19
if is_child or is_teen:
    print("Young visitor")
OUTPUT
Young visitor

Mixing and with or? Use brackets, or name the parts first.

your turnPredict the output

What will this print?

a = True b = False print(not a or b)
Your turn! What will this code show on the screen?
your turnPick the right idea

Which condition means: a member who is over 60, or anyone with a coupon?

Think about it, then pick one.

Practice: and, or, not

Solve these to lock in what you learned in this topic. Each one runs in your browser.

  1. 64.Python Program to Check If a Number Is Between 1 and 100EasySolve
  2. 65.Python Program to Check If a Day Is a WeekendEasySolve
  3. 66.Python Program to Check If a Year Is a Leap YearMediumSolve