1 of 64

དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག།

Department of School Education

Ministry of Education & Skills Development

Python Coding

January 2024

Class IX ICT Curriculum

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

2 of 64

LOOPS

Key Concept # 12

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

3 of 64

Concept Overview

Coding Concept

Sub Concept

Loops

  • Definition of loops
  • Importance of loops
  • for loop
  • Looping through string
  • Looping through range function
  • while loop
  • Looping using the single condition
  • Control statements in loops
  • Break, Pass, Continue
  • Understanding loops using turtle module

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

4 of 64

  • Define loops with examples.
  • Explain the importance of using loops in Python programs.
  • Apply correct for loop and while loop syntax in a Python program.
  • Differentiate between for and while loop with examples.
  • Use different loops to solve Python programs.
  • Use control flow statements (break, continue, pass) to alter the normal flow of a program with loops.
  • Draw shapes using loops in Turtle module.

Objectives

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

5 of 64

Understanding Loops

for

while

Types of Loops

  • A loop is a sequence of instructions that is executed repeatedly as long as a certain condition is true.
  • The basic idea behind a loop is to automate repetitive tasks. It performs a specific action multiple times without having to write the same code over and over again.

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

6 of 64

Importance of Loops

Problem Solving

4

  • Solves repetitive problems.

Reduced Code Length

3

  • Reduces the overall length of code, making it more concise and easier to manage.

Efficiency

2

  • Loops improve the efficiency of code by avoiding redundancy.

Repetition and Automation

1

  • Automates repetitive tasks.
  • Avoids redundant code.

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

7 of 64

Demo 1 - Loops

Write a Python program that gives the following output. Write down the solution in your notebook before testing it in Python IDLE.

Coding is awesome

Coding is awesome

Coding is awesome

Coding is awesome

Coding is awesome

Coding is awesome

Coding is awesome

Coding is awesome

Coding is awesome

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

8 of 64

Demo 1 - Solution

1

2

for i in range(9):

print(“Coding is awesome”)

1

2

3

4

i=1

while i<=9:

print("Coding is awesome")

i=i+1

1

2

3

4

5

6

7

8

9

print("Coding is awesome")

print("Coding is awesome")

print("Coding is awesome")

print("Coding is awesome")

print("Coding is awesome")

print("Coding is awesome")

print("Coding is awesome")

print("Coding is awesome")

print("Coding is awesome")

Without loop

With while loop

With for loop

Note: Using loops will reduce the line of codes making the program more efficient.

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

9 of 64

RANGE FUNCTION

ACTIVITIES

  1. Understanding Range Function
  2. Activity - Check Your Understanding

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

10 of 64

Understanding Range

In everyday life, a range often refers to a series or sequence of things that follow a certain order or pattern.

Marks scored in ICT out of 100.

  • Students will get different marks in between 0 and 100. The range of marks is difference of highest and lowest marks.

Age of students in your class.

  • In this class there are students of different ages from 15 years to 18 years. The range is 15-18.

Examples

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

11 of 64

Understanding range( ) Function

  • The range( ) function returns a sequence of numbers.
  • The range function can have one, two or three arguments.

  • start (optional) - the starting number of the sequence; the default is 0.
  • stop (required) - the number to stop the sequence; this number is not included in the sequence.
  • step (optional) - it refers to an incremental value. The default value is 1.

range(start, stop, step)

Syntax

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

12 of 64

Understanding range( ) function

  1. range(stop)
  2. This form generates a sequence of numbers starting from 0 up to (but not including) the specified stop value. The step size is 1 by default.
  3. Example:

range(5) #it generates numbers from 0 to 4

  1. range(start, stop)
  2. This form generates a sequence of numbers starting from the specified start value and ending just before the specified stop value. The step size is 1 by default.
  3. Example:

range(2,5) # it generates number from 2, 3, 4

  1. range(start, stop, step)
  2. This form generates a sequence of numbers starting from the start value and ending just before the stop value, with increments of the specified step value.
  3. Example:

range(1, 10, 3) # output: 1, 4, 7

The range() function has three main forms.

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

13 of 64

Activity - Check Your Understanding

1. State the output of the following range function.

  • range(7)
  • range(2, 9)
  • range(2, 11, 2)

2. Complete the following statements with the most appropriate word.

  1. A loop is a sequence of instructions that is executed repeatedly as long as a certain condition is ____
  2. If there are two arguments in range function then the default increment value is __
  3. If there is only one argument then default starting value in a range function ___

0,1,2,3,4,5,6

2,3,4,5,6,7,8

2,4,6,8,10

True

1

0

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

14 of 64

FOR LOOP

ACTIVITIES

  1. Understanding For Loop
  2. Demo 1 - Looping Through String.
  3. Demo 2 - Looping Through Number.
  4. Activity 1 - Displaying first 5 Even Numbers
  5. Activity 2 - Accessing Characters in String
  6. Activity 3 - Displaying Multiplication Table
  7. Demo 3 - Drawing Triangle
  8. Activity 4 - Drawing Square
  9. Activity 5 - Drawing Hexagon
  10. Activity 6 - Displaying the First Three Square Numbers
  11. Activity 7 - Displaying Multiple of 7
  12. Activity 8 - Finding Roots of Number in a List.
  13. Activity 9 - Finding Sum of the First 5 Multiples of 7
  14. Activity 10 - Finding the Sum of Numbers in a List

1

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

15 of 64

For Loop

  • In Python, for loop is used for repeated execution of a group of statements for the desired number of times.
  • It iterates over the items of strings, lists, tuples, dictionaries and other iterable objects.

for variable in sequence:

statements

1

2

for i in range(6):

print(i)

Syntax

Code

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

16 of 64

For Loop Flowchart

START

i in [0,1,2]?

Print i

STOP

False

True

1

2

for i in range(3):

print(i)

Code

Iteration

i value

condition

Print i

1

0

0 in [0,1,2] [True]

0

2

1

1 in [0,1,2][True]

1

3

2

2 in [0,1,2] [True]

2

4

3

3 in [0,1,2] [False]

Exit out of the loop

Final Output: 0, 1, 2

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

17 of 64

Working of For loop

Watch the video to understand working of for loop

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

18 of 64

Demo 1 - Looping Through String

Write a Python program to display characters in the string ”PYTHON”

1

2

for x in “PYTHON”:

print(x)

P

Y

T

H

O

N

Code

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

19 of 64

Demo 2 - Looping Through Number

Write a Python program that displays the first five whole numbers. Use for loop and workout the tracing table for the program.

1

2

3

#Displaying the first 5 numbers

for i in range(5):

print(i)

Code

Output

0

1

2

3

4

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

20 of 64

Demo 2 - Solution

Iteration

i value

condition

Print i

1

0

0 in [0,1,2,3,4] [True]

0

2

1

1 in [0,1,2,3,4] [True]

1

3

2

2 in [0,1,2,3,4] [True]

2

4

3

3 in [0,1,2,3,4] [True]

3

5

4

4 in [0,1,2,3,4] [True]

4

6

5

5 in [0,1,2,3,4][False]

Exit out of the loop

Final Output: 0, 1, 2, 3, 4

Given alongside is the tracing table for the code below.

1

2

for i in range(5):

print(i)

Code

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

21 of 64

Activity 1 - Displaying First 5 Even Numbers

Write a Python program that displays the first five even numbers using for loop. Workout the tracing table for the program.

1

2

for num in range(2, 11, 2):

print(num)

1

2

3

for num in range(2,11):

if num%2==0:

print(num)

Solution 1

Solution 2

Note: There are multiple solutions to solve a problem as given above. In solution 2 if condition and % operator are used while incremental value in range is used in solution 1.

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

22 of 64

Activity 1 - Displaying First 5 Even Numbers

Iteration

i value

condition

Print i

1

2

2 in [2,4,6,8,10] [True]

2

2

4

4 in [2,4,6,8,10] [True]

4

3

6

6 in [2,4,6,8,10] [True]

6

4

8

8 in [2,4,6,8,10] [True]

8

5

10

10 in [2,4,6,8,10] [True]

10

6

12

12 in [2,4,6,8,10] [False]

Exit out of the loop

Final Output: 2, 4, 6, 8, 10

Given below is the tracing table of solution 1 for the problem activity 1.

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

23 of 64

Activity 2 - Accessing Characters in String

Write a Python program that displays each character of the string stored in following variable.

message = "BHUTAN"

1

2

3

message = ”BHUTAN”

for c in message:

print(c)

Code

Output

B

H

U

T

A

N

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

24 of 64

Activity 3 - Displaying Multiplication Table

Write a Python program to generate a multiplication table of a number entered by a user. Limit the multiples of the number till 10.

1

2

3

n = int(input(“Enter a number:”))

for i in range(1,11):

print(f’{i} X {n} = {i*n}’)

Code

Enter a number:5

1 X 5 = 5

2 X 5 = 10

3 X 5 = 15

4 X 5 = 20

5 X 5 = 25

6 X 5 = 30

7 X 5 = 35

8 X 5 = 40

9 X 5 = 45

10 X 5 = 50

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

25 of 64

Demo 3 - Drawing Triangle

Write a Python program using the Turtle module to draw a triangle with side length of 200 pixels. Use for loop.

1

2

3

4

5

6

7

from turtle import *

shape(“turtle”)

title(“Drawing Triangle”)

for i in range(3):

forward(200)

left(120)

done()

Code

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

26 of 64

Activity 4 - Drawing Square

Write a Python program using the Turtle module to draw a square with side length of 200 pixels. Use for loop.

1

2

3

4

5

6

7

from turtle import *

title(“Drawing Square”)

shape(“turtle”)

for i in range(4):

forward(200)

right(90)

done()

Code

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

27 of 64

Activity 5 - Drawing Hexagon

Write a Python program using the turtle module to draw a hexagon with sides of 100 pixels. Use while loop.

1

2

3

4

5

6

7

from turtle import *

title(“Drawing Hexagon”)

shape(“turtle”)

for i in range(6):

forward(100)

right(60)

done()

Code

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

28 of 64

Activity 6 - Displaying First 3 Square Numbers

Write a Python program that displays the square of the first three natural numbers.

1

2

for i in range(1,4):

print(f’The square of {i} is {i**2}’)

Code

The square of 1 is 1

The square of 2 is 4

The square of 3 is 9

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

29 of 64

Activity 7 - Displaying Multiples of 7

Write a Python program to display first five multiples of 7.

1

2

for i in range(7,36,7):

print(i)

1

2

3

for i in range(1,36):

if i%7==0:

print(i)

Solution 1

Solution 2

7

14

21

28

35

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

30 of 64

Activity 8 - Finding Roots of Numbers in a List

Write a Python program that displays the square root of the numbers given in the list [1, 4, 9, 16, 25]. Use for loops.

1

2

3

numbers = [1, 4, 9, 16, 25]

for n in numbers:

print(f’The square root of {n} is {n**0.5}’)

Code

The square root of 1 is 1.0

The square root of 4 is 2.0

The square root of 9 is 3.0

The square root of 16 is 4.0

The square root of 25 is 5.0

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

31 of 64

Activity 9 - Finding Sum of the first 5 multiples of 3

Write a Python program using for loop to display the sum of the first 5 multiples of 3.

1

2

3

4

total = 0

for i in range(3,16,3):

total += i

print(“Total:”,total)

Solution 1

Solution 2

1

2

3

4

5

total = 0

for i in range(1,16):

if i%3==0:

total += i

print(“Total:”,total)

Total:45

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

32 of 64

Activity 10 - Finding Sum of Numbers in a List

Sonam has collected money from his friends to buy brooms for the classroom. He collected Nu. 5, 10, 15, 20, and 25. Write a Python program using for loop to calculate the total amount collected by Sonam.

1

2

3

4

total = 0

for n in [5,10,15,20,25]:

total = total + n

print(“Total:”,total)

1

2

3

4

total = 0

for n in range(5,26,5):

total = total + n

print(“Total :”,total)

Solution 2

Solution 1

Total:75

Output

Total:75

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

33 of 64

WHILE LOOP

ACTIVITIES

  1. Understanding While Loop
  2. Demo: Generating Number Series 0-4
  3. Activity 1: Displaying Message
  4. Activity 2 - Displaying the first five even number
  5. Activity 3 - Finding sum of first 100 numbers
  6. Activity 4 - Calculating the Factorial of number
  7. Activity 5 - Finding average of numbers
  8. Activity 6 - Displaying Multiplication Table
  9. Activity 7 - Finding Sum of the first 5 multiples of 4
  10. Activity 8 - Drawing Hexagon
  11. Activity 9 - Drawing Octagon

2

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

34 of 64

While Loop

  • A while loop is used to repeatedly execute a block of code as long as a certain condition remains true.
  • After each iteration, the condition is evaluated again. If the condition is still true, the loop continues. If the condition becomes false, the loop is exited, and the program continues with the next statement after the loop.

while condition(s):

Statements…

………………………

Syntax

1

2

3

4

x=1

while x<=3:

print(“དཔལ་ལྡན་འབྲུག་པ།”)

x=x+1

Example

དཔལ་ལྡན་འབྲུག་པ།།

དཔལ་ལྡན་འབྲུག་པ།།

དཔལ་ལྡན་འབྲུག་པ།།

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

35 of 64

While Loop Flowchart

False

True

START

Condition

Print statement

STOP

Update

Initialization

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

36 of 64

While Loop

Watch the video to understand the working of while loop

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

37 of 64

Demo - Generating Numbers 0 - 4

Write an algorithm and Python program to generate numbers from 0 - 4 using while loop. Workout a tracing table for the program.

Step 1: START

Step 2: Initialize i to 0

Step 3: While i is less than 5, repeat Step 4 to Step 5

Step 4: Print i

Step 5: Increase the value of i by 1

Step 6: END

Algorithm

1

2

3

4

i = 0

while i < 5:

print(i)

i = i + 1

Code

0

1

2

3

4

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

38 of 64

Demo - Generating Numbers 0 - 4

Iteration

i value

condition

Print i

New i value

[i = i + 1]

1

0

0 < 5 [True]

0

i = 0 + 1

2

1

1 < 5 [True]

1

i = 1 + 1

3

2

2 < 5 [True]

2

i = 2 + 1

4

3

3 < 5 [True]

3

i = 3 + 1

5

4

4 < 5 [True]

4

i = 4 + 1

6

5

5 < 5 [False]

Exit out of the loop

Output: 0, 1, 2, 3, 4

1

2

3

4

i = 0

while i < 5:

print(i)

i = i + 1

Code

Tracing table for the following code.

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

39 of 64

Activity 1 - Displaying Message

Write a Python program to display the message “I am awesome!” for 5 times using the while loop.

1

2

3

4

5

i = 1

while i <= 5:

print(“I am awesome!”)

i = i + 1

I am awesome!

I am awesome!

I am awesome!

I am awesome!

I am awesome!

Code

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

40 of 64

Activity 2 - Displaying the First Five Even Numbers

Write a Python program to display the first five even numbers using the while loop.

1

2

3

4

5

6

i = 2

print(“First 5 even numbers:”)

while i <= 10:

print(i)

i += 2

Code

First 5 even numbers:

2

4

6

8

10

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

41 of 64

Activity 3 - Finding Sum of First 100 Numbers

Write a Python program using while loop to calculate the sum of numbers from 1 to 100.

1

2

3

4

5

6

i = 1

total = 0

while i <= 100:

total += i

i += 1

print(f“Sum of 100 numbers:{total}”)

Code

Sum of 100 numbers:5050

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

42 of 64

Activity 4 - Calculating the Factorial of a Number

Write a Python program using the while loop to calculate the factorial of the number entered by the user. Hint: factorial of n! = n×(n−1)×(n−2)×………..×3×2×1. 3! = 3x2x1 = 6

1

2

3

4

5

6

7

n = int(input(“Enter a number to find the factorial:”))

i = 1

fact = 1

while i <= n:

fact = fact*i

i += 1

print(f’The factorial of {n} is {fact}’)

Code

Enter a number to find the factorial:5

The factorial of 5 is 120

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

43 of 64

Activity 5 - Finding Average of Numbers

Write a Python program using while loop to find the average for the given numbers.

3, 6, 9, 12, 15, 18, 21

1

2

3

4

5

6

7

i = 3

total = 0

while i <= 21:

total = total+i

i += 3

average = total/7

print(f’Average of numbers:{average}’)

Code

Average of numbers:12.0

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

44 of 64

Activity 6 - Displaying Multiplication Table of a number

Write a Python program using while loop to display multiplication table of the number entered by a user.

1

2

3

4

56

n = int(input(“Enter a number:”))

i = 1

while i <= 5:

print(f’{n} X {i} = {n*i}’)

i += 1

Code

Enter a number:10

10 X 1 = 10

10 X 2 = 20

10 X 3 = 30

10 X 4 = 40

10 X 5 = 50

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

45 of 64

Activity 7 - Finding Sum of the first 5 multiples of 4

Write a Python program using while loop to display the sum of the first 5 multiples of 4.

1

2

3

4

5

6

total = 0

i = 4

while i <= 20:

total = total + i

i = i + 4

print(“Total:”,total)

1

2

3

4

5

6

7

Total = 0

i = 1

while 1 <= 20:

if i%4==0:

total = total + i

i = i + 1

print(“Total:”,total)

Solution 1

Solution 2

Total:60

Output

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

46 of 64

Activity 8 - Drawing Hexagon

Write a Python program using the turtle module to draw a hexagon with sides of 100 pixels. Use while loop.

1

2

3

4

5

6

7

8

from turtle import *

title(“Drawing Hexagon”)

shape(“turtle”)

i = 1

while i <= 6:

forward(100)

right(60)

i += 1

Code

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

47 of 64

Activity 9 - Drawing Octagon

Write a Python program using the turtle module to draw a octagon with sides of 100 pixels. Use while loop.

1

2

3

4

5

6

7

8

from turtle import *

title(“Drawing Octagon”)

shape(“turtle”)

i = 1

while i <= 8:

forward(100)

right(45)

i += 1

Code

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

48 of 64

Activity 8 - Drawing Octagon

Write a Python program using the turtle module to draw a octagon with sides of 100 pixels. Use while loop.

1

2

3

4

5

6

7

8

from turtle import *

title(“Drawing Octagon”)

shape(“turtle”)

i = 1

while i <= 8:

forward(100)

right(45)

i += 1

Code

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

49 of 64

Differences Between While and For Loop

Feature

for loop

while loop

Syntax

Iterates over a sequence (eg: list, tuple, string).

Executes a block of code as long as a condition is true.

Initialization

Automatically initialized with each item in the sequence.

Requires explicit initialization of loop control variable.

Condition

Relies on the length of the sequence or iterable object.

Depends on a condition that must be explicitly specified.

Control

Controlled by the length of the sequence or iterable object.

Controlled by the condition provided in the loop header.

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

50 of 64

CONTROL STATEMENT IN LOOPS

ACTIVITIES

  1. Understanding Control Statement
  2. Demo 1 - Exiting the Loop
  3. Demo 2 - Skipping an Iteration
  4. Activity 1 - Skipping Odd Numbers From a Range
  5. Activity 2 - Check Your Understanding
  6. Activity 3 - Break, Pass, & Continue

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

51 of 64

Flow Control Statements

Flow Control statements alter the normal flow of control in a program having loops.

Types of Flow Control Statements

break

continue

pass

Terminates the loop statement.

Skips the current iteration and jumps to the next iteration.

Acts as a placeholder, doing nothing when executed

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

52 of 64

1. Control Flow Statement - Break

  • The break statement is used to control the sequence of the loop when its external condition is met.
  • We use break keyword to exit out of a loop.

1

2

3

4

5

6

for num in [11, 9, 88, 10, 90, 3, 19]:

print(num)

if num==88:

print("88 is found")

print("Loop Terminates ")

break

11

9

88

88 is found

Loop Terminates

Code

Output

Example

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

53 of 64

Demo 1 - Exiting the Loop

Write a Python program that stops displaying numbers from a range of 0-6 when it reaches number 3. Hint- Use for loop and break control flow statement.

1

2

3

4

5

for x in range(6):

if x == 3:

print("Loop terminated")

break

print(f'x value is {x}')

x value is 0

x value is 1

x value is 2

Loop terminated

Output

Code

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

54 of 64

2. Control Flow Statement - Continue

  • The continue statement is used to stop the current iteration in a loop and continue to the next iteration.
  • We use continue keyword to skip a current statement but continue the iteration.

1

2

3

4

5

6

i = 0

while i < 6:

i += 1

if i == 3:

continue

print(i)

1

2

4

5

6

Output

Code

Example

Value 3 has been skipped in the output and the program has continued with 4.

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

55 of 64

Demo 2 - Skipping an Iteration

Write a Python program using for loop and continue control flow statement to display odd numbers less than 10.

1

2

3

4

5

for i in range(1,11):

if i%2==0:

continue

print(i)

1

3

5

7

9

Output

Code

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

56 of 64

Activity 1 - Skipping Odd Numbers From a Range

Write a Python program that skips the odd numbers from the range of numbers from 1 to 11.

1

2

3

4

for i in range(1,11):

if i%2 != 0:

continue

print(“Even Number: ”,i)

Even Number: 2

Even Number: 4

Even Number: 6

Even Number: 8

Even Number: 10

Output

Code

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

57 of 64

3. Control Flow Statement - Pass

  • The pass statement is a null statement which can be used as a placeholder for future code.
  • The pass statement is used in the process of developing or prototyping code that needs a temporary solution to avoid syntax errors.
  • Nothing happens when the pass is executed.

1

2

3

i = 1

while i < 6:

pass

Code

Output

The output will be empty to indicate that there is no error.

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

58 of 64

Activity 2 - Check Your Understanding

What will be the output of the following code? Choose the correct answer.

Display numbers from 2 - 6

Display syntax error.

1

2

for i in range(2,7):

pass

Code

A

Display empty screen

Display the text pass

B

C

D

Display empty screen

C

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

59 of 64

Activity 3 - Pass

1

2

3

4

5

6

7

8

9

for i in range(11):

if i==2:

pass

elif i==3

continue

elif ==6

break

else:

print(i)

Study the following code carefully and complete the following tasks.

Code

  1. Design a tracing table for the code.
  2. Write the output of the program.
  3. What did you notice about the program in terms of break, pass and continue.

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

60 of 64

Activity 4 - Applying Control Flow Statements

1

2

3

4

5

6

7

8

9

for i in range(11):

if i==2:

pass

elif i==3:

continue

elif i==6:

break

else:

print(i)

Study the following code carefully and complete the following tasks.

Code

  1. Design a tracing table for the code.
  2. What numbers will be printed?.
  3. What did you notice about the program in terms of break, pass and continue.

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

61 of 64

Activity 4 - Solution

Iteration

i value

Pass

Continue

Break

Print

1st

0

False

False

False

0

2nd

1

False

False

False

1

3rd

2

True

-

-

-

4th

3

False

True

-

-

5th

4

False

False

False

4

6th

5

False

False

False

5

7th

6

False

False

True

Terminate loop

The numbers 0, 1, 4, 5 will be printed

  • During 3rd iteration, the value of i is equal to 2, so it is passed and not printed.
  • During 4th iteration, the value of i is equal to 3, so the code is continued without printing.
  • During 7th iteration, the value of i is equal to 6, so the loop terminates prematurely.

1

2

3

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

62 of 64

  • A loop is a sequence of instructions that is executed repeatedly until a specific condition is met.
  • The range() function returns a sequence of numbers and it can have one, two or three arguments.
  • Loops in Python automate repetitive tasks, iterate over data, and streamline code execution, boosting productivity and efficiency.
  • for loop is used to iterate over a known number of items in a sequence and the loop variable is automatically set to the next item on each iteration.
  • while loop is used to repeatedly execute a block of code as long as a certain condition is true. The condition is checked at the beginning of each iteration.

Key Points

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

63 of 64

  • With break statements, we can prematurely terminate loop when an external condition is met.
  • The continue keyword is used to end the current iteration in a for loop(or a while loop), and continues to the next iteration.
  • The pass statement is a null statement which can be used as a placeholder for future code.
  • Control statements are important for directing program flow, making decisions, iterating over data, handling errors, and enabling customization in order to write dynamic and efficient code.

Key Points

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD

64 of 64

བཀྲིན་ཆེ།

THANK YOU

January 2024

PYTHON

Key stage IV

Class IX

© ICT Curriculum, MoESD