དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག།
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
LOOPS
Key Concept # 12
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Concept Overview
Coding Concept | Sub Concept |
Loops |
|
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Objectives
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Understanding Loops
for
while
Types of Loops
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Importance of Loops
Problem Solving
4
Reduced Code Length
3
Efficiency
2
Repetition and Automation
1
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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
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
RANGE FUNCTION
ACTIVITIES
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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.
Age of students in your class.
Examples
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Understanding range( ) Function
range(start, stop, step)
Syntax
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Understanding range( ) function
range(5) #it generates numbers from 0 to 4
range(2,5) # it generates number from 2, 3, 4
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
Activity - Check Your Understanding
1. State the output of the following range function.
2. Complete the following statements with the most appropriate word.
→ 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
FOR LOOP
ACTIVITIES
1
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
For Loop
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
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
Working of For loop
Watch the video to understand working of for loop
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
WHILE LOOP
ACTIVITIES
2
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
While 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
While Loop Flowchart
False
True
START
Condition
Print statement
STOP
Update
Initialization
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
While Loop
Watch the video to understand the working of while loop
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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
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
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
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
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
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
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
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
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
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
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
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
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
CONTROL STATEMENT IN LOOPS
ACTIVITIES
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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
1. Control Flow Statement - Break
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
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
2. Control Flow Statement - Continue
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
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
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
3. Control Flow Statement - Pass
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
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
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
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 4 - Solution
Iteration | i value | Pass | Continue | Break | |
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 | |||||
1
2
3
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Key Points
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Key Points
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
བཀྲིན་ཆེ།
THANK YOU
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD