Functions - Worksheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

WORKING WITH FUNCTIONS

WORKSHEET – 2
1. Consider the code given below:

Which of the following statements should be given in the blank


for #Missing Statement, if the output produced is 110?
a. global a
b. global b=100
c. global b
d. global a=100
2. Assertion(A): Python standard library consists of number of
modules.
Reasoning(R): A function in a module is used to simplify the code
and avoids repetition.
3. Assertion (A) : To use a function from a particular module, we
need to import the module.
Reason (R) : import statement can be written anywhere in the
program, before using a function from that module.
4. Assertion (A):- If the arguments in function call statement match
the number and order of arguments as defined in the function
definition, such arguments are called positional arguments.
Reasoning (R):- During a function call, the argument list first
contains default argument(s) followed by positional argument(s).
5. Assertion (A): For changes made to a variable defined within a
function to be visible outside the function, it should be declared
as global.
Reasoning (R): Variables defined within a function are local to
that function by default, unless explicitly specified with the global
keyword.
6. The code given below accepts a number as an argument and
returns the reverse number. Observe the following code carefully
and rewrite it after removing all syntax and logical errors.
Underline all the corrections made.

7. Rao has written a code to input a number and check whether it is


prime or not. His code is having errors. Rewrite the correct code
and underline the corrections made.
def prime():
n=int(input("Enter number to check :: ")
for i in range (2, n//2):
if n%i=0:
print("Number is not prime \n")
break
else:
print("Number is prime \n’)
8. Atharva is a Python programmer working on a program to find
and return the maximum value from the list. The code written
below has syntactical errors. Rewrite the correct code and
underline the corrections made.
def max num (L) :
max=L(0)
for a in L:
if a> max
max=a
return max
9. Predict the output:
def Call(P=40,Q=20):
P=P+Q
Q=P–Q
print(P,'@',Q)
return P
R=200
S=100
R=Call(R,S)
print (R,'@',S)
S=Call(S)
print(R,'@',S)
10. Predict the output:

11. def makenew(mystr):


newstr=""
count=0
for i in mystr:
if count%2!=0:
newstr=newstr+str(count)

else:
if i.islower():
newstr=newstr+i.upper()
else:
newstr=newstr+i
count+=1
print(newstr)
makenew("No@l")
12. Predict the output:
def Update(X=10):
X += 15
print('X = ', X)
X=20
Update()
print('X = ', X)
13. Write a function dispBook(BOOKS) in Python, that takes a
dictionary BOOKS as an argument and displays the names in
uppercase of those books whose name starts with a consonant.
For example, Consider the following dictionary
BOOKS = {1:"Python", 2:"Internet Fundamentals ", 3:"Networking ",
4:"Oracle sets", 5:"Understanding HTML"}
The output should be:
PYTHON
NETWORKING
14. Write a Python Program containing a function FindWord(STRING,
SEARCH), that accepts two arguments : STRING and SEARCH, and
prints the count of occurrence of SEARCH in STRING. Write
appropriate statements to call the function.
For example, if STRING = "Learning history helps to know about
history with interest in history" and SEARCH = 'history', the
function should display ‘The word history occurs 3 times’.
15. Write a function EOReplace() in Python, which
accepts a list L of numbers. Thereafter, it increments
all even numbers by 1 and decrements all odd numbers
by 1.
Example:
If Sample Input data of the list is :
L=[10,20,30,40,35,55]
Output will be :
L=[11,21,31,41,34,54]
16. Write a function INDEX_LIST(L), where L is the list of elements
passed as argument to the function. The function returns another
list named ‘indexList’ that stores the indices of all Non-Zero
Elements of L.
For example:
If L contains [12,4,0,11,0,56]
The indexList will have - [0,1,3,5]

You might also like