The Logic
All the notes. All the examples. All in one place. The challenges prove you understand it. Solve them all and you mastered The Logic.
▶📝 Cheatsheet
1. Comparisons
Comparisons ask a question. The answer is always True or False.
10 == 10 # True10 != 5 # True10 > 5 # True10 < 5 # False10 >= 10 # True10 <= 3 # FalseComparing variables
age = 22print(age >= 18) # Trueprint(age == 25) # FalseComparing strings
name = "Ahmed"print(name == "Ahmed") # Trueprint(name == "ahmed") # False2. If / Elif / Else
Now you can make decisions based on that True or False.
If
age = 20if age >= 18: print("Adult")If / else
age = 14if age >= 18: print("You can vote")else: print("Too young")If / elif / else
age = 14if age < 12: price = 80elif age < 18: price = 100elif age < 65: price = 130else: price = 90print(f"Ticket: ${price}")3. Logical Operators
Combine multiple conditions in one line.
and (both must be true)
age = 24has_id = Trueif age >= 18 and has_id: print("Entry allowed")or (one must be true)
is_student = Trueis_senior = Falseif is_student or is_senior: print("Discount applied")not (flips the value)
is_banned = Falseif not is_banned: print("Welcome")4. For Loops
You send "Eid Mubarak!" to 50 contacts. Without a loop, you type the message, hit send, type it again, hit send, 50 times. With a loop, you type it once and Python sends it to everyone on your list.

# Without a loopprint("Eid Mubarak, contact 1!")print("Eid Mubarak, contact 2!")print("Eid Mubarak, contact 3!")# ... 47 more times
# With a loopfor i in range(1, 51): print(f"Eid Mubarak, contact {i}!")Looping through a range
for i in range(5): print(i) # 0, 1, 2, 3, 4Range with start and stop
for i in range(1, 6): print(i) # 1, 2, 3, 4, 5Range with step
for i in range(0, 20, 5): print(i) # 0, 5, 10, 15Looping through a string
name = "Ahmed"for letter in name: print(letter)Loop with a condition
for i in range(1, 11): if i % 2 == 0: print(i) # 2, 4, 6, 8, 10Print only odd numbers
for i in range(1, 21): if i % 2 != 0: print(i) # 1, 3, 5, 7 ... 195. While Loops
A for loop runs a set number of times. A while loop keeps running until a condition is no longer true.
Basic while loop
count = 1while count <= 5: print(count) count += 1 # 1,2, 3, 4,5 Adding until limit
total = 0while total < 100: total = total + 25 print(total)Mental model: for = you know how many times. while = you don't know, just keep going until done.
Summary
| Category | Key Ideas |
|---|---|
| Comparisons | ==, !=, >, <, >=, <= |
| Conditions | if, elif, else |
| Logical | and, or, not |
| For loops | for i in range(), loop with condition |
| While loops | while condition, until |
▶🧩 Challenges
Challenge 1: Can You Vote?
Ask the user for their age. If 18 or older, print "You can vote." If not, print how many years they have to wait.
Age: 15You can't vote yet. 3 more years!Challenge 2: Ticket Price
Ask the user for their age. Under 12: $5. 12-17: $8. 18+: $12. Print the price.
Age: 14Ticket: $8Challenge 3: Parking Fee
Ask the user how many hours they parked. 1 hour: $5, 2-3 hours: $10, 4+ hours: $20. Print the fee.
Hours: 4Fee: $20Challenge 4: Speed Check
Ask the user for the speed limit and their speed. If over the limit, the fine is $50 for every 10 km/h over. If not over, print "No fine."
Speed limit: 90Your speed: 112Over by: 22 km/hFine: $100Challenge 5: Countdown
Ask the user for a number. Count down to 1 and print "Go!" at the end.
Start: 554321Go!Challenge 6: Grade Checker
Ask the user for their score (0-100). Print the letter grade: A (90+), B (80-89), C (70-79), D (60-69), F (below 60).
Score: 85Grade: BChallenge 7: Even or Odd
Ask the user for a number. Print if it is even or odd. Then print all even numbers from 1 to that number.
Number: 1010 is even
Even numbers: 2, 4, 6, 8, 10Challenge 8: Discount Day
Ask for the day of the week and the price. Monday and Friday get 20% off. Wednesday gets 10% off. All other days full price.
Day: MondayPrice: $50Discount: 20%Final: $40Challenge 9: Star Printer
Ask the user for a number. Print that many stars on one line.
Number: 7*******Challenge 10: Flight Check-in
Ask for passenger name, age, and bag weight. Must be 18+ to fly alone. Bag must be under 23 kg. If both pass, print boarding pass. If not, print which rule failed.
Name: HodanAge: 22Bag weight: 18======================== BOARDING PASS========================Passenger: HODANStatus: Checked in========================