Grade VIII Computer Science

Chapter 7 : Advanced Python

Session 1 : Loops, Iterative Statements & the for Loop

Learning Objectives

After completing this lesson, students will be able to:

Prerequisite Knowledge

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

  1. Initialize the loop variable.
  2. Take the next value from range().
  3. Execute the statements inside the loop.
  4. Repeat until all values are exhausted.
  5. Exit the loop.

Advantages of the for Loop

Classroom Activity

  1. Print numbers from 1 to 20.
  2. Print all even numbers from 2 to 30.
  3. Print all odd numbers from 1 to 25.
  4. Print your name 10 times.
  5. Print the multiplication table of 7.

Lab Activity

Write Python programs to:

Quick Check

  1. What is a loop?
  2. What is an iterative statement?
  3. When should a for loop be used?
  4. What is the purpose of the range() function?
  5. What will range(3) generate?
  6. What is the difference between range(5) and range(1,6)?

Homework

  1. Print numbers from 1 to 100.
  2. Print all multiples of 5 from 5 to 100.
  3. Print the multiplication table of 9.
  4. Print numbers from 20 down to 1.
  5. Print the first 15 natural numbers using a for loop.

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.