Chapter 5 Getting Started With Python
Chapter 5 Getting Started With Python
Question 1:
v) Total_Marks: Valid
vii) _Percentage: Valid
Question 2:
e) Assign the concatenated value of string variables first, middle and last to
variable fullname. Make sure to incorporate blank spaces appropriately
between different parts of names.
ANSWER:
d) first,middle,last = "Mohandas","Karamchand","Gandhi"
d) The string ‘middle’ is larger than the string ‘first’ and smaller than the string
‘last’
e) len(Stationery) == 0
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
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
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'}
Question 7:
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.
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
Now, the complete equation can be written as
print(True and True)
Therefore, the output will be 'True'.