![]() |
|
CHAPTER 10 — Conditions & If Statements - Printable Version +- The Lumin Archive (https://theluminarchive.co.uk) +-- Forum: The Lumin Archive — Core Forums (https://theluminarchive.co.uk/forumdisplay.php?fid=3) +--- Forum: Courses — Structured Learning (https://theluminarchive.co.uk/forumdisplay.php?fid=69) +---- Forum: Beginner’s Guide to Coding — Python From Zero to Skill (https://theluminarchive.co.uk/forumdisplay.php?fid=72) +---- Thread: CHAPTER 10 — Conditions & If Statements (/showthread.php?tid=228) |
CHAPTER 10 — Conditions & If Statements - Leejohnston - 11-15-2025 Chapter 10 — Conditions & If Statements This is where your programs start thinking for themselves. Up until now, your programs ran the same way every time. Conditions let your code choose between different actions, based on: • user input • numbers • comparisons • logic --- 10.1 Comparison Operators Python uses the following: Code: == equal toExamples: Code: print(5 > 2) # True--- 10.2 Basic if Statement Code: age = 16If the condition is False, Python skips the inside block. --- 10.3 if–else Code: age = 16--- 10.4 if–elif–else (Multiple Options) Code: temp = 30--- 10.5 Logical Operators Combine conditions: Code: and # both must be trueExamples: Code: age = 25--- 10.6 Nested Conditions Code: age = 20--- 10.7 Comparing Strings Code: name = "lee"String comparisons must match exactly unless you force lowercase. --- 10.8 Typical Beginner Mistakes ❌ Using = instead of == Code: if age = 18: # WRONG✔ Correct: Code: if age == 18:❌ Forgetting indentation Python uses 4 spaces (or a tab). --- 10.9 Mini Project — Grade Checker Ask the user for a score from 0–100. Output: • A if ≥ 90 • B if ≥ 80 • C if ≥ 70 • D if ≥ 60 • Fail otherwise --- 10.10 Challenge — Login System Ask the user: • username • password Stored credentials: • username = admin • password = python123 Correct → “Login successful” Wrong → “Access denied” BONUS: • allow 3 attempts --- 10.11 Chapter Summary • if statements execute only when True • else handles all other cases • elif checks additional conditions • comparison operators compare values • and, or, not combine logic • indentation matters • strings should be normalised (lowercase) Next: Chapter 11 — Loops & Repetition Where you learn how to make Python repeat actions efficiently without writing the same line 50 times. --- Written and Compiled by Lee Johnston — Founder of The Lumin Archive |