Class 12th Computer Worksheet
Class 12th Computer Worksheet
Class 12th Computer Worksheet
FUNCTIONS IN PYTHON
Short Answer Type Questions
1. Name the built-in mathematical function/method that is used to return an absolute value of a number.
2. Find and write the output of the following python code:
def myfunc(a):
a=a+2
a=a*2
return a
print(myfunc(2))
3. What is the default return value for a function that does not return any value explicitly?
4. Name the keyword use to define function in Python.
5. Predict the output of following code snippet:
def function1(a):
a=a+‟1‟
a=a*2
function1(„Hello‟)
6. Variable defined in function referred to _________variable.
7. Name the argument type that can be skipped from a function call.
8. Positional arguments can be passed in any order in a function call. (True/False)
9. Which of the following is function header statement is correct.
a. def fun(x=1,y) b. def fun(x=1,y,c=2) c. def fun(a,y=3)
10.Predict the output of following code snippet.
def printDouble(A):
print(2*A)
print(3,end=” ”)
print(printDouble(4))
1. Observe the following Python code very carefully and rewrite it after removing all syntactical errors
with each correction underlined.
DEF result_even( ):
x = input(“Enter a number”)
if (x % 2 = 0) :
print (“You entered an even number”)
else:
print(“Number is odd”)
even ( )
2. Ravi a python programmer is working on a project, for some requirement, he has to define a function
with name CalculateInterest(), he defined it as:
def CalculateInterest (Principal, Rate=.06,Time): # code
But this code is not working, Can you help Ravi to identify the error in the above function and what is
the solution.
3. Predict the output of the following python code:
def guess(s):
n = len(s)
m=""
for i in range(0, n):
if (s[i] >= 'a' and s[i] <= 'm'):
m = m +s[i].upper()
elif (s[i] >= 'n' and s[i] <= 'z'):
m = m +s[i-1]
elif (s[i].isupper()):
m = m + s[i].lower()
else:
m = m +'#'
print(m)
guess("welcome2kv")
5. Rewrite the following code in python after removing all syntax errors. Underline each correction done
in the code:
Def func(a):
for i in (0,a):
if i%2 =0:
s=s+1
elseif i%5= =0
m=m+2
else:
n=n+i
print(s,m,n)
func(15)
Application Based Questions
1. Write a function listchange(Arr)in Python, which accepts a list Arr of numbers , the function will
replace the even number by value 10 and multiply odd number by 5 .
Sample Input Data of the list is:
a=[10,20,23,45]
listchange(a,4)
output : [10, 10, 115, 225]
3. Write a function REP which accepts a list of integers and size of list and replaces elements having even
values with its half and elements having odd values with twice its value. eg: if the list contains
3, 4, 5, 16, 9
then the function should rearranged list as
6, 2,10,8, 18
4. Write a function which accept the two lists, and returns a list having only those elements that are
common between both the lists (without duplicates) in ascending order.
Make sure your program works on two lists of different sizes. e.g.
L1= [1,1,2,3,5,8,13,21,34,55,89]
L2= [20,1,2,3,4,5,6,7,8,9,10,11,12,13]
The output should be:
[1,2,3,5,8,13]
5. Write a user defined function countwords() to accept a sentence from console and display the total
number of words present in that sentence.
For example if the sentence entered by user is:
“Living a life you can be proud of doing your best.” then the countwords() function should display the
output as:
Total number of words : 11