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

function_wk

The document is a worksheet containing various questions related to Python functions, including built-in functions, function definitions, argument types, and scope. It includes both short answer and application-based questions that require coding examples and explanations. Additionally, it addresses common errors in function definitions and asks for corrections and predictions of code outputs.

Uploaded by

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

function_wk

The document is a worksheet containing various questions related to Python functions, including built-in functions, function definitions, argument types, and scope. It includes both short answer and application-based questions that require coding examples and explanations. Additionally, it addresses common errors in function definitions and asks for corrections and predictions of code outputs.

Uploaded by

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

FUNCTIONS

WORKSHEET

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)

11.Define a function?

12.Write a python function that takes two numbers and find their product.

Short Answer Type Questions (2-Marks)

1. What is the difference between a Local Scope and Global Scope ? Also, give a suitable Python code to illustrate
both.

2. Define different types of formal arguments in Python, with example.


3. Observe the following Python code very carefully and rewrite it after removing all syntactical errors with eac
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 ( )
4. Differentiate between Positional Argument and Default Argument of function in python with suitable example

5. 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.

6. 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")

7. What is the meaning of return value of a function? Give an example to illustrate its meaning.

8. Differentiate between call by value and call by reference with a suitable example for each.

9. Find and write the output of the following Python code:


def Show(str):
m=""
for i in range(0,len(str)):
if(str[i].isupper()):
m=m+str[i].lower()
elif str[i].islower():
m=m+str[i].upper()
else:
if i%2==0:
m=m+str[i-1]
else:
m=m+"#"
print(m)
Show('HappyBirthday')

10. 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 (3 Marks)

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]

2. Write a function LShift(Arr,n) in Python, which accepts a list Arr of numbers and
n is a numeric value by which all elements of the list are shifted to left. Sample
Input Data of the list
Arr= [ 10,20,30,40,12,11], n=2
Output
Arr = [30,40,12,11,10,20]

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

______________________________________________________________

Q.1) Write a python function that takes two numbers and print the smaller number. Also write how to
call this function.
Q.2) How many values a python function can return? Explain how?

Q.3) Rewrite the correct code after removing the errors: -

def SI(p, t=2, r):

return (p*r*t)/100
Q.4) Write a small python function that receive two numbers and return their sum, product, difference
and multiplication and modulo division.

Q.5) What is scope of a variable?

Q.6) Explain two types of variable scope with example.

Q.7) Consider the following function headers. Identify the correct statement: -

1) def correct(a=1,b=2,c): 2) def correct(a=1,b,c=3):

c) def correct(a=1,b=2,c=3): 4) def correct(a=1,b,c):

Q.8) Find the output of the following code: -

Ans: - def CALLME(n1=1,n2=2):

n1=n1*n2

n2+=2

print(n1,n2)

CALLME()

CALLME(2,1)

CALLME(3)

______________________________________________

Q.1) What is Libraries in Python and How many types of main library use in Python?

Q.2) How we can import library in Python program?

Q.3) Give the basic structure of user defined function.

Q.4) Create a function in Python to calculate and return Area of rectangle when user
enters length and bredth.

Q.5) What is module in Python?

Q.6) Write the features of a module.

Q.7) Create a package Arithmetic Operations(named AO) contain sum, product and
difference of two numbers and use it in your main programme.

You might also like