Answer Key Chapter 5
Answer Key Chapter 5
i Serial_no. v Total_Marks
ii 1st_Room vi total-Marks
iii Hundred$ vii _Percentage
iv Total Marks viii True
ANSWER:
i) Serial_no.: Invalid - Identifier in python cannot contain any special character except
underscore(_).
iii) Hundred$: Invalid - Identifier in Python cannot contain any special character except
underscore(_).
iv) Total Marks: Invalid - Identifier in Python cannot contain any special character
except underscore(_). If more than one word is used as a variable then it can be
separated using underscore ( _ ), instead of space.
v) Total_Marks: Valid
vi) total-Marks: Invalid - Identifier in Python cannot contain any special character except
underscore(_). If more than one word is used as a variable then it can be separated
using underscore ( _ ), instead of a hyphen ( - ).
Page No 115:
Question 2:
b) Assign the average of values of variables length and breadth to a variable sum
c) Assign a list containing strings ‘Paper’, ‘Gel Pen’, and ‘Eraser’ to a variable stationery.
d) Assign the strings ‘Mohandas’, ‘Karamchand’, and ‘Gandhi’ to variables first, middle
and last.
e) Assign the concatenated value of string variables first, middle and last to variable full
name. Make sure to incorporate blank spaces appropriately between different parts of
names.
ANSWER:
d) first,middle,last = "Mohandas","Karamchand","Gandhi"
Page No 115:
Question 3:
d) The string ‘middle’ is larger than the string ‘first’ and smaller than the string ‘last’
e) len(Stationery) == 0
Page No 116:
Question 4:
a) 0 == 1 == 2
b) 2 + 3 == 4 + 5 == 7
c) 1 < -1 == 3 > 4
ANSWER:
a) ( 0 == (1==2))
b) (2 + (3 == 4) + 5) == 7
Page No 116:
Question 5:
a) num1 = 4
num2 = num1 + 1
num1 = 2
print (num1, num2)
b) num1, num2 = 2, 6
num1, num2 = num2, num1 + 2
print (num1, num2)
c) num1, num2 = 2, 3
num3, num2 = num1, num3 + 1
print (num1, num2, num3)
ANSWER:
a) 2, 5
b) 6, 4
c) Error as num3 is used in RHS of line 2 (num3, num2 = num1, num3 + 1) before
defining it earlier.
Page No 116:
Question 6:
Which data type will be used to represent the following data values and why?
c) Mobile number
d) Pocket money
e) Volume of a sphere
f) Perimeter of a square
a) The int data type will be used to represent 'Number of months in a year' as it will be
an integer i.e. 12.
b) The boolean data type will be used to represent 'Resident of Delhi or not' as a person
will be either a resident of Delhi or not a resident of Delhi. Therefore, the values True or
False will be sufficient to represent the values.
c) The integer data type will be used to represent 'Mobile number' as it will be a ten-digit
integer only.
d) The float data type will be used to represent 'Pocket money' as it can be in decimal.
e.g Rs. 250.50 i.e 250 rupees and 50 paise.
Note:- If the side length is a whole number, the perimeter will always be an integer,
however, we should be open to the possibility that the side length can be in decimal as
well.
g) The string data type will be used to represent 'Name of the student'.
h)The string data type will be used to represent 'Address of the student'. However, if we
have to store the address in a more structured format, we can use dictionary data type
as well. e.g. Address = { 'Line1': ‘Address line 1', 'Line2':'Address
Line2', 'Locality':'Locality Name', 'City':'City Name',
'Pincode':110001, 'Country':'India'}
Page No 116:
Question 7:
e) print(4.00/(2.0+2.0))
f) num1 = 2+9*((3*12)-8)/10
print(num1)
g) num1 = 24 // 4 // 2
print(num1)
h) num1 = float(10)
print (num1)
i) num1 = int('3.14')
print (num1)
j) print('Bye' == 'BYE')
a) num1 += 3 + 2
The above statement can be written as
num1 = num1 + 3 + 2 = 4 + 3 + 2 = 9
Therefore, print(num1) will give the output 9.
e) print(4.00/(2.0 + 2.0))
The numbers written in the statement are in float data type therefore, the output will be
also in float data type.
print(4.00/(2.0 + 2.0))
print(4.0/4.0)
1.0
Therefore, the output will be 1.0.
f) num1 = 2 + 9 * ((3 * 12) - 8) /10
# The expression within inner brackets will be evaluated first
num1 = 2 + 9 * (36 - 8) /10
# The expression within outer brackets will be evaluated next
num1 = 2 + 9 * 28/10
# * and / are of same precedence, hence, left to right order is followed
num1 = 2 + 252/10
num1 = 2 + 25.2
num1 = 27.2
Therefore, the output will be 27.2.
g) num1 = 24 // 4 // 2
#When the operators are same, left to right order will be followed for operation
num1 = 6 // 2
#When floor division is used, return value will be int data type
num1 = 3
Therefore, the output will be 3
h) num1 = float(10)
float(10) will convert integer value to float value and therefore, the output will be
10.0.
i) num1 = int('3.14')
This will result in an error as we cannot pass string representation of float to an int
function.
j) print('Bye' == 'BYE')
As Python compares string character to character and when different characters are
found then their Unicode value is compared. The character with lower Unicode value is
considered to be smaller. Here, 'y' has Unicode 121 and 'Y' has 89. Therefore, the
output will be 'False'.
LHS:
10 + 6 * 2 ** 2 != 9//4 - 3
10 + 6 * 4 != 2 - 3
10 + 24 != -1
34 != -1
True
RHS:
29 >= 29/9
True
LHS :
5 % 10 + 10 < 50
5 + 10 < 50
15 < 50
True
RHS:
29 <= 29
True
Page No 117:
Question 8:
a) 25 / 0
b) num1 = 25; num2 = 0; num1 / num2
ANSWER:
a) Runtime Error. The syntax for the division is correct. The error will arise only when
'interpreter' will run this line.
b) Runtime Error. The syntax is correct. The error will arise only when 'interpreter' will
run the line containing these statements.
Page No 117:
Question 9:
A dartboard of radius 10 units and the wall it is hanging on are represented using a two-
dimensional coordinate system, with the board’s centre at coordinate (0,0). Variables x
and y store the x-coordinate and the y-coordinate of a dart that hits the dartboard. Write
a Python expression using variables x and y that evaluates to True if the dart hits (is
within) the dartboard, and then evaluate the expression for these dart coordinates:
a) (0,0)
b) (10,10)
c) (6, 6)
d) (7,8)
ANSWER:
The distance formula can be used to calculate the distance between the points where
the dart hits the dartboard and the centre of the dartboard.
The distance of a point P(x, y) from the origin is given by �2+�2.
To calculate the square root, the equation can be raised to the power 0.5.
Program:
x = int(input('Enter X Coordinate: '))
y = int(input('Enter Y Coordinate: '))
dis = (x ** 2 + y ** 2) ** 0.5
#if dis is greater than 10, means that dart is more than 10 units
away from the centre.
print(dis <= 10)
Question 10:
Program:
#defining the boiling and freezing temp in celcius
boil = 100
freeze = 0
print('Water Boiling temperature in Fahrenheit::')
#Calculating Boiling temperature in Fahrenheit
tb = boil * (9/5) + 32
#Printing the temperature
print(tb)
OUTPUT:
Water Boiling temperature in Fahrenheit::
212.0
Water Freezing temperature in Fahrenheit::
32.0
Page No 117:
Question 11:
Write a Python program to calculate the amount payable if money has been lent on
simple interest.
Principal or money lent = P, Rate of interest = R% per annum and Time = T years. Then
Simple Interest (SI) = (P x R x T)/ 100.
Amount payable = Principal + SI. P, R and T are given as input to the program.
ANSWER:
Program:
OUTPUT:-
Enter the principal: 12500
Enter the rate of interest per annum: 4.5
Enter the time in years: 4
Total amount: 14750.0
Page No 118:
Question 12:
Write a program to calculate in how many days a work will be completed by three
persons A, B and C together. A, B, C take x days, y days and z days respectively to do
the job alone. The formula to calculate the number of days if they work together is
xyz/(xy + yz + xz) days where x, y, and z are given as input to the program.
ANSWER:
Program:
OUTPUT:-
Enter the number of days required by A to complete work alone: 8
Enter the number of days required by B to complete work alone: 10
Enter the number of days required by C to complete work alone: 20
Total time taken to complete work if A, B and C work together:
3.64
Page No 118:
Question 13:
Write a program to enter two integers and perform all arithmetic operations on them.
ANSWER:
Program:
#Program to input two numbers and performing all arithmetic
operations
OUTPUT:
Enter first number: 8
Enter second number: 3
Results:-
Addition: 11
Subtraction: 5
Multiplication: 24
Division: 2.6666666666666665
Modulus: 2
Floor Division: 2
Exponentiation: 512
Page No 118:
Question 14:
Program:
OUTPUT:
The values of x and y are 5 and 6 respectively.
The values of x and y after swapping are 6 and 5 respectively.
Page No 118:
Question 15:
Program:
#defining two variables
x = 5
y = 6
OUTPUT:
The values of x and y are 5 and 6 respectively.
The values of x and y after swapping are 6 and 5 respectively.
Page No 118:
Question 16:
Write a program to repeat the string ''GOOD MORNING'' n times. Here 'n' is an integer
entered by the user.
ANSWER:
The string will be repeated only if the value is greater than zero, otherwise, it will return a
blank. Therefore, ‘if’ condition will be used to check the values of 'n' first and then the
print statement will be executed.
Program:
str = "GOOD MORNING "
n = int(input("Enter the value of n: "))
if n>0:
print(str * n)
else:
print("Invalid value for n, enter only positive values")
OUTPUT:
Enter the value of n: 5
GOOD MORNING GOOD MORNING GOOD MORNING GOOD MORNING GOOD MORNING
Page No 118:
Question 17:
OUTPUT:
The average of 5 6 and 7 is 6.0
Page No 118:
Question 18:
The volume of a sphere with radius r is 4/3πr3. Write a Python program to find the
volume of spheres with radius 7 cm, 12 cm, 16 cm, respectively.
ANSWER:
Program:
#defining three different radius using variables r1, r2, r3
r1 = 7
r2 = 12
r3 = 16
#calculating the volume using the formula
volume1 = (4/3*22/7*r1**3)
volume2 = (4/3*22/7*r2**3)
volume3 = (4/3*22/7*r3**3)
OUTPUT:
When the radius is 7 cm, the volume of the sphere will be 1437.33 cc
When the radius is 12 cm, the volume of the sphere will be 7241.14 cc
When the radius is 16 cm, the volume of the sphere will be 17164.19 cc
Page No 118:
Question 19:
Write a program that asks the user to enter their name and age. Print a message
addressed to the user that tells the user the year in which they will turn 100 years old.
ANSWER:
Program:
#Program to tell the user when they will turn 100 Years old
name = input("Enter your name: ")
#calculating the 100th year for the user considering 2020 as the
current year
hundred = 2020 + (100 - age)
OUTPUT:
Enter your name: John
Enter your age: 15
Hi John ! You will turn 100 years old in the year 2105
Page No 118:
Question 20:
The formula E = mc2 states that the equivalent energy (E) can be calculated as the mass
(m) multiplied by the speed of light (c = about 3×108 m/s) squared. Write a program that
accepts the mass of an object and determines its energy.
ANSWER:
OUTPUT:
Enter the mass of object(in grams): 25
The energy of an object with mass 25.0 grams is
2250000000000000.0 Joule.
Page No 118:
Question 21:
Presume that a ladder is put upright against a wall. Let variables length and angle store
the length of the ladder and the angle that it forms with the ground as it leans against the
wall. Write a Python program to compute the height reached by the ladder on the wall for
the following values of length and angle:
a) 16 feet and 75 degrees
b) 20 feet and 0 degrees
c) 24 feet and 45 degrees
d) 24 feet and 80 degrees
ANSWER:
To calculate the sin() value in Python, math module sin() function is needed. The values
need to be passed in radians to the sin() function. Therefore, the degree will be
converted to radians and then the sin() function will be applied.
Program:
OUTPUT:
a) Enter the length of the ladder: 16
Enter the alignment degree: 75
The height reached by the ladder with length 16 feet aligned at
75 degrees is 15.45 feet.