Professional Documents
Culture Documents
FUNCTIONS - Workksheet
FUNCTIONS - Workksheet
4.
5.
6. Vivek has written a code to input a number and check whether it is even or
odd number. His code is having errors. Rewrite the correct code and underline
the corrections made.
Def checkNumber(N):
status = N%2
return
#main-code
num=int( input(“ Enter a number to check :))
k=checkNumber(num)
if k = 0:
print(“This is EVEN number”)
else:
print(“This is ODD number”)
7. Write a function lenFOURword(L), where L is the list of elements (list of
words) passed as argument to the function. The function returns another list
named ‘indexList’ that stores the indices of all four lettered word of L.
For example:
If L contains [“DINESH”, “RAMESH”, “AMAN”, “SURESH”, “KARN”]
The indexList will have [2, 4]
8. Write the output of the code given below:
def printMe(q,r=2):
p=r+q**3
print(p)
#main-code
a=10
b=5
printMe(a,b)
printMe(r=4,q=2)
9. We can pass the argument in the function call in any order using…
a. Keyword argument b. Variable Length argument
c. No argument d. D. default argument
10. Which of the arguments can be skipped in the function call?
a. Positional arguments b. Default arguments
c. Keyword arguments d. No arguments
11. Sameer has written a python function to compute the reverse of a number.
He has however committed a few errors in his code. Rewrite the code after
removing errors also underline the corrections made.
define reverse(num):
rev = 0
While num > 0:
rem == num %10
rev = rev*10 + rem
num = num//10
return rev
print(reverse(1234))
12. Predict the output of the following python code:
def foo(s1,s2):
l1=[]
l2=[]
for x in s1:
l1.append(x)
for x in s2:
l2.append(x)
return l1,l2
a,b=foo("FUN",'DAY')
print(a,b)
13. Write a function modilst(L) that accepts a list of numbers as argument and
increases the value of the elements by 10 if the elements are divisible by 5.
Also write a proper call statement for the function.
For example:
If list L contains [3,5,10,12,15]
Then the modilist() should make the list L as [3,15,20,12,25]
18. (c)
19. Riya was asked to accept a list of even numbers ,but she did not put the
relevant condition while accepting the list of numbers. She wrote a user
defined function odd to even (L) that accepts the list L as an argument and
converts all the odd numbers into even by multiplying them by 2.
def oddtoeven (L)
for i in range (size(L)):
if (L[i]%2! == 0)
L[i]= L[1] ** 2
print (L)
There are some errors in the code. Rewrite the correct code.
20. Write a user defined function in python which taken an integer n as
argument and return the count of all numbers with unique digits, say x,
where 0 <= x < 10n.
For Example:
If the Input n=2
Then the output should be 91
Explanation: The answer should be the total numbers in the range
0<=x<100, excluding 11,22,33,44,55,66,77,88,99
21. Write the definition of a function Reverse (x) in Python, to display the
elements in reverse order such that each displayed element is the twice of
the original element (element *2) of the List x in the following manner:
Example : If List x contains 7 integers is as follows:
x [0] x [1] x [2] x [3] x [4] x [5] x [6]
4 8 7 5 6 2 10
After executing the function, the array content should be displayed as follows:
x [0] x [1] x [2] x [3] x [4] x [5] x [6]
20 4 12 10 14 16 8
22. Carefully observe the following Python code and answer the question that
follow:
x=5
def func2():
global x
x=x+1
print(x)
x=3
func2()
print(x)
On execution the above code, produces the following output:
6
3
Explain the output with respect to the scope of the variables.
30.