1. What is the difference between else and elif construct of if statement?
Answer: – else statement does not allow to check the condition, while elif allow to check the condition. elif is use to handle the multiple situations.
2. What is the purpose of range() function? Give one example.
Answer: – The purpose of range() function is to generate a sequence (list) of numbers, started from start and ended at end-1, with the gap of step. i.e. to generate a list.
It becomes useful when we want to execute loop n-times, generate a list of integers.
Syntax :
(a) range(end),
(b) range(start, end),
(c) start(start, end, step)
For example:
>>> range(5) # generate a list like [0,1,2,3,4]
>>> range(2, 6) # generate a list like [ 2, 3, 4, 5]
>>> range(1, 10, 3) #generate a list like [1,4,7]
3. Differentiate between break and continue statements using examples.
Answer: – break and continue both comes under the category of jump statement. In Python, it is used inside the loop statement.
break:
break jumps the control outside the loop. It is use to terminate the execution of loop on the basis of certain condition. You can also use the break without condition (not preferred).
The break statement immediately exits a loop, skipping the rest of the loop’s body. Execution continues with the statement immediately following the body of the loop.
Syntax: break
Example :
for n in range(1, 20):
if n == 5:
break
print(n, end = ' - ')
Output: 1 – 2 – 3 – 4 –
continue:
continue jumps the control to the beginning of the loop for the next iteration. It is use to skip the some parts of body of loop and start the next iteration of loop on the basis of certain condition. You can also use the continue without condition (not preferred).
When a continue statement is encountered, the control jumps to the beginning of the loop for the next iteration.
Syntax: continue
Example :
for n in range(1, 10):
if n % 2 == 1:
continue
print(n, end = ' ')
Output: 2 4 6 8
4. What is an infinite loop? Give one example.
Answer: – A loop, which never terminates called infinite loop. For Example:
# Example 1:
n = 1
while True:
print(n, end = ' ')
# Example 2:
n = 1
while n <= 10:
print(n, end = ' ')
5. Find the output of the following program segments:
(i)
a = 110
while a > 100:
print(a)
a -= 2
Answer: – Output:
110
108
106
104
102
(ii)
for i in range(20,30,2):
print(i)
Answer: – Output:
20
22
24
26
28
(iii)
country = 'INDIA'
for i in country:
print (i)
Answer: – Output:
I
N
D
I
A
(iv)
i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print (sum)
Answer: – Output: 12
(v)
for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print (x * y)
Answer: – Output:
2
3
4
4
6
8
6
9
(vi)
var = 7
while var > 0:
print ('Current variable value: ', var)
var = var - 1
if var == 3:
break
else:
if var == 6:
var = var - 1
continue
print ("Good bye!")
Answer: – Output:
Current variable value : 7
Current variable value : 5
Good bye!
Current variable value : 4
Programming Exercises
1. Write a program that takes the name and age of the user as input and displays a message whether the user is eligible to apply for a driving license or not. (the eligible age is 18 years).
Answer: – Eligible for a Driving License or not.
name = input("Enter your name : ")
age = int(input("Enter your age : "))
if age >= 18:
print(name, "You are eligible for driving license")
else:
print(name, "You are not eligible for driving license")
2. Write a function to print the table of a given number. The number has to be entered by the user.
Answer: – Function to print Table of given number:
def printtable(num):
for n in range(num, num*10+1, num):
print(n, end = ' ')
print()
#main block
Number = int(input("Enter a number :"))
print("Table of", Number)
printtable(Number)
3. Write a program that prints minimum and maximum of five numbers entered by the user.
Answer: – Minimum and Maximum of five numbers
min = 0
max = 0
for count in range(5):
n = int(print("Enter Number ",count+1))
if n < min:
min = n
if n > max:
max = n
print("Maximum : ",max)
print("Minimum : ",min)
4. Write a program to check if the year entered by the user is a leap year or not.
Answer: – Python program to check leap year or not a leap year.
Method – 1
year = int(input("Enter a year :"))
if year % 4 == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Method – 2
year = int(input("Enter a year :"))
if year % 4 == 0 and year % 100 != 0:
print(year, "is a leap year")
elif year % 100 == 0 and year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
5. Write a program to generate the sequence: –5, 10, –15, 20, –25….. upto n, where n is an integer input by the user.
Answer: – Program to generate Sequence in Python
# Generate a Sequence in Python
n = int(input("Enter a Number : "))
flag = 1
for t in range(5, n+1,5):
print(t * flag, end=' ')
flag = flag * - 1
Output:
6. Write a program to find the sum of 1+ 1/8 + 1/27……1/n3, where n is the number input by the user.
Answer: – Python program to find sum of given sequence:
# Find the sum of 1 + 1/8 + 1/27……1/n^3
n = int(input("Enter a Number : "))
sum = 0
for t in range(1, n+1):
sum = sum + 1/t**3
print("Sum : ", sum)
Output:
7. Write a program to find the sum of digits of an integer number, input by the user.
Answer: – Find the sum of digits of an integer number, input by the user.
Method – I ( Pythonist Way )
# Method - I (Pythonist way)
number = input("Enter a Number : ")
sum = 0
for digit in number:
sum = sum + int(digit)
print("Sum of Digits of", number, "is", sum)
Method – II (General Way)
# Method - II(General way)
number = int(input("Enter a Number : "))
sum = 0
rem = 0
temp = number
while number > 0:
rem = number % 10
sum = sum + rem
number = number // 10
print("Sum of Digits of", temp, "is", sum)
Output:
8. Write a function that checks whether an input number is a palindrome or not.
[Note: A number or a string is called palindrome if it appears same when written in reverse order also. For example, 12321 is a palindrome while 123421 is not a palindrome]
Answer: – Checks whether an input number is palindrome or not.
#Method - 1 (General)
number = int(input("Enter a Number : "))
rev = 0
rem = 0
temp = number
while number > 0:
rem = number % 10
rev = rev * 10 + rem
number = number // 10
if temp == rev:
print(temp, "is a palindrome")
else:
print(temp, "is not a palindrome")
#Method - II ( Its Python Way)
number = input("Enter a Number : ")
reverse = number[::-1]
if number == reverse:
print(number, "is a palindrome")
else:
print(number, "is not a palindrome")
Output:
9. Write a program to print the following patterns:
Answer: –
10. Write a program to find the grade of a student when grades are allocated as given in the table below.
Percentage of Marks | Grade |
Above 90% | A |
80% to 90% | B |
70% to 80% | C |
60% to 70% | D |
Below 60% | E |
Percentage of the marks obtained by the student is input to the program.
Answer: – A python program to find grade of students
per = float(input("Enter Percentage Marks : "))
if per > 90 :
Grade = 'A'
elif per > 80 and per <= 90:
Grade = 'B'
elif per > 70 and per <= 80:
Grade = 'C'
elif per >= 60 and per <= 70:
Grade = 'D'
elif per < 60 :
Grade = 'E'
print("Your Grade is ", Grade)
Output:
Case Study-based Questions
Let us add more functionality to our SMIS developed in Chapter 5.
6.1 Write a menu driven program that has options to
• accept the marks of the student in five major subjects in Class X and display the same.
• calculate the sum of the marks of all subjects. Divide the total marks by number of subjects (i.e. 5), calculate percentage = total marks/5 and display the percentage.
• Find the grade of the student as per the following criteria:
Criteria | Grade |
percentage > 85 | A |
percentage < 85 && percentage >= 75 | B |
percentage < 75 && percentage >= 50 | C |
percentage > 30 && percentage <= 50 | D |
percentage <30 | Reappear |
Let’s peer review the case studies of others based on the parameters given under “DOCUMENTATION TIPS” at the end of Chapter 5 and provide a feedback to them.
Answer: – Marks and Result Calculator
Discover more from EduGrown School
Subscribe to get the latest posts sent to your email.