What is printed when this code runs?
x = 3
y = x + 2
print(y)
5
Explanation: x is set to 3.
y becomes 3 + 2, which is 5.
The program prints the value of y.
What is printed when this code runs?
for i in range(3):
print(i)
10
Explanation: range(3) generates the numbers 0, 1, 2.
Each number is printed on its own line as the loop runs.
What is printed?
x = 10
if x > 5:
print("Yes")
else:
print("No")
yes
Explanation: x is 10, which is greater than 5, so the if block runs and prints "Yes".
print("Hello world)
what's wrong with this code?
Missing closing quotation mark.
Explanation: The string isn’t closed properly, which causes a SyntaxError.
What is a variable in Python?
A storage location that holds a value.
Explanation: Variables store data that can be used and changed throughout a program.
What is the final output of this code?
total = 0
for i in range(1, 4):
total = total + i
print(total)
6
Explanation: The loop runs with i = 1, 2, 3.
After first loop: total = 1
After second loop: total = 3
After third loop: total = 6
The final value of total is printed.
What is the output of this code?
count = 5
while count > 0:
print(count)
count -= 2
5
3
1
Explanation: The loop runs while count is greater than 0.
First loop: prints 5, then count becomes 3
Second loop: prints 3, then count becomes 1
Third loop: prints 1, then count becomes -1
The loop stops when count is no longer greater than 0.
What is printed?
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")
odd
Explanation: 7 % 2 equals 1, which is not 0, so the else block executes.
x = 10
if x = 5:
print("x is 5")
what's wrong?
Using = instead of == in the if condition.
Explanation: = is for assignment, == is for comparison. Python cannot evaluate x = 5 in an if statement.
What does len() do in Python?
Returns the length of a string, list, or other collection.
Explanation:
For example, len("hello") returns 5 because there are 5 characters.
What is printed when this code runs?
def mystery(n):
if n <= 1:
return n
return n + mystery(n - 1)
print(mystery(4))
10
Explanation: This function uses recursion.
mystery(4) → 4 + mystery(3)
mystery(3) → 3 + mystery(2)
mystery(2) → 2 + mystery(1)
mystery(1) → 1
Adding them together:
4 + 3 + 2 + 1 = 10
What is printed when this code runs?
for i in range(1, 6):
if i % 2 == 0:
print(i)
2
4
Explanation: The loop runs from 1 to 5.
The if statement checks if i is even.
Only values divisible by 2 are printed.
So, only 2 and 4 appear in the output.
What is printed?
a = 5
b = 8
if a > 3 and b < 10:
print("Inside")
elif a > 3 or b > 10:
print("Outside")
else:
print("None")
Inside
Explanation:
a > 3 → True
b < 10 → True
if a > 3 and b < 10 → True, so "Inside" is printed.
The elif and else blocks are skipped.
for i in range(5)
print(i)
what's wrong?
Missing colon : at the end of the for loop
Explanation: Python requires a colon after the for statement to start the loop block. Without it, a SyntaxError occurs.
What is recursion in programming?
A function that calls itself to solve a problem.
Explanation:
Recursion breaks a problem into smaller parts, and each function call works toward a base case to stop the recursion.