TalentJR

🐍 Python for kids · Class 6 · Lesson 14 of 18 · Free

Complex Conditions – and, or, not Together

⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in

In this lesson you will

  • Combine two or more checks with and, or and not.
  • Use brackets to control which part Python checks first.
  • Write range checks like 13 <= age <= 19 and the leap year rule.

To play in the school cricket team, you must be in Class 6 to 8, AND have 75% attendance, AND either bat well OR bowl well. Real rules have many parts. Today you will write rules like this in one line of Python.

How do and, or and not work in Python?

and is True only when both sides are True. One False side makes the whole thing False.

or is True when at least one side is True. It is False only when both sides are False.

not flips a value. not True is False, and not False is True.

🐍 Python — you can edit this code
Output
sunny and holiday: False
sunny or holiday: True
not holiday: True
not sunny: False

💡 Each condition becomes True or False first. Then and, or and not combine those answers.

How do I use and to check a number is in a range?

A teenager is 13 or more and 19 or less. That needs two checks joined with and.

Python also lets you chain the checks, like in maths: 13 <= age <= 19. It means the same thing and reads more naturally.

Chained checks are handy for marks bands, temperature limits and age groups.

🎬 How Python thinks — step 1 of 7Python is about to run line 1
▶age = int(input("Your age: "))
2long_way = age >= 13 and age <= 19
3short_way = 13 <= age <= 19
4print("Teen (long way):", long_way)
5print("Teen (short way):", short_way)
6if short_way:
7 print("Welcome to the teen quiz!")
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Your age: 12
Teen (long way): False
Teen (short way): False

Why do brackets matter when I mix and with or?

Python checks and before or, just like it does * before + in maths.

Sometimes that order is not what you mean. Brackets ( ) force Python to check a part first.

Below, a student gets a library card if they are in Class 6, and either have a form or a teacher's note. Watch both answers.

🎬 How Python thinks — step 1 of 8Python is about to run line 1
▶in_class6 = False
2has_form = False
3has_note = True
4no_brackets = in_class6 and has_form or has_note
5with_brackets = in_class6 and (has_form or has_note)
6print("No brackets:", no_brackets)
7print("With brackets:", with_brackets)
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
No brackets: True
With brackets: False

💡 Without brackets, has_note alone made it True, even though the student is not in Class 6. Brackets fix the meaning.

How do I use not in a condition?

not is useful when you want to act on the no answer. It reads like English.

You can also use not in with lists and strings. It asks: is this value missing?

Below, a shop sells sweets only when it is not closed and the sweet is not sold out.

🐍 Python — you can edit this code
Output
Which sweet? ladoo
Here is your ladoo!

How do I check a leap year in Python?

The leap year rule is a famous complex condition. A year is a leap year if it divides by 4 but not by 100.

There is one more rule: years that divide by 400 are leap years too. So 2000 was a leap year, but 1900 was not.

We use % (remainder) for each part and join the parts with and, or and brackets.

🎬 How Python thinks — step 1 of 14Python is about to run line 1
▶for year in [2024, 1900, 2000, 2026]:
2 leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
3 print(year, "leap year?", leap)
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
2024 leap year? True
1900 leap year? False
2000 leap year? True
2026 leap year? False

How do I split a long condition to make it readable?

A condition with many parts is hard to read on one line. Store each part in a well-named variable first.

Then the final if reads almost like a sentence. It is also easier to find a mistake.

Here is the cricket team rule from the start of the lesson.

🐍 Python — you can edit this code
Output
Selected for the school team!

🤔 Predict the output

Think like Python: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

a = 5
b = 12
print(a > 3 and b < 10)
print(a > 3 or b < 10)

Question 2. What does this print?

x = 0
print(not x > 5 and x < 3)

Question 3. What does this print?

temp = 38
if 36 <= temp <= 37.5:
    print("normal")
elif temp > 37.5 or temp < 30:
    print("see doctor")
else:
    print("check again")

🧩 Your challenge

A water park allows a ride if the visitor is between 120 and 190 cm tall (both included), and either is 10 or older, or comes with an adult. Ask for height, age and adult (yes/no). Print "Enjoy the ride!" or "Sorry, not this ride."

Expected output (with the inputs given):
Height in cm: 135
Age: 8
With an adult? (yes/no): yes
Enjoy the ride!
🐍 Python — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

colour = "blue"
if colour == "red" or "green":
    print("Traffic light colour!")

No error message – but it prints "Traffic light colour!" even for blue.

✅ Right

colour = "blue"
if colour == "red" or colour == "green":
    print("Traffic light colour!")

Each side of or must be a full check. A word like "green" alone counts as True, so the if always passes.

❌ Wrong

marks = 75
if marks >= 60 and <= 80:
    print("Grade B")

SyntaxError: invalid syntax

✅ Right

marks = 75
if 60 <= marks <= 80:
    print("Grade B")

After and, Python needs a complete check like marks <= 80. Or use a chained comparison.

❌ Wrong

day = "Sunday"
if day == "Saturday" and day == "Sunday":
    print("Weekend – no school!")
else:
    print("School day.")

No error message – but it prints "School day." even on Sunday.

✅ Right

day = "Sunday"
if day == "Saturday" or day == "Sunday":
    print("Weekend – no school!")
else:
    print("School day.")

One day cannot be Saturday and Sunday at the same time, so and is never True. Either day is enough, so use or.

📌 Remember

  • and is True only if both sides are True; or needs just one True side.
  • not flips True to False and False to True.
  • Python checks not first, then and, then or – use brackets to change or show the order.
  • Chained checks like 13 <= age <= 19 test a range in one go.
  • Each side of and/or must be a full check, like x == "red" or x == "green".
  • Store parts of a long rule in named variables to keep it readable.

Finished the quiz and the challenge?

Questions students ask

Which runs first in Python, and or or?

not runs first, then and, then or. Brackets always run before all of them, so use them when in doubt.

Can I write 10 < x < 20 in Python?

Yes. Python supports chained comparisons. It means 10 < x and x < 20.

Why does if x == 1 or 2 always run?

Python reads it as (x == 1) or (2). The number 2 counts as True, so the whole condition is True.

What is the difference between not in and !=?

!= compares two single values. not in checks that a value is missing from a list or string.