F.
Short Answer Type Questions
1. Explain the use of break statement in Python with the help of an example.
The break statement is used to exit a loop immediately when a certain condition is met.
for i in range(1, 6):
if i == 3:
break
print(i)
# Output:
# 1
# 2
2. Write a program to find the smallest number among three numbers by using the nested 'if-else'
statements.
a = 11
b = 5
c = 7
if a < b:
if a < c:
smallest = a
else:
smallest = c
else:
if b < c:
smallest = b
else:
smallest = c
print("Smallest number is", smallest)
3. Write a Python program to check if a given number is even or odd.
n = 8
if n % 2 == 0:
print("Even")
else:
print("Odd")
4. Write a Python program to add all the elements of a tuple.
t = (3, 8, 4)
total = 0
i = 0
while i < len(t):
total = total + t[i]
i = i + 1
print("Sum is", total)
G. Long Answer Type Questions
1. Write a program to delete/remove all the numbers less than 10 from the list.
lst = [15, 5, 12, 3, 21]
new_lst = [0] * len(lst) # Create list of same size
j = 0 # Index for new list
i = 0
while i < len(lst):
if lst[i] >= 10:
new_lst[j] = lst[i]
j = j + 1
i = i + 1
# Copy values back to original list
i = 0
while i < j:
lst[i] = new_lst[i]
i = i + 1
# Print only valid elements
i = 0
while i < j:
print(lst[i], end=" ")
i = i + 1
# Output: 15 12 21
2. Write a Python program to find the largest element in a tuple.
t = (11, 23, 7, 18)
largest = t[0]
i = 1
while i < len(t):
if t[i] > largest:
largest = t[i]
i = i + 1
print("Largest element is", largest)
3. Write a Python program to find the square of all elements between 1 to 20.
i = 1
while i <= 20:
print(i, "squared is", i * i)
i = i + 1
4. Write a Python program to demonstrate the use of the if-elif-else statements.
marks = 55
if marks >= 80:
print("Excellent")
elif marks >= 50:
print("Good")
else:
print("Needs Improvement")