Class _ XI & XII – Computer Science – Python Functions
1 keyword is used to define a function 1
2 Function will perform it section only when it is 1
3 Write statement to call the function. 1
def Add():
X = 10 + 20
print(X)
#statement to call the above function
4 Write statement to call the function. 1
def Add(X,Y):
Z = X+Y
print(Z)
#statement to call the above function
5 Which Line Number Code will never execute? 1
defCheck(num):
#Line1
if num%2==0:
#Line2
print("Hello") #Line3
return True #Line4
print("Bye") #Line5 [Hint
print() given after return statement]
else: #Line6
return False #Line7
x = int(input(‘Enter a value’)
C =Check(x)
print(C)
6 What will be the output of following code? 1
def Cube(n):
print(n*n*n)
Cube(n) #n is 10 here print(Cube(n))
7 What are the different types of actual arguments in function? Give example of any 1
one of them.
8 What will be the output of following code: 2
def Alter(x,y=10,z=20)
sum=x+y+z
print(sum)
Alter(10,20,30)
Alter(20,30)
Alter(100)
9 What will be the output of following code? 2
def Calculate(A,B,C):
return A*2, B*2, C*2
val = calculate(10,12,14)
print(type(val))
print(val)
1 What will be the output of following code? 2
0 def check():
num = 50
print num
num = 100
print (num)
check ()
print (num)
1 What will be the output of following code? 2
1 def check():
global num
num=1000
print(num)
num=100
print(num)
check()
print(num)
1 What will be the output of following code? 2
2 print(“Welcome!”)
print(“I am“, _name__) # is double underscore
1 Write a function called two_words(a, b) that accepts two words as an argument. 3
3
Hint: The function should check whether the two words are made of same letters
or not. The function should return a boolean True if words are made of same
letters else False
1 Write a function called evensum(s) that accepts a non-empty integer list as an 3
4 argument.
Hint: The function should return the sum of all the even numbers in the list.
1 Write a function called counta(s) that accepts a string as an argument. 3
5
The function should count the number of occurrence of letter 'a' and return the
count.
Hint: the count should be case insensitive, means upper case 'A' is also counted.
1 Write a function called getsum(s) that accepts a non-empty integer list as an 3
6 argument.
1 Write a function called alllowercase(s) that accepts a string as an argument and 3
7 lower the characters.
Hint: The function should return a string with all characters in lower case.
1 Write a function called calculate_average(a, b) that accepts two numeric values as 3
8 the arguments.
The function should return the average of two numbers.
Hint: return value should be average of numbers and rounded up to only two
place.
1 Write a function called remove_underscores(s) that accepts a string as an 3
9 argument.
2 What would be the answer of the following code: 3
0 >>> a = array([[1,2,3], [4,5,6], [7,8,9]])
>>> b = a
>>> a[0, 2] = 9
>>> b[0, 1] = 5
Ans 1 Def 1
Ans 2 Called / Invoked or any other word with similar meaning 1
Ans 3 Add() 1
Ans 4 Add(10,20) # Parameter value is userdependent 1
Ans 5 Line 5 1
Ans 6 1000 1
1000
None
Ans 7 1. Positional 1
2. Keyword
3. Default
4. Variable length argument
Example : Keyword Argument—
def Interest(principle,rate,time):
return (principal*rate*time)/100
R = Interest(rate=.06, time=7,principal=100000)
Ans 8 60 2
70
130
Ans 9 <class 'tuple'>(20, 24, 28) 2
Ans 100 2
10 50
100
Ans 100 2
11 1000
1000
Ans Welcome! 2
12 Iammain
Ans def two_words(a,b): 3
13 char_a = set([char for char in a])
char_b = set([char for char in b])
return char_a == char_b
Ans def evensum(l1): 3
14 sum=0
for i in l1:
if i%2==0:
sum=sum+i
return sum
Ans def counta(s): 3
15 x = s.count('a') + s.count('A')
return x
Ans def getsum(list1): 3
16 sum=list1[0]+list1[-1]
return sum
Ans def alllowercase(s): 3
17 return s.lower()
Ans def calculate_average(a, b): 3
18 c=(a+b)/2
return(round(c,2))
Ans def remove_underscores(s): 3
19 s=s.strip("_")
l = len(s)
for i in range(l,0,-1):
sub="_"*i
s=s.replace(sub,"_")
s=s.replace(" _"," ")
s=s.replace("_ "," ")
return s
Ans array([[1, 5, 9], 3
20 [4, 5, 6],
[7, 8, 9]])