Brief Overview of Python

This page contains the NCERT Informatics Practices class 11 chapter 3 Brief Overview of Python. You can find the solutions for the chapter 3 of NCERT class 11 Informatics Practices Exercise. So is the case if you are looking for NCERT class 11 Informatics Practices related topic Brief Overview of Python questions and answers for the Exercise
Exercise
1. Which of the following identifer names are invalid and why?
a)
Serial_no.
e)
Total_Marks
b)
1s_Room
f)
total-Marks
c)
Hundred$
g)
_Percentage
d)
TotalMarks
h)
True
The identification of whether the given identifier is valid or not along with the reason is given below.
S.No.
Identifier Name
Validity
Reason
a)
Serial_no.
❌ Invalid
Identifier cannot use special symbols like !, @, # $, %, etc. This identifier has ‘.’ (period/full-stop) in its name. So, it is invalid.
b)
1st_Room
❌ Invalid
An identifer can only start with an alphabet(upper/lower case) or an underscore sign ( _ ). It cannot start with a digit. Note that it can contain a digit as part of the name. But it can not start with a digit.
c)
Serial_no
✔ valid
d)
total Marks
❌ Invalid
Identifier cannot use special symbols like !, @, # $, %, etc. This identifier has ‘ ‘ (space) in its name. So, it is invalid.
e)
Total_Marks
✔ valid
f)
total-Marks
❌ Invalid
Identifier cannot use special symbols like !, @, # $, %, etc. This identifier has ‘-‘ (hiphen/minus) in its name. So, it is invalid. Also note that the identifier can have an underscore which is different from hiphen/minus.
g)
_Percentage
✔ valid
h)
True
❌ Invalid
The variable name can not be a keyword reserved in Python. We know that True is a reserved keyword. So, it cannot be used as a variable name.
2. Write the corresponding Python assignment statements:
a)
Assign 10 to variable length and 20 to variable breadth.
b)
Assign the average of values of variable 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 fullname. Make sure to incorporate blank spaces appropriately between different parts of names.
The following are the corresponding assignment statment(s) for the given descriptions.
a) Python assignment statement to assign 10 to a variable length and 20 to variable breadth:
length = 10
breadth = 20
OR the following statement can also be used:
length, breadth = 10, 20
b) Python statement to assign the average of values of variable length and breadth to a variable sum:
sum = (length + breadth) / 2
c) Python assignment statement to assign a list containing strings ‘Paper’, ‘Gel Pen’ and ‘Eraser’ to a variable stationery:
stationery = ['Paper', 'Gel Pen', 'Eraser']
d) Python assignment statement to assign the strings ‘Mohandas’, ‘Karamchand and ‘Gandhi’ to variables first, middle and last:
first = 'Mohandas'
middle = 'Karamchand'
last = 'Gandhi'
Or the folloing statement can also be used:
first, middle, last = 'Mohandas', 'Karamchand', 'Gandhi'
3. Which data type will be used to represent the following data values and why?
a)
Number of months in a year
b)
Resident of Delhi or not
c)
Mobile number
d)
Pocket money
e)
Volume of a sphere
f)
Perimeter of a square
g)
Name of the student
h)
Address of the student
The data type that will be used for the given descriptions is provided below along with examples.
S.No.
Description
Data Type
Example(s)
a)
Number of months in a year
int
2, 12 etc.
b)
Resident of Delhi or not
bool
True/False
c)
Mobile number
int
1234567890
d)
Pocket money
float
2000.75, 500.25 etc.
e)
Volume of a sphere
float
256.74, 524.725 etc
f)
Perimeter of a square
float
54.92, 94.8324 etc
g)
Name of the student
string
Aman, Sahil etc.
h)
Address of the student
string
Chandni Chowk, Charminar etc.
4. Give the output of the following when num1 = 4, num2 = 3, num3 = 2
a)
num1 += num2 + num3
b)
print(num1)
c)
num1 = num1 ** (num2 + num3)
d)
print(num1)
e)
num1 **= num2 + c
f)
num1 = '5' + '5'
g)
print(num1)
h)
print(4.00/(2.0+2.0))
i)
num1 = 2+9*((3*12)-8)/10
j)
print(num1)
k)
num1 = float(10)
l)
print(num1)
m)
num1 = int('3.14')
n)
print (num1)
o)
print(10 != 9 and 20 >= 20)
p)
print(5 % 10 + 10 < 50 and 29 <= 29)
The output of the given python statements is provided below. Note that when the output is blank, there is some assignment internally. The result of this assigment is given in the explanation.
a) num1 += num2 + num3
num1 += num2 + num3
Output: No output
Explanation: There will not be any output. num1‘s value will now be (4 + 3 + 2) which is equal to 9
b) print(num1)
print(num1)
Output:
9
c) num1 = num1 ** (num2 + num3)
num1 = num1 ** (num2 + num3)
Output: No Output.
Explanation: There will not be any output. The following operations will take place internally.
num1 = 9 ** (3 + 2)
     = 9 ** 5
     = 9 * 9 * 9 * 9 * 9
     = 59049.
d) print(num1)
print(num1)
Output:
59049
e) num1 **= num2 + c
num1 **= num2 + c
Output:
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
Name Error: name 'c' is not defined
Explanation: Error will be displayed as the variable c is not yet defined.
f) num1 = '5' + '5'
num1 = '5' + '5'
Output: No output.
Explanation: As this operation is adding two string variables, the resulting value will be concatenation of '5' and '5'. num1 will now hold the string '55' (num1 is no more an int)
g) print(num1)
print(num1)
Output:
55
h) print(4.00/(2.0+2.0))
print(4.00 / (2.0 + 2.0))
Output:
1.0
Explanation: The given expression will be evaluated as follows:
(4.00/(2.0+2.0))
= (4.00/4.0)
= 1.0
i) num1 = 2+9*((3*12)-8)/10
num1 = 2 + 9 * ((3 * 12) - 8) / 10
Output: No output.
Explanation:num1’s value will be evaluated as follows:
num1 = 2 + 9 * ((3 * 12) - 8) / 10
     = 2 + 9 * (36 - 8) / 10
     = 2 + 9 * (28) / 10
     = 2 + 252/10
     = 2 + 25.2
     = 27.2
j) print(num1)
print(num1)
Output:
27.2
Explanation: num1‘s current value will be printed
k) num1 = float(10)
num1 = float(10)
Output: No Output.
Explanation: num1 will now hold the value 10
l) print(num1)
print(num1)
Output:
10.0
Explanation: Current value of num1 is 10.0 and it will be printed.
m) num1 = int(‘3.14’)
num1 = int('3.14')
Output:
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.14'
Explanation: The additional decimal point in the string literal 3.14 has caused the error. Note that for int function, the valid argument is an int string literal (not float)
n) print (num1)
print (num1)
Output:
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
Name Error: name 'num1' is not defined
o) print(10 != 9 and 20 >= 20)
print(10 != 9 and 2020)
Output:
True
Explanation: Recall that the truth value of 10 != 9 is True and also the truth value of 20 ≥ 20 is also True. So, this expression will be evaluated as follows:
10 != 9 and 20 >= 20
= True and True
= True
p) print(5 % 10 + 10 < 50 and 29 ≤ 29)
print(5 % 10 + 10 < 50 and 2929)
Output:
True
Explanation: This expression will be evaluated as follows:
5 % 10 + 10 < 50 and 29 ≤ 29
= 5 + 10 < 50 and 29 ≤ 29
= 15 < 50 and 29 ≤ 29
= True and True
= True
5. Categorise the following as syntax error, logical error or runtime error:
a)
25 / 0
b)
num1 = 25; num2 = 0; num1 / num2
Note that in both the given cases there is division by zero happening. So, the error occurs during runtime. So, both the given statements cause runtime error.
The following will be the error message.
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
6. Write a Python program to calculate the amount payable if money has been lent on simple interest. Principle or money lent = P, Rate = R% per annum and Time = T years. Then Simple Interest (SI) = (P × R × T)/ 100.
Amount payable = Principal + SI.
P, R and T are given a input to the program.
The following is the program.
# Requesting the user to input the Principal, Rate of Interest and Time in years.
# and capturing the input into the variables P, R and T.
P = float(input('Please enter the Principal Amount: '))
R = float(input('Please enter the Rate of Interest: '))
T = float(input('Please enter the Time in years: '))

# calculating the simple interest
# using the formula SI = (P × R × T)/100
SI = (P * R * T)/100

# Calculating the amount payable as
# Amount Payable = Principal + SI
amountPayable = P + SI

# Output the amount payable.
print('Total Amount Payable: ', amountPayable)
Sample Run:
Please enter the Principal Amount: 20000
Please enter the Rate of Interest: 7
Please enter the Time in years: 5
Total Amount Payable: 27000.0
7. Write a program to repeat the string “GOOD MORNING” n times. Here n is an integer entered by the user.
# Request the user to input the number of times he/she wants to print the string GOOD MORNING
n = int(input("Please enter the number of times you want to repeat the string GOOD MORNING: "))
if n > 0:
    print("GOOD MORNING " * n)
else:
    print("Please input only positive integer")
Output from Sample Run 1:
Please enter the number of times you want to repeat the string GOOD MORNING: 4
GOOD MORNING GOOD MORNING GOOD MORNING GOOD MORNING
Output from Sample Run 2:
Please enter the number of times you want to repeat the string GOOD MORNING: 0
Please input only positive integer
8. Write a program to find the average of 3 numbers.
# Defining 3 variables to hold the 3 numbers
p = 3
q = 4
r = 5
# Finding the average
avg = (p + q + r) / 3
# output the numbers and the average
print('The 3 numbers are p = ', p, ', q = ', q, 'and r = ', r, ' and their average avg = ', avg)
Output:
The 3 numbers are p =  3 , q =  4 and r =  5  and their average avg =  4.0
9. Write a program that asks the user to enter one’s name and age. Print out a message addressed to the user that tells the user the year in which he/she will turn 100 years old.
# Request the user to input the name and age
name = input("Please enter your name: ")
age = int(input("Please enter your age: "))

# Assuming that the current year is 2023, we'll calculate the number of years.
hundred_years_year = 2023 + (100 - age)

# Output the year in which the user will be 100 years old.
print("Hello ", name, "! You will be 100 years old in the year ", hundred_years_year)
Output:
Please enter your name: Sahil
Please enter your age: 16
Hello Sahil! You will be 100 years old in the year 2107
10. What is the difference between else and elif construct of if statement?
The following are the differences between else and elif constructs of if statement.
Basis
elif
else
Condition
Used along with if when it is required to check multiple conditions.
Used when there is only one final alternate path when none of the previous conditions are met.
Position
Can be the last condition when there is no else. But if else is present, it should come before else
When used, it will be ultimate final conditional path to be executed.
Alternate form
elif is short form for elseif
It is already short. No other short form.
Usage
When used, it should always be accompanied by a condition which evaluates to either True or False
Exists without any condition as it is the ultimate path taken when no other previous conditions are met.
Limitation
There is no limit. A conditional statement can have any number of elif
A conditional statement can have only one final else.
Mutual Dependency
When multiple elif are used, each of them should represent a different mutually exclusive condition.
Not applicable as there is only one ultimate else
Construct
Used as either if...elif or if...elif...else
Used as either if...else or if...elif...else
11. Find the output of the following program segments:
a)
for i in range (20, 30, 2):
print(i)
b)
country = 'INDIA'
for i in country:
print (i)
c)
i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print (sum)
The output of the given program segments will be as follows:
a) The pregram segment is:
for i in range (20, 30, 2):
    print(i)
Output:
20
22
24
26
28
b) The program segment is:
country = 'INDIA'
for i in country:
    print (i)
Output:
I
N
D
I
A
c) The program segment is:
i = 0; sum = 0
while i < 9:
    if i % 4 == 0:
        sum = sum + i
    i = i + 2
print (sum)
Output:
12
Note: The while loop iterates incrementing the value of i in each iteration by 2. Thus the if statement is executed when i = 0, i = 4 and i = 8. So, 0, 4 and 8 will be added to sum making it 12.