Course progress Course outline 24 of 24 lessons available
Python Foundations
Data and Collections
Building Reliable Programs
Modeling with Objects
Professional Python
Advanced Python
Give Python traffic lights
Until now, Python followed one straight road. A useful program also needs traffic lights: “go this way if the answer is yes,” and “do this again while more work remains.” This is control flow.
- Ask a True-or-False question
- Choose run one matching branch
- Repeat until the job is done
By the end, you will build a 7-Day Study Tracker that asks seven questions, counts successful days, and prints one weekly message.
Ask questions and choose one road
A comparison produces the Boolean value True or False:
minutes = 35
print(minutes == 35)
print(minutes != 35)
print(minutes > 30)
print(minutes >= 40)
Output:
True
False
True
False
The six comparison signs are ==, !=, >, >=, <, and <=. Remember: one equals sign assigns; two equals signs compare. Also, 30 == "30" is False because an integer and a string are different values.
Use if, elif, and else to choose exactly one road:
score = 82
if score >= 90:
print("Excellent")
elif score >= 60:
print("Passed")
else:
print("Try again")
Output:
Passed
- 90 or more? Excellent
- 60 or more? Passed
- Otherwise Try again
- First match wins then Python moves on
The colon opens a block. Four spaces show which instructions belong inside it. Indentation is part of Python’s grammar.
Join questions with and when both must be true, or when either one is enough, and not to flip a Boolean:
minutes = 40
exercise_done = True
if minutes >= 30 and exercise_done:
print("Session complete.")
Common “empty” values—False, None, 0, and ""—act false in a condition. Most others act true. Be careful: "False" is non-empty text, so bool("False") is True.
Walk the loop road
Use for when you know how many turns you need:
for day in range(1, 4):
print(f"Day {day}")
Output:
Day 1
Day 2
Day 3
range(1, 4) stops before 4. Therefore, range(1, 8) gives days 1 through 7.
A running total and a counter begin before the loop and change inside it:
total = 0
days_met = 0
for minutes in (30, 20, 40):
total += minutes
if minutes >= 30:
days_met += 1
print(total)
print(days_met)
Output:
90
2
Use while when the ending depends on a changing condition:
remaining = 3
while remaining > 0:
print(remaining)
remaining -= 1
print("Go!")
-
Check
is
remaining > 0? - Run print this number
- Change subtract 1
Output is 3, 2, 1, then Go!. break can leave a loop early; continue can skip to its next turn. Use them sparingly so the path stays easy to see.
Build a 7-Day Study Tracker
Save this complete standard-library mini-project as study_tracker.py:
target = int(input("Daily target in minutes: "))
total = 0
days_met = 0
for day in range(1, 8):
minutes = int(input(f"Day {day} minutes: "))
total += minutes
if minutes >= target:
days_met += 1
print("Target met.")
else:
missing = target - minutes
print(f"{missing} minutes short.")
average = total / 7
print()
print(f"Weekly total: {total} minutes")
print(f"Daily average: {average:.1f} minutes")
print(f"Target met on {days_met}/7 days")
if days_met == 7:
print("Perfect consistency!")
elif days_met >= 5:
print("Strong week.")
else:
print("Keep building the habit.")
Use a target of 30 and enter 30, 20, 45, 30, 35, 10, 40 one at a time. Verify the summary:
Weekly total: 210 minutes
Daily average: 30.0 minutes
Target met on 5/7 days
Strong week.
Test the borders too: exactly 30 must count because the code uses >=. Seven successful days should choose “Perfect consistency!”, while four should choose “Keep building the habit.”
Three tiny missions and loop safety
- Score gate. Print Excellent for 90+, Passed for 60–89, or Try again below 60. Test 59, 60, 89, and 90.
- Add 1 through 10. Use
range()and an accumulator, notsum(). Verify the answer is 55. - Safe countdown. Ask for a starting integer and count down with
while. Make sure one value changes every turn and decide what 0 should do.
Sharp corners: text passed to int() raises ValueError; Chapter 10 will handle that. Missing a colon or four-space indent causes a syntax or indentation error. A while condition that can never become false creates an infinite loop—press Ctrl+C, then find the missing update.
Ready to name repeated jobs?
- I can make Booleans with all six comparison signs.
- I know
=assigns and==compares. - I can write one clear
if/elif/elsechain. - I can combine conditions with
and,or, andnot. - I know that the stop number in
range()is not included. - I can keep a total and a counter across a loop.
- I can explain exactly why my
whileloop stops. - My tracker reaches the expected 210-minute summary.
- I completed the three tiny missions.
Next, you will put repeated work inside named functions so longer programs stay easy to read and test.