0% found this document useful (0 votes)
84 views

Class 12 - Python Functions Question Bank 3

Python function

Uploaded by

nitisri218
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Class 12 - Python Functions Question Bank 3

Python function

Uploaded by

nitisri218
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Python Library Functions

Q – 1 What do you mean by Library Function in Python?


Q – 2 Enlist some commonly used libraries of python and explain in short.
Q – 3 Explain the structure of python module.
Q – 4 How to import a python module? or What are the ways to insert python modules in a program?
Ans.: To insert a python module you can use the import statement in the following two ways:
1. import command: Ex. import math
2. from import: Ex. from math import *

Q – 5 Explain some commonly used mathematical functions in detail with example.


Q – 6 Explain some commonly used string functions in detail with example.
Q – 7 Write related library function name to do the following:
1. Display the negative number into positive: abs()
2. Display nearest integer for the given number: ceil()
3. Get the remainder: fmod(), remainder()
4. Get the compute cube of a given number: pow()
5. Get the square root of the given number: sqrt()
6. Convert the first letter of the text into capital: capitalise()
7. Join two words: join()
8. Convert the text into the lower case: lower()

Q – 8 Name the module required to import for the following functions:


1. pow() – math
2. fabs() – math

In the next section of important QnA python library functions 12 you will find some output questions.
Q – 9 Find the output for the following:
1.

def str_exp():
str1= 'COVID-19,Sanitizer,Mask,LockDown'
str1=str1.lower()
str2 =str1.split(',')
for i in str2:
if i<'s':
print(str.lower(i))
else:
print(str.upper(i))
str_exp()

Answer:
covid-19
SANITIZER
mask
lockdown
In the above example, the text starts with the alphabet ‘s’ and letter coming after ‘s’ will be converted into uppercase and rest text will be in
lower case.
2.

def str_exp():
str1='Covid - 19 forces the entire worl to be lockdown'
str1=str1.replace('e','i')
print(str1)
str_exp()

Answer:
Covid – 19 forcis thi intiri worl to bi lockdown
All e replace with the letter i in the text.
3.

def str_exp():
str="Lockdown to Unlock 1.0"
d=str.split()
print(d)
str_exp()

Answer:
[‘Lockdown’, ‘to’, ‘Unlock’, ‘1.0’]
The words will be separated in a string after space
4.

import math
def mth_exp():
a=5.5
b=3.75
c=2.25
print(round(math.fsum([a,b,c]),0))
mth_exp()

Answer:
12.0
Function fsum() returns sum of specified variables a,b,c. The values are 5.5 + 3.75 + 2.25 = 11.5. Then round function returns next integer if
the adjecent digit is more than 5. So the final output will be 12.0
In the next section of important QnA python library functions 12 we will provide you some programs for QnA python library functions
12.
1. Write a program to accept the marks of 5 subjects and do the following:
1. Display the total using fsum() function
2. Display total marks in round integers

Ans.:
import math
def compute_result():
eng=float(input("Enter marks of English:"))
phy=float(input("Enter marks of Physics:"))
che=float(input("Enter marks of Chemistry:"))
mat=float(input("Enter marks of Maths:"))
cs=float(input("Enter marks of Computer Science:"))
tot=round(math.fsum([eng,phy,che,mat,cs]),0)
print("Total:",tot)
compute_result()

2. Write a program to display the computation of power using math module function.
Ans.:

import math
def compute_power():
no=int(input("Enter the number:"))
p=int(input("Enter the power to be raised:"))
ans=math.pow(no,p)
print("The",p,"power of ",no," is:", ans)
compute_power()

3. Write a program to convert the first letter of the sentence into a capital letter.
Ans.:

def first_upper():
s=input("Enter the sentences:(in lower case:)")
print(s.capitalize())
first_upper()

4. Write a program to convert the first letter of each word of the sentence into a capital letter.

def first_upper():
s=input("Enter the sentences:(in lower case:)")
print(s.title())
first_upper()
Important QnA Python Library Functions Class 12

Topics Covered
1. QnA Python Library Functions class 12
2. Theory Questions
3. Application Based questions
3.1. Error Based questions
3.2. Output Questions

QnA Python Library Functions class 12

Theory Questions
1. What is a library?
2. Write a few examples of commonly used libraries with examples.
3. What is the python module? Explain the structure of the python module.
4. How to import the python module?
5. Explain the commonly used python mathematical methods/functions with examples.
6. Explain the commonly used python string/text methods/functions with examples.
Follow this link to find the above answers of QnA Python Library Functions Class 12.
In the next section of QnA Python Library Functions class 12 we will discuss some application based questions.

Application Based questions


Error Based questions

1. def python_lib():
x = float(input(“Enter the number:”))
y = Math.ceil((x)
Assume that, x = 10.66777878

Ans.: There are three errors in the code, import math statement is missing and y = math.ceil(x) should be used.

2. x = 5
y = math.pow(3)

Ans.: There are two errors in the code, import math statement is missing and pow() function required two parameters,

3. a = 10
b = sqrt(a,2)
Assume that math module is imported.

Ans.: sqrt() function requires only one parameter.

4. s = “Welcome to My Blog”
print(str.s.capitalise())

Ans.: Error in print() function. The method capitalize() not written correctly and str word is not required.
Output Questions
Note: Assume that the math module is imported wherever required.
1. x = -45.2342343254325
b = math.fabs(x)
y = math.ceil(b)
print(y+5)

Ans.: 51
fabs() function convert the nagetive value into postive. As ceil() function returns the nearest integer of the passed argument value as well as
in print() function y+5 is written. So math.ceil(x) returns 46, 46 + 5 = 51.

2. import math
def python_lib():
s = “Computer Science with Python”
print(s.swapcase())
print(s.title())
print(s.split())
print(s.replace(‘with’,’-‘))
python_lib()

Ans.:
cOMPUTER sCIENCE WITH pYTHON
Computer Science With Python
[‘Computer’, ‘Science’, ‘with’, ‘Python’]
Computer Science – Python

3. import math
def python_lib():
s = “Python is midlle level language. Learning python is fun.”
c=0
w = s.split()
for i in w:
if i==’is’:
c=c+1
print(c)
python_lib()

Ans.: 2
In this code w = s.split() is used that generates a list of words from the text. Then in the for loop, if condition is given for counting the word
‘is’ is available 2 times in the text. Then finally the counter variable is printed. So the output is 2.

4. import math
def python_lib():
s = “Python is Programing Platform that Playful”
w = s.split()
c=0
for i in w:
if i[0]==’P’:
print(i,end=”#”)
python_lib()

Ans.: Python#Programing#Platform#Playful#
In for loop, i[0] is searching the word starts with letter P and printed in the next line.

5. import math
def python_lib():
s = “Python is Programing Platform that is Playful”
s1=”
print(s.lstrip(‘yPt’))
print(s.rstrip(‘Pul’))
print(s1.join([s,’ in 2020′]))
python_lib()

Ans.:
hon is Programing Platform that is Playful
Python is Programing Platform that is Playf
Python is Programing Platform that is Playful in 2020
Working with Functions XII – 13 Output & Error Questions with
easy solutions

Find the solved host questions for working with functions xii output
and error based questions.

Topics Covered 
1. Working with functions xii Output Questions
2. Error-based questions

Working with functions xii Output Questions


1. def Fun1():
print(‘Python, let’s fun with functions’)
Fun1()
Ans.: Python, let’s fun with functions.
Explanation: The code has a simple print function. is used to print character ‘ in python language.
2. def add(i):
if(i*3%2==0):
i*=i
else:
i*=4
return i
a=add(10)
print(a)
Ans. : 100
Explanation: A add() passed one variable i.e a=10.
So i=10
10 * 3 % 2 =0 means first priority will be given to * operator
30 % 2 = 0, Condition is True as remainder will be 0
i*=i i.e. i=10*10=100.
3. import math
def area(r):
return math.pi*r*r
a=int(area(10))
print(a)
Ans.: 314
Explanation: Function area() has one parameter r, and passed value is 10. When it execute the return statement of function, it calculates
value 3.14*10*10 (pie= 3.141592653589793), so the answer will be 314.1592653589793. Now in call statement int() function is used that
converts answer into interger.
The next question is based on parameters for working with functions xii.
4.def fun1(x, y):
x=x+y
y=x–y
x=x–y
print(‘a=’,x)
print(‘b=’,y)
a=5
b=3
fun1(a,b)
Ans.: a= 3, b= 5
Explanation: a = 5 and b = 3 passed into function. When cursor jumps to function header fun1(x,y) is seems like fun1(5,3). Then
x = x + y i.e. x = 5 + 3 = 8
y = x – y i.e. y = 8 – 3 = 5
x = x – y i.e. x = 8 – 5 = 3
So finally a 3 and b = 5.
5. def div5(n):
if n%5==0:
return n*5
else:
return n+5
def output(m=5):
for i in range(0,m):
print(div5(i),’@’,end=” “)
print(‘n’)
output(7)
output()
output(3)
Ans.:
0 @ 6 @ 7 @ 8 @ 9 @ 25 @ 11 @
0@6@7@8@9@
0@6@7@
Explanation:
The function output(7) à in this case m=7
Now, the range starts with 0 to 6.
When i = 0, div5(i), 0 % 5 = 0 à n * 5 à 0 * 5 à 0, Now jump to loop à print à o @
When i = 1, div5(i), 1 % 5 = 1 à n + 5 à 1 + 5 à 6, Now jump to loop à print à 6 @
When i = 2, div5(i), 2 % 5 = 2 à n + 5 à 2 + 5 à 7, Now jump to loop à print à 7 @
When i = 3, div5(i) 3 % 5 = 3 à n + 5 à 3 + 5 à 8, Now jump to loop à print à 8 @
When i = 4, div5(i) 4 % 5 = 4 à n + 5 à 4 + 5 à 9, Now jump to loop à print à 9 @
When i = 5, div5(i) 5 % 5 = 0 à n + 5 à 5 * 5 à 25, Now jump to loop à print à 25 @
When i = 6, div5(i) 6 % 5 = 1 à n + 5 à 6 + 5 à 25, Now jump to loop à print à 11 @
So line 1 is 0 @ 6 @ 7 @ 8 @ 9 @ 25 @ 11 @
Similarly without parameter m=5, Do your self.
6.def sum(*n):
total=0
for i in n:
total+=i
print(‘Sum=’, total)
sum()
sum(5)
sum(10,20,30)
Ans.:
Sum= 5
Sum= 10
Sum= 30
Sum= 60
Explanation:
Function 1: sum() without argument so output is None.
Function 2: sum(5) à i = 5 à total + = i à total = total + i à 0 + 5 à 5
Function 3: sum(10,20,30) à n(10,20,30) à i = 10 à total + i = 0 + 10 à 10
sum(10,20,30) à n(10,20,30) à i = 20 à total + i = 10 + 20 à 30
sum(10,20,30) à n(10,20,30) à i = 30 à total + i = 30 + 30 à 60
The next question is based on use of Global key word for working with functions xii.
6.def func(b): ×
global x
print(‘Global x=’, x)
y=x+b
x=7
z=x–b
print(‘Local x = ‘,x)
print(‘y = ‘,y)
print(‘z = ‘,z)
x=5
func(10)
Ans: x is used as global and local variable.
Global x= 5
Local x = 7
y = 15
z = -3
Explanation:
Values of variables: x global à 5 , b passed as an argument 10
y = x + b = 5 + 10 à 15
x=7
z = 7 – 10 = -3
This questions is based on default argument for working with functions xii.
7. def func(x,y=100):
temp = x + y
x += temp
if(y!=200):
print(temp,x,x)
a=20
b=10
func(b)
print(a,b)
func(a,b)
print(a,b)
Ans.:
110 120 120
20 10
30 50 50
20 10
Explanation:
Function 1:
func(b)àx=b=10, y=100àtemp = 10 + 100 = 110àx = 110 + 10à120à120!=200 à 120
So Line 1, print(temp,x,x) à temp=110, x 120 à 110 120 120
Line 2 print(a,b) à 20 10
Function 2:
Func(a,b)à x=20, y=10àtemp = 20 + 10à30à x = 30 + 20 à 50 à 50!=200à50
Line 3 print(temp,x,x) à temp = 30, x= 50 à 30 50 50
Line 4 print(a,b) à 20 10
8. def get(x,y,z):
x+=y
y-=1
z*=(x-y)
print(x,’#’,y,’#’,z)
def put(z,y,x):
x*=y
y+=1
z*=(x+y)
print(x,’$’,y,’$’,z)
a=10
b=20
c=5
put(a,c,b)
get(b,c,a)
×
put(a,b,c)
get(a,c,b)
Ans.:
100 $ 6 $ 1060
25 # 4 # 210
100 $ 21 $ 1210
15 # 4 # 220
Explanation:
Function 1:put(a,c,b)àa=z=10, c=y=5, b=x=20
x= 5 x 20 à 100 à y= 5 + 1 = 6à z = 10 * (100 +6) à 1060
Line 1 print(x,’$’,y,’$’,z) à 100 $ 6 $ 1060
Function 2:get(b,c,a)àb=x=20, c=y=5, z=a=10
x = 20 + 5 = 25 à y = 5 – 1 = 4 à z = 10 * (25-4) à 10 * 21 à 210
Line 1 print(x,’$’,y,’$’,z) à 25 # 4 # 210
Similarly function 3 & function 4 will execute, do in practice.
In the next section of working with functions xii you will get error-based questions.

Error-based questions
1. def in(x,y):
x=x + y
print(x.y)
x*=y
print(x**y)
Ans.: def in(x,y): à in can’t be used as function name because it’s a keyword
print(x.y) à The variable in python print() function separate by comma not dot
The next questions is based on default argument for working with functions xii.
2. void get(x=10,y):
x=x+y
print(x,n,y)
Ans.: void get(x=10,y): à Default argument must be assign a value from right to left
print(x,n,y) à ‘n’ must be enclosed with quotes
3. // Program to compute result
def res():
eng = 56
math = 40
sci = 60
if eng<=35 || math<=35 ||
sci=35
print(‘Not Qualified’)
else:
print(“Qualified”)
Ans.: // Program to compute result à // is not used in python, it should be #
if eng<=35 || math<=35 || à || is not any operator in python, it should be or
sci=35 à It should written in above line or should be use in upper line
This question is based on positional argument for working with functions xii.
4. a=5, b=10
def swap(x,y):
x=a+b
y=x–y
x=x–y
swap(a)
swap(15,34)
swap(b)
swap(a,b)
Ans.: a=5, b=10 à In python variable should not assign in single line by comma
swap(a) à The positional arguments must be passed
swap(b) à Same as above
5. def cal_dis(qty,rate=50,dis_rate): #discount rate = 5%
bil_amt = (qty*rate)*dis_rate
print(bil_amt)
caldis(10)
cal_dis(10,50,0.05)
Ans.: def cal_dis(qty,rate=50,dis_rate=0.05): #discount rate = 5%
caldis(10) à Function call statement; cal_dis should be there
So here I have tried with the most common topics from working with functions xii with questions and answers.
For all the contents of computer science click on the below given link:

https://www.tutorialaicsip.com/cs-xii/computer-science-class-12-python/

You might also like