KnowledgeBoat Logo
OPEN IN APP

Chapter 13

Introducing Python

Class 9 - Sumita Arora CBSE Computer Code 165



Objective Type Questions

Question 1

Which of the following is not a legal type in Python ?

  1. Integer
  2. String
  3. List
  4. Generic

Answer

Generic

Reason — Generic is not a legal type in Python while Integer, String and List are legal data types in Python.

Question 2

Which of the following allow you to take input from user ?

  1. raw_input( )
  2. enter( )
  3. eval( )
  4. input

Answer

raw_input( ) and input

Reason — raw_input( ) and input() functions allow us to take input from user.

Question 3

Which of the following functions will you use to find out the type of a variable ?

  1. datatype( )
  2. type( )
  3. typeof( )
  4. dtype( )

Answer

type( )

Reason — The type( ) function is used to find out the type of a variable.

Question 4

To convert input received via raw_input( ) of Python 2.x in a numeric type, which of the given functions can be used ?

  1. convert( )
  2. float( )
  3. int( )
  4. No conversion required as input is already in desired form.

Answer

float( ) and int( )

Reason — Python offers float( ) and int( ) functions to be used with raw_input() to convert the values received through raw_input() into integer and float types.

Question 5

To convert input received via input( ) of Python 2.x in a numeric type, which of the given functions can be used ?

  1. convert( )
  2. float( )
  3. int( )
  4. No conversion required as input in already in desired form.

Answer

No conversion required as input in already in desired form.

Reason — The input() evaluates the given input and returns the value accordingly. Thus, no conversion is required as input in already in desired form.

Theoretical Questions

Question 1

How are floating constants represented in Python ? Give examples to support your answer.

Answer

Floating constants are represented in Python in two forms — Fractional Form and Exponent form. Examples:

  1. Fractional Form — 2.0, 17.5, -13.0, -0.00625
  2. Exponent form — 152E05, 1.52E07, 0.152E08, -0.172E-3

Question 2

How are string-literals represented and implemented in Python ?

Answer

A string-literal is represented as a sequence of characters surrounded by quotes (single, double or triple quotes). String-literals in Python are implemented using Unicode.

Question 3(i)

What will be the output produced by following code fragment ?

X = 10
X = X + 10
X = X - 5
print X
X, Y = X - 2, 22
print X, Y

Answer

Output
15
13 22
Explanation
  1. X = 10 ⇒ assigns an initial value of 10 to X.
  2. X = X + 10 ⇒ X = 10 + 10 = 20. So value of X is now 20.
  3. X = X - 5 ⇒ X = 20 - 5 = 15. X is now 15.
  4. print (X) ⇒ print the value of X which is 15.
  5. X, Y = X - 2, 22 ⇒ X, Y = 13, 22.
  6. print (X, Y) ⇒ prints the value of X which is 13 and Y which is 22.

Question 3(ii)

What will be the output produced by following code fragment ?

first = 2
second = 3
third = first * second
print first, second, third 
first = first + second + third 
third = second * first
print first, second, third

Answer

Output
2 3 6
11 3 33
Explanation
  1. first = 2 ⇒ assigns an initial value of 2 to first.
  2. second = 3 ⇒ assigns an initial value of 3 to second.
  3. third = first * second ⇒ third = 2 * 3 = 6. So variable third is initialized with a value of 6.
  4. print first, second, third ⇒ prints the value of first, second, third as 2, 3 and 6 respectively.
  5. first = first + second + third ⇒ first = 2 + 3 + 6 = 11
  6. third = second * first ⇒ third = 3 * 11 = 33
  7. print first, second, third ⇒ prints the value of first, second, third as 11, 3 and 33 respectively.

Question 3(iii)

What will be the output produced by following code fragment ?

side = int(raw_input('side'))	#side given as 7
area = side * side
print side, area

Answer

Output
7 49
Explanation
  1. side = int(input('side') ) ⇒ This statements asks the user to enter the side. We enter 7 as the value of side.
  2. area = side * side ⇒ area = 7 * 7 = 49.
  3. print (side, area) ⇒ prints the value of side and area as 7 and 49 respectively.

Question 4

What are data types ? How are they important ?

Answer

Data types are used to identify the type of data a memory location can hold and the associated operations of handling it. The data that we deal with in our programs can be of many types like character, integer, real number, string, boolean, etc. Hence programming languages including Python provide ways and facilities to handle all these different types of data through data types.

The data types define the capabilities to handle a specific type of data such as memory space it allocates to hold a certain type of data and the range of values supported for a given data type, etc.

Question 5

Write instructions to get the following result :

Math is Fun so don't be resistant
Just learn the rules, the rules are consistent 
And most important, you must be persistent !

Adding fractions, get common denominators.
Multiply by missing factors to get the denominators. 
Add numerators only, NOT denominators.

Do it in both interactive mode and script mode.

Answer

Interactive Mode
>>> print("Math is Fun so don't be resistant")
Math is Fun so don't be resistant
>>> print("Just learn the rules, the rules are consistent")
Just learn the rules, the rules are consistent
>>> print("And most important, you must be persistent !")
And most important, you must be persistent !
>>> print("")

>>> print("Adding fractions, get common denominators.")
Adding fractions, get common denominators.
>>> print("Multiply by missing factors to get the denominators.")
Multiply by missing factors to get the denominators.
>>> print("Add numerators only, NOT denominators.")
Add numerators only, NOT denominators.
>>>


Script Mode
Solution
print("Math is Fun so don't be resistant")
print("Just learn the rules, the rules are consistent")
print("And most important, you must be persistent !")
print("")
print("Adding fractions, get common denominators.")
print("Multiply by missing factors to get the denominators.")
print("Add numerators only, NOT denominators.")
Output
Math is Fun so don't be resistant
Just learn the rules, the rules are consistent
And most important, you must be persistent !

Adding fractions, get common denominators.
Multiply by missing factors to get the denominators.
Add numerators only, NOT denominators.

Question 6

How many integer types are supported by Python ? Name them.

Answer

Two integer types are supported by Python. They are:

  1. Integers (signed)
  2. Booleans

Question 7

Differentiate between plain integer and long integer.

Answer

Plain integers generally use 32 bits (4 bytes) to store a value. Long integers, on the other hand, don't have any size limitation for storing the value, they can grow arbitrarily large subject to available memory.

Question 8

Write instructions in interactive mode for the following :

(i) to display sum of 8, 9.0, 2 * 3

(ii) to print sum of 8, 9.0, 2 * 3

Answer

(i)

>>> 8 + 9.0 + 2 * 3
23.0

(ii)

>>> print(8 + 9.0 + 2 * 3)
23.0

Question 9

Write a program that generates the following output :

5
10
9

Assign value 5 to a variable using assignment operator (=). Multiply it with 2 to generate 10 and subtract 1 to generate 9.

Answer

Solution
a = 5
print(a)
a = a * 2
print(a)
a = a - 1
print(a)
Output
5
10
9

Question 10

Write a Python program that accepts radius of a circle and prints its area.

Answer

Solution
r = float(input("Enter radius of circle: "))
a = 3.14159 * r * r
print("Area of circle =", a)
Output
Enter radius of circle: 7.5
Area of circle = 176.7144375

Question 11

Write Python program that accepts marks in 5 subjects and outputs average marks.

Answer

Solution
m1 = int(input("Enter first subject marks: "))
m2 = int(input("Enter second subject marks: "))
m3 = int(input("Enter third subject marks: "))
m4 = int(input("Enter fourth subject marks: "))
m5 = int(input("Enter fifth subject marks: "))
avg = (m1 + m2+ m3+ m4 + m5) / 5;
print("Average Marks =", avg)
Output
Enter first subject marks: 65
Enter second subject marks: 78
Enter third subject marks: 79
Enter fourth subject marks: 80
Enter fifth subject marks: 85
Average Marks = 77.4

Question 12

Write a program to read a number n and print n2, n3 and n4.

Answer

Solution
n = int(input("Enter n: "))
n2, n3, n4 = n ** 2, n ** 3, n ** 4
print("n =", n)
print("n^2 =", n2)
print("n^3 =", n3)
print("n^4 =", n4)
Output
Enter n: 2
n = 2
n^2 = 4
n^3 = 8
n^4 = 16

Question 13

Write a program to compute simple interest and compound interest.

Answer

Solution
p = float(input("Enter principal: "))
r = float(input("Enter rate: "))
t = int(input("Enter time: "))
si = (p * r * t) / 100
ci = p * ((1 + (r / 100 ))** t) - p
print("Simple interest = ", si)
print("Compound interest = ", ci)
Output
Enter principal: 15217.75
Enter rate: 9.2
Enter time: 3
Simple interest =  4200.098999999999
Compound interest =  4598.357987312007

Question 14

Write a program to read details like name, class, age of a student and then print the details firstly in same line and then in separate lines.

Make sure to have two blank lines in these two different types of prints.

Answer

Solution
n = input("Enter name of student: ")
c = int(input("Enter class of student: "))
a = int(input("Enter age of student: "))
print("Name:", n, "Class:", c, "Age:", a)
print()
print()
print("Name:", n)
print("Class:", c)
print("Age:", a)
Output
Enter name of student: Kavya
Enter class of student: 11
Enter age of student: 17
Name: Kavya Class: 11 Age: 17


Name: Kavya
Class: 11
Age: 17

Application Oriented Questions

Question 1

(a) What will be the output produced by following code fragments ?

(i)

a, b = 2, 3
c, b = a, c + 1 
print a, b, c

(ii)

x, y = 2, 6
x, y = y, x + 2
print x, y

(b) What will the output produced if we interchange the first two lines of above two codes [given in part (a)], e.g., as shown below for code (i) ? Will it give any error ? Give reason(s).

c, b = a, c + 1 
a, b = 2, 3
print a, b, c

Answer

(a)

(i) This code fragment will generate the below error:

NameError: name 'c' is not defined

Reason — The error is generated because in line 2, the variable c is not defined before being used.

(ii)

Output
6 4
Explanation
StatementValue of XValue of YRemark
x, y = 2, 626Both x, y are initialized
x, y = y, x + 264x = 6, y = 2 + 2 = 4
print (x, y)646 (x), 4 (y) are printed

(b) When we interchange the first two lines of above codes, we get the following outputs:

(i) It will give the same error as before because even after interchanging first two lines variable C is still used before it is defined.

(ii) Interchanging the first two lines will generate the below error:

NameError: name 'y' is not defined

Reason — The error is generated because now in line 1, the variable y is not defined before being used.

Question 2

A bank account contains ₹10,000 and earns interest at the rate of 5 percent per year. The interest is added to the account. After one year the bank account contains the original sum of ₹10,000 plus the interest, which is 5% of ₹10,000, i.e., 10,000 * (5/100).

So, the final account balance is 10000 + 10000 * (5/100) = 10000 * (1 + (5/100)) = 10000 * 1.05.

(a) Write Python code script to calculate the balance in account as per this after one year.

(b) Write Python code script to calculate the balance in account as per this after four years while showing balance after every year.

(c) Write Python script that displays the number of takes for the account balance to be at least double the original balance.

Answer

(a) Python code script to calculate the balance in account after one year:

p = 10000
r = 5
si = p * r  / 100
bal = p + si
print ("Balance = ₹", bal)
Output
Balance = ₹ 10500.0

(b) Python code script to calculate the balance in account after four year while showing balance after every year:

p = 10000
r = 5
si = p * r / 100
p = p + si
print ("Balance after 1st year = ₹", p) 
si = p * r / 100
p = p + si
print ("Balance after 2nd year = ₹", p) 
si = p * r / 100
p = p + si
print ("Balance after 3rd year = ₹", p) 
si = p * r / 100
p = p + si
print ("Balance after 4th year = ₹", p) 
Output
Balance after 1st year = ₹ 10500.0
Balance after 2nd year = ₹ 11025.0
Balance after 3rd year = ₹ 11576.25
Balance after 4th year = ₹ 12155.0625

(c) Python code script to display the number of takes for the account balance to be at least double the original balance:

p = 10000
r = 5
t = p * 2
c = 0
while p < t:
    si = p * r  / 100
    p = p + si
    c = c + 1
print ("No. of takes =", c)
Output
No. of takes = 15
PrevNext