0% found this document useful (0 votes)
20 views5 pages

Supplementary Exercise On Programming - SOL

The document outlines a supplementary exercise for S.3 Information & Communication Technology students at The Chinese Foundation Secondary School, focusing on programming algorithms and Python. It includes examples of data types, basic Python code, conditional statements, loops, and trace tables to illustrate program execution. Additionally, it provides exercises to reinforce learning through practical coding tasks.

Uploaded by

nickyip100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Supplementary Exercise On Programming - SOL

The document outlines a supplementary exercise for S.3 Information & Communication Technology students at The Chinese Foundation Secondary School, focusing on programming algorithms and Python. It includes examples of data types, basic Python code, conditional statements, loops, and trace tables to illustrate program execution. Additionally, it provides exercises to reinforce learning through practical coding tasks.

Uploaded by

nickyip100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

The Chinese Foundation Secondary School

S.3 Information & Communication Technology (2024 -2025)


Programming Algorithm and Python– Supplementary Exercise

Data Type
Data Type Sample
String (str) "CFSS"
Integer (int) 2
Float (float) 2.9
Boolean (bool) True
String (str) "2904"
Integer (int) 7322
Boolean (bool) False
Float (float) 5.0

Basic python
Write down the code / output in the following table.
Code Output
print("CFSS") CFSS
print(29047322) 29047322
x = 5678 5678
print(x)
x = 5678 x
print("x")
print(2+9) 11
print("2"+ "9") 29
print("2 + 9") 2 +9
x = 2.7181828 2
x = int(x)
print(x)
x = 2.7181828 2.7181828
x = float(x)
print(x)
x = int(input("Enter: ") ) [user type:5] 5
print(x)
x = input("Enter: ") [user type:5] Name Error
print(y)

Condition
Write down the code / output in the following table.
Code Output
x = 10 x is greater than 5
if x > 5:
print("x is greater than 5")
num = 15 Odd
if num % 2 == 0:
print("Even")
else:
print("Odd")
age = 18 Teenager
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
else:
print("Adult")
Loop (for)
Write down the code / output in the following table.
Code Output
for i in range(3, 4): 3
print(i)

for i in range(-2): (Nothing will print)


print(i)

x = int(input(“Enter a number: ”)) Assume that x is 3, what is the output?


for i in range (0,9,x):
print(i) 036

for i in range (2): (Not covered in exam)


for j in range (3): 0 0
print(i,j) 0 1
0 2
1 0
1 1
1 2

Loop (while)
Write down the code / output in the following table.
Code Output
i = 3 3
while i < 4:
print(i)
i = i + 1
i = -2 -2
while i < 0: -1
print(i)
i = i + 1
x = int(input("Enter a number: ")) Assume that x is 3, what is the output?
i = 0 0
while i < 9: 3
print(i) 6
i = i + x

while i < 2:
j = 0
while j < 3:
NameError: name 'i' is not defined
print(i, j)
j = j + 1
i = i + 1
Trace Table
⚫ It is a tool used to track variable changes step by step during program execution.

It can help us to:


⚫ Debug errors
⚫ Understand loops & conditions
⚫ Predict program output
⚫ Develop logical thinking

Example 1 – simple for loop

Python Code Trace table Final Output


for i in range(1, 4): Iteration i Output 1
print(i) 1st 1 1 2
2nd 2 2 3
3rd 3 3

Example 2 – simple while loop

Python Code Trace table Final Output


i = 1 Iteration i Output 1
while i <= 3: 0 1 2
print(i) 1st 2 1 3
i = i + 1 2nd 3 2
3rd 4 3

Exercise

Python Code Trace table Final Output


1. for i in range(1, 5): Iteration i Output 1
print(i)
1 1 1 2
2 2 2 3
3 3 3 4

4 4 4

2. Python Code Trace table Final Output


i = 1 Iteration Output i 1
while i < 5:
(initial value) 1 2
print(i)
i = i + 1 1 1 2 3
2 2 3 4
3 3 4
4 4 5
3. Python Code Trace table Final Output
x = 81 Iteration x i Output 27.0
for i in range(4):
(initial value) 81 9.0
x = x / 3
print(x) 1 27 0 27.0 3.0
2 9 1 9.0 1.0
3 3 2 3.0
4 1 3 1.0

Example 4 – while Loop with Odd Numbers


Python Code Trace table Final Output
i = 1 Iteration i Output 1
while i < 10: (initial value) 1 3
print(i) 1 3 1 5
i = i + 2 2 5 3 7
3 7 5 9
4 9 7
5 11 9

Example 5 – for Loop with if Condition


Python Code Trace table Final Output
for i in range(1, 6): Iteration i i % 2 == 0 Output 2 is even
if i % 2 == 0: 1 1 False 4 is even
print(i, "is even") 2 2 True 2 is even
3 3 False
4 4 True 4 is even
5 5 False

Example 6 – while Loop with Multiplication


Python Code Trace table Final Output
x = 1 Iteration x Output 2
while x < 20: (initial value) 1 4
x = x * 2 1 2 2 8
print(x) 2 4 4 16
3 8 8 32
4 16 16
5 32 32

Exercise
Python Code Trace table Final Output
4. i = 0 Iteration i Output 2
while i < 10:
(initial value) 0 4
i += 2  i = i + 2
print(i) 1 2 2 6

2 4 4 8

3 6 6 10

4 8 8
5 10 10
5. Python Code Trace table Final Output
for i in range(1, 6): Iteration i i % 2 == 1 Output 1 is odd
if i % 2 == 1:
1 1 True 1 is odd 3 is odd
print(i, "is odd")
2 2 False 5 is odd
3 3 True 3 is odd
4 4 False
5 5 True 5 is odd

6. Python Code Trace table Final Output


x = 625 Iteration x Output 125.0
while x > 1:
(initial value) 625 25.0
x = x / 5
print(x) 1 125 125.0 5.0
2 25 25.0 1.0

3 5 5.0
4 1 1.0

Example 7– while Loop with if-else Condition

Python Code Trace table


x = 10 Iteration x x % 3 == 0 Output
while x > 0: 0 10
if x % 3 == 0: 1 8 False 10 is not
print(x, "is divisible by 3") divisible by 3
else: 2 6 False 8 is not
print(x, "is not divisible by 3") divisible by 3
x -= 2 3 4 True 6 is divisible
by 3
4 2 False 4 is not
divisible by 3
5 0 False 2 is not
divisible by 3

Example 8– Nested for Loop


(More to learn)
Python Code Trace table
for i in range(1, 4): Iteration (i) i j Output
for j in range(1, 4): 1st 1 1 1*1=1
print(i, "*", j, "=", i * j) 1 2 1*2=2
1 3 1*3=3
2nd 2 1 2*1=2
2 2 2*2=4
2 3 2*3=6
3rd 3 1 3*1=3
3 2 3*2=6
3 3 3*3=9

You might also like