Supplementary Exercise On Programming - SOL
Supplementary Exercise On Programming - SOL
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)
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.
Exercise
4 4 4
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
3 5 5.0
4 1 1.0