Form 4 - Computer

2024-2025

Floor 4 - Computer Room

Mr. Peter

While Loop & List

Santa Rosa de Lima English Secondary School

聖羅撒英文中學

Colégio de Santa Rosa de Lima - Secção Inglesa

Outline

List

1.

Explore the concept of List

How to process List in While Loop

2.

Focusing on the List processing in looping

Python List implementation

3.

Define while loop statement for List scanning

What is List?

What is List?

0

1

...

[

]

2

1

4

1

1

1

3

1

boxes = 

In Python:

boxes = [ 1, 1, 1, 1, 1 ]
print( boxes[ 0 ] )

Get variable from List

list[ position ]

Output: 1

What is List?

0

a

"

"

2

c

4

e

1

b

3

d

text = 

We can also get letter from text

text = "abcde"
print( text[ 0 ] )

Get letter from text

Output: a

text[ position ]

Python List Exercises

Python Nested Loops - Ex01

Multiplication Table using while Loop

1.

Save your file as "ClassNumber_YourName_Ex01.py"

Write a Python program that prints a 10x10 multiplication table using a while loop.

2

3

Use a while loop to iterate through the numbers.

1

(  The variable "i" increases from 0 to 10)

Print the multiplication table.

 1  2  3  4  5  6  7  8  9 
 2  4  6  8 10 12 14 16 18 
 3  6  9 12 15 18 21 24 27 
 4  8 12 16 20 24 28 32 36 
 5 10 15 20 25 30 35 40 45 
 6 12 18 24 30 36 42 48 54 
 7 14 21 28 35 42 49 56 63 
 8 16 24 32 40 48 56 64 72 
 9 18 27 36 45 54 63 72 81 

Use a while loop inside the previous loop to iterate through the numbers.

(  The variable "j" increases from 0 to 10)

Python Nested Loops - Ex01

Multiplication Table using while Loop

1.

Save your file as "ClassNumber_YourName_Ex01.py"

Write a Python program that prints a 10x10 multiplication table using a while loop.

2

3

Use a while loop to iterate through the numbers.

1

(  The variable "i" increases from 0 to 10)

Print the multiplication table.

 1  2  3  4  5  6  7  8  9 
 2  4  6  8 10 12 14 16 18 
 3  6  9 12 15 18 21 24 27 
 4  8 12 16 20 24 28 32 36 
 5 10 15 20 25 30 35 40 45 
 6 12 18 24 30 36 42 48 54 
 7 14 21 28 35 42 49 56 63 
 8 16 24 32 40 48 56 64 72 
 9 18 27 36 45 54 63 72 81 

Use a while loop inside the previous loop to iterate through the numbers.

(  The variable "j" increases from 0 to 10)

Python Nested Loops - Ex02

String Subtraction

1.

Save your file as "ClassNumber_YourName_Ex02.py"

Write a Python program that removes all characters from one string that appear in another string.

Python Output Result

Result: "he"
str1 = "hello"
str2 = "world"

Python Nested Loops - Ex03

Narcissistic number

1.

Save your file as "ClassNumber_YourName_Ex02.py"

A Narcissistic number (also known as an Armstrong number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits in the number. Write a Python program that lists all the Narcissistic numbers from 100 to 999.

Iterate through the numbers from 100 to 999.

1

Check if a number is Narcissistic.

2

Print each Narcissistic number as it is found.

3

Print a descriptive message if no Narcissistic numbers are found in the range.

4

153 → 1³ + 5³ + 3³ = 153 (3-digit example)

370 → 3³ + 7³ + 0³ = 370 (3-digit example)

371 → 3³ + 7³ + 1³ = 371 (3-digit example)

407 → 4³ + 0³ + 7³ = 407 (3-digit example)

1634 → 1⁴ + 6⁴ + 3⁴ + 4⁴ = 1634 (4-digit example)

8208 → 8⁴ + 2⁴ + 0⁴ + 8⁴ = 8208 (4-digit example)

9474 → 9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474 (4-digit example)

Output Example

5

Python Nested Loops - Ex03

Narcissistic number

1.

Save your file as "ClassNumber_YourName_Ex02.py"

A Narcissistic number (also known as an Armstrong number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits in the number. Write a Python program that lists all the Narcissistic numbers from 1 to 1000.

Iterate through the numbers from 100 to 999.

1

Check if a number is Narcissistic.

2

Print each Narcissistic number as it is found.

3

Print a descriptive message if no Narcissistic numbers are found in the range.

4

153 → 1³ + 5³ + 3³ = 153 (3-digit example)

370 → 3³ + 7³ + 0³ = 370 (3-digit example)

371 → 3³ + 7³ + 1³ = 371 (3-digit example)

407 → 4³ + 0³ + 7³ = 407 (3-digit example)

1634 → 1⁴ + 6⁴ + 3⁴ + 4⁴ = 1634 (4-digit example)

8208 → 8⁴ + 2⁴ + 0⁴ + 8⁴ = 8208 (4-digit example)

9474 → 9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474 (4-digit example)

Output Example

5