Exercise Solutions Available
Step-by-step answers for all textbook questions in this chapter
How to Use This Page
1. Attempt every exercise on your own before reading the solution. 2. Compare your approach with the solution — focus on the logic, not just the final answer. 3. Type and run the Python code on your computer; reading alone is not enough. 4. Review the Common Mistakes section to avoid errors that cost marks in exams.
Key Concepts
| Concept | Syntax | Purpose |
|---|---|---|
| Simple if | if condition: | Execute block only when condition is True |
| if-else | if … else: | Choose between two alternatives |
| elif | if … elif … else: | Choose among multiple alternatives |
| Nested if | if … if … | If inside another if |
| for loop | for var in sequence: | Iterate over a sequence/range |
| while loop | while condition: | Repeat while condition is True |
| break | break | Exit the loop immediately |
| continue | continue | Skip rest of current iteration |
| pass | pass | Null placeholder — do nothing |
| range() | range(start, stop, step) | Generate a sequence of numbers |
| loop-else | for … else: / while … else: | Execute block only if loop was not broken |
Multiple Choice Questions (MCQs)
Q1. Which keyword is used to exit a loop prematurely in Python?
- (a) exit
- (b) break
- (c) return
- (d) stop
Q2. What does range(1, 10, 2) produce?
- (a) 1, 2, 3, 4, 5, 6, 7, 8, 9
- (b) 1, 3, 5, 7, 9
- (c) 2, 4, 6, 8
- (d) 1, 3, 5, 7
Q3. What is the output of the following code?
for i in range(5):
if i == 3:
continue
print(i, end=" ")
- (a) 0 1 2 3 4
- (b) 0 1 2 4
- (c) 0 1 2
- (d) 0 1 2 4 5
Q4. The else clause of a for loop executes when:
- (a) The loop is terminated by break
- (b) The loop completes normally
- (c) The loop condition becomes False
- (d) The loop never runs
Q5. Which of the following is an infinite loop?
- (a)
for i in range(10): - (b)
while True: - (c)
while x == 5:(when x is 5) - (d) Both (b) and (c)
Fill in the Blanks
Q1. The ______ statement is used to skip the remaining statements in the current iteration and move to the next iteration.
Q2. range(5) produces the sequence ______.
Q3. The ______ statement does nothing and acts as a placeholder.
Q4. In a nested if structure, an inner if is placed inside the block of an ______ if.
Q5. The ______ clause of a loop is executed only when the loop is not terminated by a break.
True or False
Q1. The elif keyword can be used without a preceding if.
Q2. A for loop in Python can iterate over a string.
Q3. The break statement terminates only the innermost loop in case of nested loops.
Q4. The pass statement is the same as continue.
Q5. range(1, 5) includes the number 5.
Match the Following
| Column A | Column B |
|---|---|
| 1. break | (a) Null operation placeholder |
| 2. continue | (b) Exits the loop |
| 3. pass | (c) Skips to next iteration |
| 4. range() | (d) Generates number sequence |
| 5. else (with loop) | (e) Runs when loop ends normally |
Short Answer Questions
Q1. Differentiate between break and continue with an example.
break terminates the loop entirely; continue skips only the current iteration.
# break example
for i in range(1, 6):
if i == 3:
break
print(i, end=" ")
# Output: 1 2
# continue example
for i in range(1, 6):
if i == 3:
continue
print(i, end=" ")
# Output: 1 2 4 5
Q2. What is the use of range()? Explain with three forms.
range() generates a sequence of integers. It has three forms:
range(stop) # range(5) → 0,1,2,3,4
range(start, stop) # range(2, 6) → 2,3,4,5
range(start, stop, step) # range(1, 10, 2) → 1,3,5,7,9
Q3. Explain the pass statement with an example.
pass is a null statement that does nothing. It is used where Python syntax requires a statement but no action is needed.
for i in range(5):
pass # placeholder — loop does nothing but is syntactically valid
def empty_function():
pass # TODO: implement later
Q4. What is a nested if? Give an example.
A nested if is an if statement placed inside another if or else block.
marks = 85
if marks >= 60:
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
else:
print("Grade B")
# Output: Grade A
Q5. Write the syntax and meaning of if-elif-else.
if condition1:
# block when condition1 is True
elif condition2:
# block when condition2 is True
else:
# block when no condition is True
Python checks conditions top to bottom. The first True block runs; remaining conditions are skipped.
Detailed Answers
Q1. Explain all control structures in Python with Python programs for each.
Python provides three categories of control structures:
(a) Sequential Structure
Statements execute one after another in order.
name = input("Enter name: ")
print("Hello", name)
(b) Selection Structure (Decision Making)
Simple if:
num = int(input("Enter a number: "))
if num > 0:
print(num, "is positive")
if-else:
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")
if-elif-else:
marks = int(input("Enter marks: "))
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Nested if:
num = int(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive")
else:
print("Negative")
(c) Iteration Structure (Loops)
for loop:
# Print squares of numbers 1 to 5
for i in range(1, 6):
print(i, "squared is", i * i)
while loop:
# Print numbers from 1 to 5 using while
i = 1
while i <= 5:
print(i, end=" ")
i += 1
# Output: 1 2 3 4 5
Q2. Write a Python program to check whether a given number is prime using a for-else construct.
num = int(input("Enter a number: "))
if num < 2:
print(num, "is not a prime number")
else:
for i in range(2, num):
if num % i == 0:
print(num, "is not a prime number")
break
else:
# else block runs only if loop was NOT broken
print(num, "is a prime number")
How it works: The for loop tries to find a divisor. If it finds one, break exits and the else block is skipped. If no divisor is found, the loop finishes normally and the else block confirms the number is prime.
Q3. Write a program to find the factorial of a number using a while loop.
n = int(input("Enter a number: "))
if n < 0:
print("Factorial is not defined for negative numbers")
else:
fact = 1
i = 1
while i <= n:
fact *= i
i += 1
print("Factorial of", n, "is", fact)
Trace for n = 5: fact starts at 1. Loop runs for i = 1,2,3,4,5 → fact = 1×2×3×4×5 = 120.
Q4. Write a program to print the Fibonacci series up to n terms using a for loop.
n = int(input("Enter number of terms: "))
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a + b
# Input: 8 → Output: 0 1 1 2 3 5 8 13
Q5. Write a program to find the sum of digits of a number using a while loop.
num = int(input("Enter a number: "))
n = abs(num) # handle negative input
total = 0
while n > 0:
digit = n % 10
total += digit
n //= 10
print("Sum of digits =", total)
# Input: 1234 → Output: Sum of digits = 10
Q6. Explain break, continue, and pass with examples.
break — exits the loop immediately:
for i in range(1, 11):
if i == 5:
break
print(i, end=" ")
# Output: 1 2 3 4
continue — skips current iteration:
for i in range(1, 11):
if i % 2 == 0:
continue
print(i, end=" ")
# Output: 1 3 5 7 9
pass — does nothing, used as placeholder:
for i in range(5):
pass # syntactically required, no action
print("Done")
# Output: Done
Q7. Write a program to print all Armstrong numbers between 1 and 1000.
print("Armstrong numbers between 1 and 1000:")
for num in range(1, 1001):
temp = num
power = len(str(num))
total = 0
while temp > 0:
digit = temp % 10
total += digit ** power
temp //= 10
if total == num:
print(num, end=" ")
# Output: 1 2 3 4 5 6 7 8 9 153 370 371 407
Q8. Write a program using nested loops to print a multiplication table (1 to 5).
for i in range(1, 6):
for j in range(1, 11):
print(f"{i} x {j} = {i*j}")
print("---")
Common Mistakes
| Mistake | Why It's Wrong | Correction |
|---|---|---|
range(1, 5) expecting 5 | range stops before the stop value | Use range(1, 6) to include 5 |
Missing colon after if, elif, else, for, while | SyntaxError | Always add : at the end |
Forgetting indentation after : | Python uses indentation for blocks | Indent 4 spaces consistently |
Using = instead of == in conditions | = is assignment; == is comparison | Use == for comparisons |
Infinite while loop — forgetting to update the loop variable | Condition never becomes False | Ensure the loop variable changes inside the loop |
Using break when you meant continue | break exits the whole loop | Use continue to skip one iteration only |
Forgetting self in method definitions inside a class | Causes TypeError | Always add self as the first parameter |
Exam Checklist
- [ ] I can write
if,if-else,if-elif-else, and nestedifstructures correctly - [ ] I can use
forloops withrange()in all three forms - [ ] I can write
whileloops and avoid infinite loops - [ ] I understand
break,continue, andpassdifferences - [ ] I can explain and use the
elseclause with loops - [ ] I can trace loop execution and predict outputs
- [ ] I can write programs for factorial, Fibonacci, prime check, and digit sum
- [ ] I always add colons and proper indentation in Python blocks
