← All lessons

Lesson 3

Control Structures and Program Flow in Python

Item 1

Conditions

Conditions let a program make decisions. if runs a block when the condition is True. elif checks another condition. else executes when none of the previous conditions are true. The block must be indented.

if — Runs code when a condition is True.
elif — Checks another condition if the previous was False.
else — Runs when no condition is True.

Try it — edit the code and press Run:

Item 2

Loops

Loops repeat code. A for loop repeats a fixed number of times; range() generates the sequence of numbers. A while loop repeats as long as its condition is True.

for loop — Repeats for each item in a sequence.
range() — Generates a sequence of numbers: range(5) → 0..4.
while loop — Repeats while a condition is True.
Iteration — One repetition of the loop.

Try it — edit the code and press Run:

Item 3

Loop Control: "break" and "continue"

break stops the loop immediately. continue skips the rest of the current iteration and moves to the next one.

break — Terminates the loop.
continue — Skips to the next iteration.

Try it — edit the code and press Run:

Quick Quiz

1. Which keyword checks another condition after if?

2. How many times does for i in range(5) run?

3. What does break do?

4. What does continue do?

5. What is required after if condition in Python?

Score: 0 / 5(Sign in to save your score)

Practice

Auto-checked exercises at three levels. Open hints one by one before the solution.

⭐Basic

Even or odd

n = 7. Print "Odd" if n is odd, otherwise "Even".

⭐⭐Intermediate

Count 1 to 10

Print the numbers 1 to 10, each on a new line.

⭐⭐⭐Challenge

Grade checker

Loop over scores = [95, 72, 48]. For each, print "A" if >= 90, "B" if >= 70, otherwise "F".

Smart Assistant Project

Task 3: Smart Decisions

Goal: Make the assistant respond to the student's results.

Requirement: Using the average from Task 2, print a grade (A/B/C/F) with if/elif/else, then use a loop to let the user enter scores until they type 0.