Python Unit 1: Conditional Statements and Looping (Q&A Format)
1. What is an 'if' statement in Python?
The 'if' statement is used to check a condition. If the condition is true, the block of code under it will
run.
Example:
if x > 0:
print("Positive")
2. What is an 'if-else' statement?
The 'if-else' statement runs one block if the condition is true, and another block if it is false.
Example:
if x > 0:
print("Positive")
else:
print("Not Positive")
3. What is an 'elif' statement?
'elif' means 'else if'. It is used to check multiple conditions one by one.
Example:
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
4. What is a nested if statement?
A nested if is an if statement inside another if block.
Example:
if x >= 0:
if x == 0:
print("Zero")
else:
print("Positive")
5. What is a while loop?
A while loop repeats a block of code as long as the condition is true.
Example:
i=1
while i <= 5:
print(i)
i += 1
6. What is a for loop?
A for loop is used to iterate over a sequence (like a list or range).
Example:
for i in range(5):
print(i)
7. What does 'break' do in a loop?
'break' is used to exit the loop early.
Example:
for i in range(10):
if i == 5:
break
print(i)
8. What does 'continue' do in a loop?
'continue' skips the current iteration and continues with the next one.
Example:
for i in range(5):
if i == 2:
continue
print(i)
9. What is a nested loop?
A nested loop is a loop inside another loop.
Example:
for i in range(2):
for j in range(2):
print(i, j)