Learning Objectives
After completing this lesson, students will be able to:
- Understand the need for loops.
- Define an iterative statement.
- Explain the purpose of the
forloop. - Use the
range()function. - Write Python programs using
forloops.
Prerequisite Knowledge
- Variables
- Input and Output
- Python Operators
- Conditional Statements (
if)
Why Do We Need Loops?
Suppose you want to print "Welcome" 100 times.
Without a loop, you would need to write the print() statement 100 times.
Loops allow us to execute the same block of code repeatedly without rewriting it.
A loop saves time, reduces errors and makes programs shorter and easier to understand.
What is a Loop?
A loop is a programming structure that repeats a block of code until a specified condition is met or for a fixed number of times.
What is an Iterative Statement?
An iterative statement is a statement that repeatedly executes one or more instructions. In Python, loops are iterative statements.
| Iterative Statement | Purpose |
|---|---|
for loop |
Repeats code for a fixed number of times. |
while loop |
Repeats code while a condition is true. |
The for Loop
The for loop is used when the number of repetitions is already known.
The
for loop works with the range() function to repeat instructions multiple times.
Syntax of the for Loop
for variable in range(start, stop, step):
statement(s)
| Parameter | Description |
|---|---|
| start | Beginning value (optional) |
| stop | Ending value (not included) |
| step | Increment value (optional) |
Understanding range()
| Statement | Output |
|---|---|
range(5) |
0 1 2 3 4 |
range(1,6) |
1 2 3 4 5 |
range(2,11,2) |
2 4 6 8 10 |
range(10,0,-2) |
10 8 6 4 2 |
Example 1 : Print Numbers 1 to 5
for i in range(1,6):
print(i)
Output
1
2
3
4
5
Example 2 : Print "Hello" Five Times
for i in range(5):
print("Hello")
Output
Hello
Hello
Hello
Hello
Hello
Example 3 : Print Even Numbers
for i in range(2,21,2):
print(i)
Output
2
4
6
8
10
12
14
16
18
20
Example 4 : Print Multiplication Table of 5
for i in range(1,11):
print("5 ×", i, "=", 5*i)
Flow of a for Loop
- Initialize the loop variable.
- Take the next value from
range(). - Execute the statements inside the loop.
- Repeat until all values are exhausted.
- Exit the loop.
Advantages of the for Loop
- Reduces repetitive coding.
- Makes programs shorter.
- Easy to read and maintain.
- Suitable when the number of repetitions is known.
- Reduces programming errors.
Classroom Activity
- Print numbers from 1 to 20.
- Print all even numbers from 2 to 30.
- Print all odd numbers from 1 to 25.
- Print your name 10 times.
- Print the multiplication table of 7.
Lab Activity
Write Python programs to:
- Print numbers from 50 to 60.
- Print squares of numbers from 1 to 10.
- Print cubes of numbers from 1 to 10.
- Print numbers from 100 down to 10 with a step of 10.
Quick Check
- What is a loop?
- What is an iterative statement?
- When should a
forloop be used? - What is the purpose of the
range()function? - What will
range(3)generate? - What is the difference between
range(5)andrange(1,6)?
Homework
- Print numbers from 1 to 100.
- Print all multiples of 5 from 5 to 100.
- Print the multiplication table of 9.
- Print numbers from 20 down to 1.
- Print the first 15 natural numbers using a
forloop.
Assessment
| Assessment Method | Description |
|---|---|
| Oral Questioning | Loops, iteration and range(). |
| Worksheet | Predict outputs and write programs. |
| Programming Activity | Develop Python programs using the for loop. |
Summary
In this session, you learned that loops help execute a block of code repeatedly. You explored iterative statements, understood the syntax of the
for loop, learned how the range() function works, and wrote Python programs to print numbers, messages and multiplication tables efficiently.