Worksheet 2
Worksheet 2
Worksheet 2
Holiday homework
Worksheet
Questions
1) Define function ?
2) What is the difference between local variable and global variable ?
3) What is an argument? Give an example.
4) What is Python module? What is its significance?
5) What is the utility of: -
print (x + y + z)
12). Trace the following code and predict the output produced by it
2. y = b ** P
3. return y
4.
6. a = power (x, 2)
7. return a
8.
9. n = 5
(i)
def increment(n):
n. append([4])
return n
L = [1, 2, 3]
M = increment(L)
print(L, M)
(ii)
def increment(n):
n. append([49])
print(L)
print(L[3] == m4)
14) Q .In the following code, which variables are in the same scope?
def func1():
a=1
b=2
def func2():
c=3
d=4
e=5
15) Q. From the program code given below, identify the parts mentioned below:
def processNumber(x):
x = 72
return x + 3
y = 54
Identify these parts: function header, function call, arguments, parameters, function body, main program
print (x + y + z)
return x * y * z
b = prod (2, 3, 6)
print(a, b)
Why did Python not report an error when void functions do not return a value?
17) Q. Consider below given function headers. Identify which of these will cause error and why?
18) Q. Following code intends to add a given value to global variable a. What will the following code
produce ?
def increase(x): #1
a = a + x #2
return #3
#4
a = 20 #5
b = 5 #6
increase(b) #7
print(a) #8
19) Q. Find and write the output of the following python code:
a = 10
def call():
global a
a = 15
b = 20
print(a)
call()
20) Q. Find and write the output of the following python code:
P=P+Q
Q=P-Q
return (P)
R = 150
S = 100
R = Change (R, S)
S = Change (S)
Solutions
Global variable: -
• It is variable which is declared outside all the functions.
• It is accessible throughout the program.
Ans4) A "module" is a chunk of Python code that exists in its own (.py) file and is
intended to be used by Python code outside itself.
Modules allow one to bundle together code in a form in which it can easily be
used later. The Modules can be "imported" in other programs so the functions
and other definitions in imported modules become available to code that
imports them.
Ans5) (i)
A parameter having a default value in function header becomes optional in
function call. Functions call may or may not have value for it.
(ii)
Keyword arguments are the named arguments with assigned values being
passed in the function call statement.
Ans6) Python's built-in function help() is very useful. When it is provided with a
program name or a module-name or a function-name as an argument, it displays
the documentation of the argument as help. It also displays the doc-string within
its passed-argument's definition.
For example:
For example:
>>>help (math.sqrt())
• The above code will list the documentation of math.sqrt() function only.
Ans7) The flow of execution refers to the order in which statement are executed
during a program run.
Ans8)
def great(a , b) :
If b == 0 :
return a
else :
return great (b, a % b)
Output :-
Enter the first number :- 257
Enter the second number :- 638
Greatest common divisor of 257 & 638 is 1
>>>
Ans9)
Advantage: -
• Recursion makes the code short and simple.
Ans13)
(i)
def increment(n):
n. append([4])
return n
L = [1, 2, 3]
M = increment(L)
print(L, M)
(ii)
def increment(n):
n. append([49])
return n[0], n[1], n[2], n[3]
L = [23, 35, 47]
mi, m2, m3, m4 = increment (L)
print(L)
print(mi, m2, m3, m4)
print(L[3] == m4)
(i)
Output:-
(ii)
Output: -
Arguments :- y
Parameters :- x
Function body :-
x = 72
return x + 3
Main program :-
y = 54
res = processNumber (y)
Ans16) Output :-
48
None 36
>>>
Reason :-
In Python, void functions do not return a value; rather they report the absence of
returning value by returning None, which is legal empty value in Python. Thus,
variable a stores None and it is not any error
Ans17) Function headers (i), (ii) and (iv) will cause error because non-default
arguments cannot follow default arguments.
Only function header (ii) will not cause any error.
Thus in line 2, when variable a is incremented with the passed value x, Python
tries to create a local variable a by assigning to it the value of the expression on
the right-hand side of the assignment. But variable a also appears on the right-
hand side of the assignment, which results in an error because a is undeclared
so far in function.
To assign some value to a global variable from within a function, without creating
a local variable with the same name, the global statement can be used. So, if we
add
global a
In the first line of the function body, the above error will be corrected. Python
won't create a local variable a, rather will work with global variable a.
Ans19) Output :-
15
Ans20) Output :-
250 # 150
250 # 100
130 # 100