Woosong University
Department of AI & Big Data
AI Literacy: Introduction to Basic Python
Assignment 001 Total: 5 marks
Student _________Saiyed Al Date:
Name Shahin________________________ ____30/4/2026_______
Student ID ______202412165___________________________ Score: ___ / 5
Instructions: Answer all 10 questions. Write your code and explanations clearly. Each question
is worth 10 points.
Q1. What is the output of the following code? Explain why.
x = 15
if x > 10:
print('Above ten,')
print('and also above 20!')
else:
print('but not above 20.')
Answer:
Output: Above ten
but not above 20
Explanation: in this program there is two block of if condition code in the first block of the if
condition which says if x greater than 10 execute the line above ten. After the that the program
moves to another block statement and here there is two conditions, the first one says if true execute
and also above 20! or if false execute but not above 20. The value assigned to the variable x
the first condition and the third condition is satisfied simultaneously which is why two lines got
executed.
Q2. Rewrite the following nested conditional using chained conditions (elif):
if x == y:
print('x and y are equal')
else:
if x < y:
print('x is less than y')
else:
print('x is greater than y')
Answer: if x == y:
print(‘x and y are equal’)
elif x < y:
print(‘x is less than y’)
elif x > y:
print(‘x is greater than y’)
Q3. What is the purpose of the pass statement in Python? Write an example where it
is useful.
Answer: The purpose of the pass statement is especially not that important in python it is a
statement where its do nothing special to the program and execute the program as it is. Sometimes
it is being executed to avoid indentation error in the program.
Example, for i in range(1,4):
if i == 3:
pass
print(i)
output: 1
2
3
Here in the program, we can see that after giving pass statement to the value 3 it does not do
anything special to the value. Because value 4 is exclusive it does not get executed.
Q4. What will be the output of the code below? Trace through each condition step by
step.
x = 10
y = 15
z = -3
if x < y:
print('x is less than y')
elif x > y or x < z:
print('x is greater than y')
elif x > y or x > z:
print('x is lesser than y')
else:
print('x and y are equal')
Answer: output: x is less than y
In the above program,
Three vaiables x,y,and z is created and value 10,15,-3 is assigned to the variables
accordingly.
In the first statement the program says if x is less than y and this is true so the computer execute
the first line which is x is less than y. in the second line with an elif condtion it checks two condition
with or and says x is greater than y or x is less than z. In the second line neither of the two condition
is true so the program does not execute anything from that line. In the third line with an elif
statement neither of the two condition is true which is x is greater than y or x is greater than z
and so the program doesn’t execute anything from the third line. As for the last line of this program
It says x and y are equal which is definitely not true so the python does execute anything from the
last line.
Q5. Write a Python program using if-elif-else that takes a score (0-100) and prints the
grade: A (90-100), B (80-89), C (70-79), D (60-69), F (below 60).
Answer:
num = int(input(“Enter your Score: “)
if num < 60:
print(“Your grade is F”)
elif num >= 60 and num <=69:
print(“Your grade is D”)
elif num >= 70 and num <= 79:
print(“Your grade is c”)
elif num >= 80 and num <= 89:
print(“Your grade is B”)
elif num >= 90 and num <= 100:
print(“Your grade is A”)
Q6. Evaluate each boolean expression below and write True or False next to each.
Assume x = 10 and y = 20.
a) x > 0 and x < 10
b) x > 0 and y > 10
c) x > 10 or y > 10
d) not (x + y > 15)
Answer:
a: False, in this Boolean expression first condition says x is greater than 0 which is true if we
assumes x = 10 and in the second condition it says x is less than 10 which is false and as for the
and as for the and conditional statement both the condition needs’ to be true to be executed as true
either if one of the condition is false than it will execute false which is in case of the first boolean
expression.
b: True, in case of the second expression both of the condition is true, also and conditional
statement is being is used and we know that if and is used than if the both condition is ture than it
will execute true.
c: True, in the third expression or conditional statement is being is used and from both the condition
one is true that is x is greater than 10 and other one is false. But in case of the or conditional
statement if one of the condition is True than the program will execute True.
d: False, in the last expression not conditional statement is being used and we know if not sits in
front of any boolean expression it reverses the expression and in this this case it is saying value of
x and y combines is not greater than 15 which is false.
Q7. Explain the difference between the = operator and the == operator in Python.
Why is confusing them a common error? Give an example of each.
Answer: = this operator is called assignment operator it assigns a value to a variable and does not
necessarily says something equals something. It checks whether the value that is being assigned is
true to the variable.
X = 20
Means whether both sides are true to each other
== is the equal operator. This operator says something is equals something like x equal 2, y equal
14. x == 2 and y == 14.
Q8. What is the output of the following code? Show your working.
a = 6
b = 10
print(a == 6 and not b == 10)
Answer: False, in the first variable it assigns a equal to 6 and b equals 10
And in the print statement with and conditional statement it says a equal to 6 which is true
by variable assigning but the second condition it says b equal not 10 which is false and we know by
and statement both condition needs to be true to be executed as true
Q9. Explain what try/except does in Python. In the code below, what will be printed if
the user enters the word 'hello' instead of a number? What if they enter 0?
inp = input('Enter a number: ')
try:
num = int(inp)
result = 100 / num
except ValueError:
print('Invalid input! Please enter an integer.')
except ZeroDivisionError:
print('Cannot divide by zero!')
else:
print('Result is:', result)
finally:
print('Execution completed.')
Answer:
The try/except block is used for Exception Handling. It allows your program to "try"
running code that might crash (like dividing by zero or converting text to a number). If an
error occurs, the except block catches it so the program can display a friendly message
instead of crashing.
Scenario 1: User enters 'hello'
Trace: int('hello') triggers a ValueError .
Output:
1. Invalid input! Please enter an integer.
2. Execution completed. (The finally block always runs).
Scenario 2: User enters 0
Trace: int('0') works, but 100 / 0 triggers a ZeroDivisionError .
Output:
1. Cannot divide by zero!
Q10. Write a Python program that asks the user to enter a Fahrenheit temperature.
Use try/except to handle the case where the user does not enter a valid number. If
the input is valid, convert it to Celsius using the formula: C = (F - 32) x 5/9 and print
the result.
Answer:
temp_input = input("Enter a Fahrenheit temperature: ")
try:
fahrenheit = float(temp_input)
celsius = (fahrenheit - 32) * 5/9
print(f"{fahrenheit} degrees Fahrenheit is {celsius:.2f} degrees Celsius.")
except ValueError:
print("Error: That is not a valid number. Please enter a numeric temperature.")
finally:
print("Thank you for using the temperature converter.")