Thread Closed 
Thread Rating:
CHAPTER 10 — Conditions & If Statements
#1
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 to
!=  not equal to
>    greater than
<    less than
>=  greater or equal
<=  less or equal

Examples:

Code:
print(5 > 2)    # True
print(3 == 4)    # False
print("a" != "b") # True

---

10.2 Basic if Statement

Code:
age = 16

if age >= 18:
    print("You can enter.")

If the condition is False, Python skips the inside block.

---

10.3 if–else

Code:
age = 16

if age >= 18:
    print("Adult ticket")
else:
    print("Child ticket")

---

10.4 if–elif–else (Multiple Options)

Code:
temp = 30

if temp > 35:
    print("Too hot!")
elif temp > 20:
    print("Warm")
else:
    print("Cold")

---

10.5 Logical Operators

Combine conditions:

Code:
and    # both must be true
or      # at least one is true
not    # reverses a condition

Examples:

Code:
age = 25
has_id = True

if age >= 18 and has_id:
    print("Entry approved")

---

10.6 Nested Conditions

Code:
age = 20
member = True

if age >= 18:
    if member:
        print("VIP Entry")
    else:
        print("Standard Entry")

---

10.7 Comparing Strings

Code:
name = "lee"

if name.lower() == "lee":
    print("Welcome, 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
« Next Oldest | Next Newest »
Thread Closed 


Forum Jump:


Users browsing this thread: 1 Guest(s)