CBSE IP Chapter 3
Brief Overview of Python
Online Classes available for different programming languages & for class IX (IT 402 Code), XII (IP 065)
Brief Overview of Python
Python is an open-source, high level, interpreter based language that can be used for a multitude of scientific and non-scientific computing purposes.
Comments are non-executable statements in a program.
An identifier is a user defined name given to a variable or a constant in a program.
Process of identifying and removing errors from a computer program is called debugging.
Trying to use a variable that has not been assigned a value gives an error.
There are several data types in Python — integer, boolean, float, complex, string, list, tuple, sets, None and dictionary.
Operators are constructs that manipulate the value of operands. Operators may be unary or binary.
An expression is a combination of values, variables, and operators.
Python has input() function for taking user input.
Python has print() function to output data to a standard output device.
The if statement is used for decision making.
Looping allows sections of code to be executed repeatedly under some condition.
for statement can be used to iterate over a range of values or a sequence.
The statements within the body of for loop are executed till the range of values is exhausted.
Q1 Which of the following identifier names are invalid and why?
a) Serial_no. - Valid Identifier Name
b) 1st_Room - InValid Identifier Name, Identifier Name cannot start with Number
c) Hundred$ - InValid Identifier Name, Identifier Name contain special character $
e) Total_Marks - Valid Identifier Name
f) total-Marks - Valid Identifier Name
g) _Percentage - Valid Identifier Name
d) Total Marks - InValid Identifier Name, Identifier Name cannot have spaces in between 2 word
h) True - InValid Identifier Name, Identifier Name cannot be a keyword.
Q2. Write the corresponding Python assignment statements:
a) Assign 10 to variable length and 20 to variable breadth.
length=10
breadth=20
b) Assign the average of values of variables length and breadth to a variable sum.
sum = (length + breadth) / 2
c) Assign a list containing strings ‘Paper’, ‘Gel Pen’, and ‘Eraser’ to a variable stationery.
stationery = [‘Paper’, ‘Gel Pen’, ‘Eraser’]
print(stationery)
d) Assign the strings ‘Mohandas’, ‘Karamchand’, and ‘Gandhi’ to variables first, middle and last.
first = ‘Mohandas’
middle = ‘Karamchand’
last = ‘Gandhi’
print(first)
print(middle)
print(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.
fullname = first +” “+middle+” “+last
print(fullname)
Q3. Which data type will be used to represent the following data values and why?
a) Number of months in a year - int (year is in number eg. 2024)
b) Resident of Delhi or not - boolean (because Question is asked)
c) Mobile number - number ( Mobile number is always in numbers)
d) Pocket money - float ( Money can be decimal)
e) Volume of a sphere- float (Volume can be decimal)
f) Perimeter of a square - float (Perimeter can be decimal)
g) Name of the student - String (Because Name can contain only alphabets)
h) Address of the student - String (Because Address can store numbers and alphabets)
Q4. Give the output of the following when num1 = 4, num2 = 3, num3 = 2
A) num1 += num2 + num3
print (num1)
B) num1 = num1 ** (num2 + num3)
print (num1)
C) num1 **= num2 + c
D) num1 = '5' + '5'
print(num1)
print(4.00/(2.0+2.0))
E) num1 = 2+9*((3*12)-8)/10
print(num1)
F) num1 = float(10)
print (num1)
G) num1 = int('3.14')
print (num1)
print(10 != 9 and 20 >= 20)
print(5 % 10 + 10 < 50 and 29 <= 29)
Output:
num1 = 4, num2 = 3, num3 = 2
A) num1 += num2 + num3
print (num1) // 9
B) num1 = num1 ** (num2 + num3)
print (num1) // 59049
C) num1 **= num2 + c // ERROR as c does not contain any value
D) num1 = '5' + '5'
print(num1) // 55 as both the values are string and they will be concat, means they are combined not added.
print(4.00/(2.0+2.0)) // 1.0 First the bracket is solved and than the division will take place.
E) num1 = 2+9*((3*12)-8)/10
print(num1) // 27.2
Expression will be solved like this:
2+9*((3*12)-8)/10
Step 1: Solve the inner most brackets
2+9*(36-8)/10
Step 2: Solve the bracket
2+9*28/10
Step 3: as * and / has the same precedence so we will start the evaluation from LEFT to RIGHT.
2+252/10
2+25.2
27.2
F) num1 = float(10)
print (num1)// 10.0 as 10 is an integer and it is converted to float.
G) num1 = int('3.14') // Will give an ERROR as 3.14 is in quotes and cannot convert to int
print (num1)
print(10 != 9 and 20 >= 20) // will print TRUE as both the conditions are TRUE
print(5 % 10 + 10 < 50 and 29 <= 29) will print TRUE as both the conditions are TRUE
Q5. Categorise the following as syntax error, logical error or runtime error:
a) 25 / 0 // LOGICAL ERROR as their is not ERROR in the Statement
b) num1 = 25; num2 = 0; num1/num2 // LOGICAL ERROR as their is not ERROR in the Statement
Q6. Write a Python program to calculate the amount payable if money has been lent on simple interest. Principal or money lent = P, Rate = 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.
#Principal Amount = P
#Rate = R
#Time = T
#Simple Interest = SI
#Amount Payable = AP
# Formulas SI = (P x R x T)/ 100, AP = P + SI
P = 0
R = 0
T = 0
P = float(input("Enter Principal Amount : "))
R = float(input("Enter Rate of Interest : "))
T = float(input("Enter Time (in Years) : "))
SI = (P * R * T) / 100
AP = P + SI
print("")
print("Amount Payable Calculated ....")
print("Principal Amount : ",P)
print("Rate of Interest : ",R)
print("Time in Years : ",T)
print("Simple Interest : ",SI)
print("Amount Payable : ",AP)
Q7. Write a program to repeat the string ‘‘GOOD MORNING” n times. Here n is an integer entered by the user.
n = 0
x = 0
n = int(input(“Enter number of Times you want to print Good Morning : “))
while x <= n:
print(“GOOD MORNING”)
x = x + 1
Q8. Write a program to find the average of 3 numbers.
num1 = 0
num2 = 0
num3 = 0
avg = 0
num1 = float(input(“Enter First Number : “))
num2 = float(input(“Enter Second Number : “))
num3 = float(input(“Enter Third Number : “))
avg = num1 + num2 + num3
print(“First Number : “,num1)
print(“Second Number : “,num2)
print(“Third Number : “,num3)
print(“Average of 3 Numbers : “,avg)
Q9. 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.
name = ""
age = 0
noofyear=0
name = input("Enter your Name : ")
age = int(input("Enter your Age : "))
noofyear = 100 - age
print("Your Name is : ",name)
print("Your age is : ",age)
print(noofyear," years till you turn 100 Years Old.")
Q10. What is the difference between else and elif construct of if statement?
Ans:
If you have one condition then you will use if else statement, but if you have multiple conditions to check, we have to use if elif .. else statement.
Examples are:
Q11. Find the output of the following program segments:
a)
for i in range(20,30,2):
print(i)
Output:
20
22
24
26
28
b)
country = 'INDIA'
for i in country:
print (i)
Output:
I
N
D
I
A
c) i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print (sum)
Output:
12
Case Study Based QuestIon
Schools use “Student Management Information System” (SMIS) to manage student related data. This system provides facilities for:
Recording and maintaining personal details of students.
• Maintaining marks scored in assessments and computing results of students.
• Keeping track of student attendance, and
• Managing many other student-related data in the school.
Let us automate the same step by step.
Identify the personal details of students from your school identity card and write a program to accept these details for all students of your school and display them in this format.