Code / Python for Beginners / Unit 2: Maths and decisions

and, or, not and, or, not

Lesson 6 of 14 · about 12 minutes · Combine conditions.

⚡ 0 XP

Step 1 of 2

Two conditions at once

if ?true ✓else ✗

Combine conditions with:

  • and: True only if both sides are True
  • or: True if at least one side is True
  • not: flips True and False

Example: free delivery when the order is ₹499 or more and you live within 5 km. If the shopkeeper wrote or by mistake, someone 50 km away with a big order would get free delivery!

Example
has_ticket = True
age = 10
print(has_ticket and age >= 12)
print(has_ticket or age >= 12)
print(not has_ticket)

🎯 Challenge: Free delivery bug +15 XP

The kirana shop gives free delivery only when the order amount is ₹499 or more and the distance km is 5 or less. Then it prints Free delivery!, otherwise Delivery fee: ₹40. The code has a bug: fix it.

Try twice to unlock the solution (0/2)

main.pyCtrl ⏎
Output

What your program prints appears here.

Checklist

Press Run to check your code.

  • ₹600 at 3 km is free
  • ₹499 at 5 km is free (edges count)
  • ₹600 at 8 km pays ₹40
  • ₹300 at 2 km pays ₹40
Lesson notes (text version)

1. Two conditions at once

Combine conditions with:

  • and: True only if both sides are True
  • or: True if at least one side is True
  • not: flips True and False

Example: free delivery when the order is ₹499 or more and you live within 5 km. If the shopkeeper wrote or by mistake, someone 50 km away with a big order would get free delivery!

                has_ticket = True
age = 10
print(has_ticket and age >= 12)
print(has_ticket or age >= 12)
print(not has_ticket)
              

Challenge: Free delivery bug

The kirana shop gives free delivery only when the order amount is ₹499 or more and the distance km is 5 or less. Then it prints Free delivery!, otherwise Delivery fee: ₹40. The code has a bug: fix it.

2. Leap years

A year is a leap year (with 29 February) when:

  • it divides by 4 and not by 100, or
  • it divides by 400

"Divides by 4" in Python is year % 4 == 0 (no remainder). So 2024 is a leap year, 1900 is not (divides by 100), and 2000 is (divides by 400).

Use brackets to group conditions so they are easy to read: (a and b) or c.

Challenge: Is it a leap year?

Read a year. Print 2024 is a leap year or 2023 is not a leap year (with the real year). The starter only checks % 4, so it is wrong for 1900. Add the 100 and 400 rules.

Search by task, format or tool name.