Class IX Part 3 Python 1 (1)
Class IX Part 3 Python 1 (1)
4. Why Python?
Python is preferred programming language due to the following reasons:
a. Easy to learn: It has fewer keywords and simple structure of instructions, so less efforts to
learn Python.
b. Free and open source
c. Portable: A Python program can run on wide variety of hardware platforms.
d. Extensive library support: Python saves times and effort using the support of extensive
library.
As an example, YouTube has been largely written in Python. Google makes extensive use of Python.
NASA uses Python for scientific programming tasks.
Advantages:
• Python, in interactive mode, is good enough to learn, experiment or explore.
• Working in interactive mode is convenient for beginners and for testing small pieces of code.
Drawback:
• We cannot save the statements and have to retype all the statements once again to re-run them.
A variable is created the moment you first assign a value to it. Variables do not need to be declared
with any type and can even change type after they have been set.
For example:
x=5
y = "John"
Here x is variable which is assigned a value 5 which is automatically declared as integer as data type
and y is variable which is assigned a value ‘John’ which is automatically declared as string data type.
Python supports a number of data types such as number, string, list, tuple, set and dictionary. We will
discuss about number and string data type here:
Number: The following are the two important number data type:
a. int – int stands for integer which is a positive or a negative number without a decimal point.
There are three types of integers in Python:
Example: a = 7; b=-90
b. float – float or floating-point number is positive or negative number with a fractional part.
Example: c = 9.7; d = -88.9
Strings: Strings in Python are a sequence of characters. These characters are enclosed within single
quotes (‘), double quotes (“) or triple quotes (“). Triple quotes are used when the string value extends
over more than one line.
Example of strings:
name = “Myra”
section = ‘A’
message = ‘‘‘A dream does not become reality through magic; it takes sweat, determination, and hard
work. ~ Colin Powell’’’
Page 2 of 12
Document Author: Ashrya Soni
13. List all basic types of arithmetic operators used in Python. Use examples.
The following are the list of arithmetic operators most used in Python:
Operator Description Example Result Comment
+ Addition 4+3 7
- Subtraction 9 - 18 -9
* Multiplication 7*5 35
/ Division 36 / 6 6
// Floor Division 7.2 // 2 3.0 divides the truncates the fractional part from
the result.
% Remainder 35 % 6 5 divides the two operands and gives the
remainder resulting.
** Exponent 3 ** 4 81 returns base raised to power exponent. 34
here.
For example, p = 20, q = 25, so p == q is false and p != q is true as 20 is not equal to 25.
15. List all basic types of relational operators used in Python. Use examples.
The following are the list of arithmetic operators most used in Python:
Operator Description Examples
== Equal to (Equality) a = 10, b = 8 so, a == b is false
x = 15, y = 15, so x == y is true
!= Not equal to (Inequality) a = 10, b = 8 so, a != b is true
x = 15, y = 15 so x != y is false
< Less than a = 10, b = 8 so, a < b is false
x = 15, y = 15, so x < y is false
<= Less than or equal to a = 10, b = 8 so, a <= b is false
x = 15, y = 15, so x <= y is true
> Greater than a = 10, b = 8 so, a > b is true
x = 15, y = 15, so x > y is false
>= Greater than or equal to a = 10, b = 8 so, a >= b is true
x = 15, y = 15, so x >= y is true
16. Can relational operators be used with String values? Explain with examples.
Equality (==) and Inequality (!=) operators can be used to compare the values of the string.
For example,
a. x = “Hi” and y = “Hello”, so x == y is false as Hi and Hello are not same and x != y is true.
b. a = “Good Bye” and b = “Good Bye”, so a == b is true and a != b is false.
17. What are logical operators in Python? List all logical operators. Explain with an example.
Logical operators are used when multiple conditions are to be tested. They give the result as true or
false.
The following are the list of logical operators:
Operators Description
and The and operator gives a True value only if both conditions are True. It gives a
False value if either or both of the two conditions are False.
or The or operator gives a True value only if both or either of the two conditions is
True. It gives a False value only if both the conditions are False.
not The not operator gives a True value if the condition is False and a False value if the
condition is True.
Page 3 of 12
Document Author: Ashrya Soni
For example, assume age = 12, sports = “cricket”
The table below shows the result of logical operators when used with variables age and sports.
Expression Result
age > 10 and sports == “football” False
age > 10 or sports == “football” True
not (sports == “cricket”) False
Syntax:
if condition:
statement (s) {block of statement(s) executed when condition is true}
else:
statement (s) {block of statement(s) executed when condition is false}
20. What is if… elif… else statement? What is the syntax? Use examples wherever necessary.
if… elif… else is similar to if… else statement and used in situation where multiple conditions
are to be checked.
For example, grading system of school examination such as A, B, C, D, E. different marks scored by
candidates result in different grades, so multiple conditions are to be checked.
Syntax:
if condition:
statement (s) {block of statement(s) executed when if condition is true}
elif:
statement (s) {block of statement(s) executed when elif condition is true}
else:
statement (s) {block of statement(s) executed when none of the condition is true}
22. What is range ( ) FUNCTION in Python? What is its syntax? Explain with examples.
The built in function range ( ) in Python is used to generate a sequence of numbers. This function can
take a maximum of three integer arguments.
Syntax:
range ([start], stop, [step])
Argument Description
start Optional Argument: An integer specifying the starting value of the sequence.
Default value is 0.
Page 4 of 12
Document Author: Ashrya Soni
stop Required Argument: An integer specifying the value at which the sequence will stop.
The stop value is not included in the sequence.
step Optional Argument: An integer specifying the incrementation. Default value is 1.
Examples:
Syntax:
for <control-variable> in <sequence/item in range>:
body of the loop {block of statement(s) to be executed}
Example: Write any of the program from Practical Questions Q. No. 9, 10 and 11
Syntax:
while condition:
body of the loop {block of statement(s) to be executed}
Example: Write any of the program from Practical Questions Q. No. 12 and 13
Answers:
1. Operator 2. IDLE 3. relational 4. or
5. inequality 6. conditional 7. if… elif… else
Page 5 of 12
Document Author: Ashrya Soni
Multiple Choice Questions
1. Which of the following statements is used to display the result of the expression 43.
a. print (3**4) b. print (4%3) c. print (4**3) d. print (4*3)
3. For which of the following options will the condition (fruit==“apple” or qty>10) evaluate to
FALSE?
a. fruit=“mango”, qty=15 b. fruit=“apple”, qty=5
c. fruit=“apple”, qty=100 d. fruit=“mango”, qty=5
4. For which of the following options will the condition (fruit==“apple” and qty>10) evaluate to
TRUE?
a. fruit=“mango”, qty=15 b. fruit=“apple”, qty=5
c. fruit=“apple”, qty=100 d. fruit=“mango”, qty=5
if score == 100:
print ("You win")
elif score >= 50:
print ("You did well")
elif score >= 25:
print ("You should practice more")
else:
print ("You do not know the game")
Page 6 of 12
Document Author: Ashrya Soni
10. How many times will the following loop run?
while (3==3):
print (“Hello World”)
a. does not run b. 2 times c. 3 times d. Infinite times
Answers:
1. c 2. c 3. d 4. c 5. d 6. d
7. c 8. b 9. d 10. d 11. d
Explanation:
1. ** is the operator used for exponent.
2. = is assignment operator while all others are relational or comparison operator.
3. Using ‘or’ operator, both conditions to be false for the entire statement to be false.
4. Using ‘and’ operator, both conditions to be true for the entire statement to be true.
5. Since score = 15, is neither = 100 nor >= 50 nor >=25, so statement after else will be executed.
6. for statement and while statement are used for loops in Python. There is no do while statement.
7. Self-explanatory
8. range (1) = range (0, 1, 1). So, the sequence generated is 0 (once). So, loop will run once.
9. range (1, 101) = range (1, 101, 1), so sequence generated is 1, 2, 3, … … …, 99, 100.
10. Since 3 will always be equal to 3, so loop will execute infinite time as the condition will never be false.
11. Step value in range ( ) function cannot be set to zero.
Practical Questions
1. Find the output of the following if the variables subject, fee and num have been assigned the
following value:
subject = “English”, fee = 2000, num = 100
Expression Result
subject != “Hindi”
(fee + 100) <= 3000
(num * 2) == (fee / 10)
(subject == “English”) and (fee == 2000)
(num == 200) or (fee != 1000)
Answer:
As given in question, subject = “English”, fee = 2000, num = 100
Page 7 of 12
Document Author: Ashrya Soni
(num == 200) or (fee != 1000) True The value of num given in the question is 100,
so (num == 200) is false.
The value of fee given in the question is 2000,
so (fee != 1000) is true.
As one of the conditions is true using ‘or’
operator, so answer is true.
2. Write a program that takes the name of the user and then displays a welcome message to the user.
The welcome message should be:
<User name>, Welcome to the world of Python Programming
Program:
name = input (“Enter your name: ”)
msg = name + “, Welcome to the world of Python Programming”
print (msg)
3. Write a program to take name and country from the user and then display the following message to
the user:
Welcome <user name>
Your country <country name> is a beautiful country
Program:
u_name = input (“Enter your name: ”)
c_name = input (“Enter name of your country: ”)
line1 = “Welcome ” + u_name
line2 = “Your country ” + c_name + “ is a beautiful country”
print (line1)
print (line2)
4. Write a program to take three integers from user and display their sum.
Program:
num1 = int (input (“Enter the first number: ”))
num2 = int (input (“Enter the second number: ”))
num3 = int (input (“Enter the third number: ”))
sum = num1 + num2 + num3
print (“The sum of three numbers = ”, sum)
5. Write a program to take length and breadth of a rectangle from the user and display its area and
perimeter.
Program:
l = int (input (“Enter the length of the rectangle: ”))
b = int (input (“Enter the breadth of the rectangle:”))
area = l * b
perimeter = 2 * (l + b)
6. Write a program that displays a message based on the age entered by the user.
Condition Message
>= 18 You have the right to cast vote
< 18 You cannot cast a vote
Page 8 of 12
Document Author: Ashrya Soni
Program:
Program:
8. Write a program to decide the grade scored by candidate in an examination on the basis of marks
scored out of 100 as per the following:
Program:
Program:
n = 5
for i in range (1, n+1):
print (i*i)
Program:
num = int (input (“Enter the number whose table is to displayed: ”))
for i in range (1, 11):
print (num, “*”, i, “=”, num*i)
Page 9 of 12
Document Author: Ashrya Soni
11. Write a program to display the sum of even numbers between 1 and 100.
Program:
sum = 0
for i in range (2, 101, 2):
sum = sum + i
print (“Sum of even number between 1 and 100: ”, sum)
12. Write a program to display odd numbers less than or equal to 15.
Program:
num = 1
while num <= 15:
print (num)
num = num + 2
13. Write a program to take in as many numbers as user wants and display their sum.
Program:
sum = 0
choice = ‘y’
while choice == ‘y’ or choice == ‘Y’:
num = int (input (“Enter a number: ”))
sum = sum + num
choice = input (“Enter y to continue: ”)
print (“Sum of numbers entered by the user: ”, sum)
Answers: The following are the correct statements after removing errors:
a. Name = “Sheela”
b. print (“sum is”, 3+5)
c. p=float(input(“Enter a number”))
d. print ("I like", "Swimming")
e. age = int (input (“Enter your age”))
f. print ("it", "is", "a beautiful day")
4. a = 3 5. a = “Important”
b = 2 b = “Message”
c = a * b print (b+“ is ”+a)
d = c * b
print (a, b, c, d)
Page 10 of 12
Document Author: Ashrya Soni
Answers: The following will be the output of each of the code:
1. Output 2. RESULT 3. 1234
answer= 9.0 12
17
4. 3 2 6 12 5. Message is Important
16. Consider the following code and answer the questions that follows:
Answer:
a. Three
b. Variable ‘name’
c. The following will be the output:
Rishi, You have to plant 300 saplings
17. Consider the following code and answer the questions that follows:
d. What is the data type of the value in the variable marks and grade?
e. What grade will be assigned to a student if the marks are 76?
f. Under what condition would a student be assigned the grade “D”?
Answer:
a. Data type of marks is integer and data type of grade is string.
b. Grade C will be assigned.
c. Grade D will be assigned to student if the student score less than 70 marks.
18. Ten families reside in a society. Consider the given code and answer the questions that follow:
count_car = 0
for i in range (1, 11):
print (“Resident Number: ”, i)
choice_car = input (“Enter Y if you own a car: ”)
if choice_car == “Y”:
count_car = count_car + 1
print (“Number of car owners: ”, count_car)
Page 11 of 12
Document Author: Ashrya Soni
a. What does the above program do?
b. How many times will the loop run?
c. What happens if the resident enters ‘y’ (alphabet y in lowercase) in response to the question?
d. What statements should be added to the program to display the count of residents who do
not own a car?
Answer:
a. The above program will ask the resident individually if they own a car. Based on the response,
the program will display the number of residents in society who owns a car.
b. 10 times.
c. The users will not be included in the resident who owns a car.
d. The following statement to be added at the end of the code:
print ("Number of residents without a car: ", 10 - count_car)
In the above code, notice the space in the line 2, that is, grade 2 start a little away from the actual
start as it is the part which is to be executed if the condition is true.
4. Use colon (:) correctly in case of if… else… statement, for loop and while loop.
5. Use brackets correctly after the command. Mostly “(” and “)” to be used correctly.
6. Use single, double, and triple quotes correctly.
Page 12 of 12
Document Author: Ashrya Soni