Python Assignment Solutions
Q1 (a) Purpose of elif Statement
The elif statement in Python is used to check multiple conditions. It means 'else if'. It allows us to
check more than two conditions in a program. The difference between if-else and if-elif-else is that
if-else handles only two cases, while if-elif-else handles multiple conditions. It is important because
it makes the program more readable and efficient.
Q1 (b) Structure of for Loop and for-else Loop
The for loop is used to iterate over a sequence such as a list, tuple, or range. The basic structure is:
for variable in sequence: statement. The for-else loop executes the else part after the loop is
completed normally without break. We can iterate over a collection like a list by directly using its
elements in the loop.
Q3 (a) Pattern Printing Program
Pattern: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Python Code: for i in range(1, 6): for j in range(i): print(i, end=' ') print()
Q3 (b) Pyramid Pattern Program
Pattern: 1 121 12321 1234321 123454321
Python Code: n = 5 for i in range(1, n + 1): for j in range(1, i + 1): print(j, end='') for j in range(i - 1, 0,
-1): print(j, end='') print()
Q4 (a) Program to Print First N Prime Numbers
Python Code: n = int(input('Enter value of N: ')) count = 0 num = 2 while count < n: prime = True for i
in range(2, num): if num % i == 0: prime = False break if prime: print(num, end=' ') count += 1 num
+= 1
Q4 (b) Program to Count Vowels in a String
Python Code: text = input('Enter a string: ') vowels = 'aeiouAEIOU' count = 0 for ch in text: if ch in
vowels: count += 1 print('Number of vowels:', count)
Q5 (a) Continue Statement
The continue statement is used to skip the current iteration of a loop and move to the next iteration.
It is mainly used when we want to skip a specific condition without stopping the entire loop.
Q5 (b) Pass Statement
The pass statement is used as a placeholder when no action is required. It is used to avoid errors
when we need an empty loop, function, or condition block.