1 of 44

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

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

2 of 44

NESTED LOOPS

Key Concept #2

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

3 of 44

Concept Overview

Coding Concept

Sub Concept

Nested Loops

  • for loop
    • Nested loop
  • while loop
  • Looping using multiple conditions
  • Nested loop
  • Control statements in nested loops
  • break, pass, continue
  • Understanding loops through Turtle module

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

4 of 44

  • Define nested loops with examples.
  • Apply correct syntax of nested loops in Python program.
  • Differentiate between nested for and while loop with examples.
  • Use nested loops in Python program to solve problems.
  • Apply pass, break and continue statements to control nested loops.
  • Draw shapes and patterns using nested loops in Turtle module.
  • Explain the importance of nested loops in Python programs.

Objectives

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

5 of 44

Nested for Loop

ACTIVITIES

  1. Understanding Nested for Loop
  2. Demo 1 - Looping with Nested for Loop
  3. Activity 1 - Nested for Loop
  4. Activity 3 - Generate Pattern
  5. Activity 4 - Nested Loop in Turtle Module

1

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

6 of 44

Understanding for Loop

  • A for loop in Python is used to iterate over a sequence (such as a list, tuple, string, or range) or any other iterable object.
  • It repeatedly executes a block of code until the sequence is exhausted.

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

7 of 44

Flowchart

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

8 of 44

Nested Loops

  • A nested loop is a loop inside a loop.
  • Nested loops are used to perform operations that require more than one level of iteration.
  • The inner loop is executed repeatedly for each iteration of the outer loop.

Nested Loops

Nested while loop

Nested for loop

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

9 of 44

Nested for Loop

  • for loop is used for repeated execution of a group of statements for the desired number of times.
  • Nested for loop refers to the placement of a for loop inside a 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

10 of 44

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

11 of 44

Flowchart for Nested for Loop

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

12 of 44

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

13 of 44

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

14 of 44

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

15 of 44

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

16 of 44

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

17 of 44

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

18 of 44

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

19 of 44

Nested while Loop

ACTIVITIES

  1. Understanding nested while loop
  2. Activity 1 - Number Pattern using while Loop
  3. Demo 1 - Nested while loop using Tracing Table
  4. Activity 2- Tracing the code
  5. Demo 2 - for loop using Turtle module
  6. Activity 3 - Nested while loop

2

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

20 of 44

While loop

  • while loop repeatedly carries out a target statement while the condition is true.
  • The loop iterates as long as the defined condition is True. When the condition becomes false, it will stop the iteration and execute the statements outside the 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

21 of 44

Flowchart for while Loop

START

Condition

Statement

STOP

Update

False

True

initialize

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

22 of 44

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

23 of 44

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

24 of 44

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

25 of 44

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

26 of 44

Nested while loop

  • Nested while loop is the loop structure where one while loop is placed inside another while loop.
  • It consist of an outer while loop that encloses an inner while loop.
  • The outer loop runs multiple times, and each time it runs, the inner loop also runs from start to finish.
  • The purpose of nested while loop is to perform more complex repetitive tasks.
  • In nested while loops, care must be taken to avoid infinite loops by ensuring proper conditions for both inner and outer loops.

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

27 of 44

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

28 of 44

Flowchart for Nested while Loop

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

29 of 44

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

30 of 44

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

31 of 44

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

32 of 44

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.

  • Writing an algorithm
  • Drawing a flowchart.
  • Writing a python program

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

33 of 44

Activity 3 - Solution

Algorithm

Flowchart

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

34 of 44

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

35 of 44

Nested Loops with Control Statement

ACTIVITIES

  1. Understanding nested loop with Control Statement.
  2. Activity 1 Break in Nested Loop
  3. Activity 2 Continue in Nested Loop
  4. Activity 3 - Pass in Nested loop
  5. Activity 4 - Printing Number patterns

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

36 of 44

Control Statements in Nested loops

  • Loop control statements are used to alter the normal flow of execution within loops.
  • Control statements helps to control the iteration process and modify the behaviour of the loop based on certain conditions.

Types of Control Flow Statements

break

pass

continue

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

37 of 44

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

38 of 44

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

39 of 44

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

40 of 44

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

41 of 44

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

42 of 44

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

43 of 44

  • A 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.
  • One loop can have another loop inside it and it is called nested loop.
  • A 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.
  • With break statement we can stop the loop before it has looped through all the items.
  • The continue keyword is used to end the current iteration in a for loop(or a while loop), and continues to the next iteration.

Key Points

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

44 of 44

བཀྲིན་ཆེ།

THANK YOU

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD