དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག།
Department of School Education
Ministry of Education & Skills Development
Python Coding
January 2024
Class X ICT Curriculum
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
NESTED LOOPS
Key Concept #2
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Concept Overview
Coding Concept | Sub Concept |
Nested Loops |
|
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Objectives
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Nested for Loop
ACTIVITIES
1
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Understanding for Loop
Code
for iterating_var in sequence:
statement(s)
Syntax
Hello
Hello
Hello
Output
1 2 | for i in range(3): print("Hello") |
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Flowchart
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Nested Loops
Nested Loops
Nested while loop
Nested for loop
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Nested for Loop
for iterating_var in sequence:
for iterating_var in sequence:
statement(s)
statement(s)
Syntax
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Nested for Loop(cont.)
Code
1 2 3 4 | for i in range(3): for j in range(2): print("inside",end=",") |
Output
inside,inside,inside,inside,inside,inside,
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Flowchart for Nested for Loop
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Demo 1 - Looping with Nested for loop
Lets us see how nested for loop work.
1 2 3 | for i in range(4): for j in range(2): print("Hello World!") |
Code
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Demo 1 - Looping with Nested for Loop
Lets us see how nested for loop work.
1 2 3 | for i in range(4): for j in range(2): print("Hello World!") |
Code
Whatever is in this box, iit will execute four times.
Output
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 1 - Combination of Two Dice
Two dice are rolled at the same time. Write a Python program to display all the possible combinations of the two dice. A sample output is given below (Hint: use nested for loops).
1 2 3 4 5 6 | for i in range(1,7): for j in range(1,7): print(i,j," ", end=" ") print() |
Code
Output
1 1 1 2 1 3 1 4 1 5 1 6
2 1 2 2 2 3 2 4 2 5 2 6
3 1 3 2 3 3 3 4 3 5 3 6
4 1 4 2 4 3 4 4 4 5 4 6
5 1 5 2 5 3 5 4 5 5 5 6
6 1 6 2 6 3 6 4 6 5 6 6
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 2 - Multiplication Table of 2 and 3
Write a Python program to generate multiplication table of 2 and 3 upto 5. Sample output is given below.
1 2 3 4 | for i in range(2, 4): for j in range(1, 6): print(i, "X", j, "=", i*j) print() |
Output
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
Code
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 3 - Generating Pattern
Write a Python program to generate pattern given below
*
* *
* * *
* * * *
* * * * *
Code
1 2 3 4 5 6 | rows = 5 for i in range(0, rows): for j in range(0, i + 1): print("*", end=' ') print("\t") |
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 3 - Printing Square Pattern
Write a Python program that uses nested for loops to print a square pattern made of asterisks .
Code
1 2 3 4 | for i in range(4): for j in range(4): print("*", end=" ") print() |
* * * *
* * * *
* * * *
* * * *
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 4 - Nested Loop in Turtle Module
Write a Python program to generate pattern given below using Turtle module
Code
1 2 3 4 5 6 7 | from turtle import * setup(150,150) for i in range(4): for j in range(4): forward(50) right(90) left(90) |
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Nested while Loop
ACTIVITIES
2
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
While loop
while condition:
statement(s)
Syntax
Hello inside, Hello inside,
Output
Code
1 2 3 4 | x=1 while x<3: print("Hello inside", end= “,”) x=x+1 |
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Flowchart for while Loop
START
Condition
Statement
STOP
Update
False
True
initialize
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Example 1 - while Loop with Multiple Conditions
Given below is the example of Python program, where while loop with multiple conditions are used.
20 15
15 11
10 7
5 3
1 2 3 4 5 6 | i = 20 j = 15 while i > 0 and j > 0 : print(i,j) i -= 5 j -= 4 |
Output
Code
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 1 - Printing Even Numbers below 40
Write a Python program that prints even numbers between 1 and 40 using a while loop with multiple conditions.
1 2 3 4 | for num in range(2,40): while num<=40 and num%2==0: print(num, end=",") num+=1 |
Code
2,4,6,8,10,12,14,16,18,20,22,24,26,30,32,34,36,38,40
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 1 - Printing Even Numbers below 40
Write a Python program that prints even numbers between 1 and 40 using a while loop with multiple conditions.
1 2 3 4 | for num in range(2,40): while num<=40 and num%2==0: print(num, end=",") num+=1 |
Code
2,4,6,8,10,12,14,16,18,20,22,24,26,30,32,34,36,38,40
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 2 - Listing Multiples of 3 and 5
Write a Python program that prints numbers divisible by both 3 and 5 in the range from 1 to 100 using a while loop with multiple conditions.
1 2 3 4 | for num in range(1,100): while num%3==0 and num%5==0: print(num, end=",") num+=1 |
Code
15,30,45,60,75,90
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Nested while loop
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Code
Inner loop
Outer loop
Outer loop
Output
while condition_outer:
# Body of outer loop
while condition_inner:
# Body of inner loop
Syntax
1 2 3 4 5 6 7 8 9 | x=1 y=1 while x<3: while y<2: print("Inner loop") y=y+1 print("Outer loop") x=x+1 |
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Flowchart for Nested while Loop
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Demo 1 - Tracing a Nested while loop
Study the code given below and trace the output of the code.
Code
1 2 3 4 5 6 7 | i = 1 while i <=2: j = 1 while j <= 2: print(i,”X”,j,”=”,i*j) j = j+1 i = i+1 |
Output
1 X 1 = 1
1 X 2 = 2
2 X 1 = 2
2 X 2 = 4
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Demo 1 - Tracing a Nested while loop
Study the code given below and trace the output of the code.
Code
1 2 3 4 5 6 7 8 | i = 1 while i <=2: j = 1 while j <= 2: print(i,”X”,j,”=”,i*j) j = j+1 i = i+1 |
i | i<=2 | j | j<=2 | i*j | j=j+1 | i=i+1 |
1 | True | 1 | True | 1 | 2 | - |
1 | - | 2 | True | 2 | 3 | - |
1 | - | 3 | False | - | - | 2 |
2 | True | 1 | True | 2 | 2 | - |
2 | - | 2 | True | 4 | 3 | 3 |
3 | False | - | - | - | - | - |
Output Tracing
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 2 - Tracing the code
Study the code given below and trace the output.
Code
1 2 3 4 5 6 7 | i = 1 while i <=2: j = 2 while j <= 3: print(i*j) j = j+1 i = i+1 |
i | i<=2 | j | j<=3 | i*j | j=j+1 | i=i+1 |
1 | True | 2 | True | 2 | 3 | - |
1 | - | 3 | True | 3 | 4 | - |
1 | - | 4 | False | - | - | 2 |
2 | True | 2 | True | 4 | 3 | - |
2 | - | 3 | True | 6 | 4 | 3 |
3 | False | - | - | - | - | - |
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 3 - Nested while loop
Sonam wants to generate multiplication table of 1, 2 and 3 upto 10 using nested while loop. She wants your help on the following questions.
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 3 - Solution
Algorithm
Flowchart
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 3 Solution(cont.)
1 2 3 4 5 6 7 8 | n = 1 while n <= 3: i = 1 while i <= 10: print(f"{n} X {i} = {n*i}") i += 1 print() n += 1 |
Code
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Nested Loops with Control Statement
ACTIVITIES
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Control Statements in Nested loops
Types of Control Flow Statements
break
pass
continue
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Break Statement
The break statement is used to terminate the loop prematurely. When encountered, it immediately exits the loop, regardless of whether the loop condition has been satisfied or not.
1 2 3 4 5 6 | i = 1 while i < 6: print(i) if i ==3: break i += 1 |
Code
Example
i | i<=6 | print(i) | if i==3 | i=i+1 |
1 | True | 1 | - | 2 |
2 | True | 2 | - | 3 |
3 | True | 3 | break | - |
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Continue Statement
The continue statement is used to skip the remaining code in the current iteration of the loop and move to the next iteration. It is often used when you want to skip certain iterations based on a condition and continue with the next 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
Code
Output
Example
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 2 Continue in Nested Loop
1 2 3 4 5 | for i in range(1,3): for j in range(2): if i == j: continue print(i*j) |
Study the code given below and trace the output .
Code
i | j | if i==j | i*j |
1 | 0 | False | 0 |
1 | 1 | True | - |
2 | 0 | False | 0 |
2 | 1 | False | 2 |
Output Tracing
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Pass Statement
The pass statement is used as a placeholder when you need a statement syntactically correct but don't want any code to execute. It is commonly used as a placeholder for a block of code that you intend to implement later.
1 2 3 | i = 1 while i < 6: pass |
Code
Example
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 4 - Printing Number Patterns
1 1 2 2 4 6 3 9 12 4 16 20 5 25 30 6 36 42 . . .
Study and identify the number pattern given below and write a python code using nested loop to generate number pattern.
1 2 3 4 5 6 7 8 | i=1 j=1 while i<10: print(i,end="") while j<=i: print(" ",i*j,"",i*j+i,end=" ") j=j+1 i=i+1 |
Code
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Key Points
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
བཀྲིན་ཆེ།
THANK YOU
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD