0% found this document useful (0 votes)
84 views7 pages

Assignment 4 (SOLUTION)

The document contains a series of programming assignments and solutions related to functions in Python. It includes code snippets, expected outputs, flow of execution, and identifies errors in function calls and definitions. Additionally, it discusses variable scopes, local vs global variables, and built-in functions.

Uploaded by

Sarthak Gupta Sg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views7 pages

Assignment 4 (SOLUTION)

The document contains a series of programming assignments and solutions related to functions in Python. It includes code snippets, expected outputs, flow of execution, and identifies errors in function calls and definitions. Additionally, it discusses variable scopes, local vs global variables, and built-in functions.

Uploaded by

Sarthak Gupta Sg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ASSINGNMENT NO 4

SOLUTION
ON

FUNCTION

BASED ON LECTURES
FUNCTION (1, 2, 3, 4, 5)

CHECK AND CORRECT IN COPY

Prepared By-
Mr. Rahul Tiwari
Vidya Devi Jindal School Kosi Kalan
Q1. Write output and Flow Of Execution:
a)
1. def power(b,p): OUTPUT
2. y=b**p
52
3. return y
4. def calcsquare(x): FLOW OF EXECUTION
5. a=power(x,2)
6. return a 1-> 4->7->8->4->5->1->2->3->5->6->8->1->2->3->8->9
7. n=5
8. result=calcsquare(n)+power(3,3)
9. print(result)

b)
1. def increment(x): OUTPUT
2. x=x+1 3
3. print(x) 4
4. x=3 3
5. print(x)
6. increment(x) FLOW OF EXECUTION
7. print(x) 1->4->5->6->1->2->3->7
c)
1. def increment(x):
2. z=45
3. x=x+1
4. return x
5. OUTPUT
6. #main 3
7. y=3 4
8. print(y) 77
9. y=increment(y) 77
10. print(y) ERROR – NAME X IS NOT DEFINED
11. q=77 NAME Z IS NOT DEFINED
12. print(q)
13. increment(q) FLOW OF EXECUTION
14. print(q) 1->7->8->9->1->2->3->4->9->10->11->12->13->1->2->3->4->13->14->15 (ERROR)
15. print(x)
16. print(z)

d)
1. def change():
2. global x OUTPUT GLOBAL ENVIRONMENT
3. x=x+10 6
4. y=45 12 X=6 16 LOCAL ENVIRONMENT CHANGE()
5. print(x)
16 Y=12 Y=45 48
6. y=y+3
7. print(y) 48
8. x=6 16
9. y=12 12
10. print(x) FLOW OF EXECUTION
11. print(y) 1->8->9->10->11->12->1->2->3->4->5->6->7->13->14
12. change()
13. print(x)
14. print(y)
Q2. Consider a function with following header:
def info( object, spacing=10, collapse=1):
Here are some functions call given below find out which of these are correct and which of these are incorrect state
reason:
a) info(obj1)
b) info(spacing=20) incorrect because first should provide value of object(positional) then name example- info(10, spacing=20)
c) info(obj2, 12) incorrect because first should provide value of object(positional) then name example- info(12,obj)
d) info(obj3, object=obj12) can not assign multiple value to object example- info(obj3)
e) info( obj4, collapse=0)
f) info() incorrect value is not given for object example- info(15)
g) info( collapse=0, obj3) named should be appear after positional example- info(obj3,collapse=0)
h) info(spacing=15, object=obj4)
Q3. Consider below given function headers. Identify which of these cause error and why?
a) def func(a=1, b): named should come after positional def func(b,a=1)
b) def func(a=1, b, c=2): named should come after positional def func(b,a=1,c=2)
c) def func(a=1, b=1, c=2):
d) def func(a=1, b=1, c=2, d): named should come after positional def func(d, a=1,b=1, c=2)
Q4. Write output of the following programs:

def switch(x, y): def switch( x, y ):


x, y=y, x x and y already defined as local
print(“Inside switch :”, end=’’) global x, y ERROR( Now can not make global
print(“x=”,x,”y=”,y) x, y=y, x
x=5 print(“Inside switch :”, end=’’)
y=7 x= 5 y= 7 print(“x=”,x,”y=”,y)
print(“x=”,x,”y=”,y) Inside switch :x= 7 y= 5 x=5
switch(x,y) x= 5 y= 7 y=7
print(“x=”,x,”y=”,y) print(“x=”,x,”y=”,y)
switch(x,y)
print(“x=”,x,”y=”,y)

def switch(a, b ):
global x, y Global Environment
x, y=b, a Local environment of switch()
X=5 7
print(“Inside switch :”, end=’’) a=5
print(“x=”,x,”y=”,y) Y=7 5 b=7
x=5
y=7
print(“x=”,x,”y=”,y)
OUTPUT x= 5 y= 7
switch(x,y)
print(“x=”,x,”y=”,y) Inside switch :x= 7 y= 5
OR
def switch( ): x= 7 y= 5
global x, y Global Environment
x, y=y, x
X=5 7
print(“Inside switch :”, end=’’)
print(“x=”,x,”y=”,y) Y=7 5
x=5
y=7 OUTPUT x= 5 y= 7
print(“x=”,x,”y=”,y)
switch( ) Inside switch :x= 7 y= 5
print(“x=”,x,”y=”,y) x= 7 y= 5
Q5. Following code intends to add a given value to global variable a . What will the following code produce?
1. def increase(x): def increase(x):
UnboundLocalError: OUTPUT
2. a=a+x Error global a
25
3. return local variable 'a' a=a+x
return GLOBAL ENVIRONMENT
4. a=20 referenced before a=20
5. b=5 assignment b=5 a=20 25
6. increase(b) increase(b) b=5
7. print(a) print(a)

Q6. Which variable are local, which are global and which are built-in in the following code fragement?

1. invader=’Big names’
2. pos=200 Local Variable-> max_level
3. level=1
4. def play( ): Global Variables-> invader, pos, level, res
5. max_level=level+10
6. print(len(invaders)==0) Built-in -> len( ), print( )
7. return max_level
8. res=play( ) User defined-> play()
9. print(res)

Q7. Predict the output of the following code:


OUTPUT
def func( message, num=1 ):
print(message * num) Python
func(‘Python’)
func( ‘Easy’, 3) EasyEasyEasy
Q8. Write the output of the following code:

a) Output
33
32
53

OUTPUT
b)
1

OUTPUT
c) 610
500
1800
2000
Q9. What are the errors in the following code:

total=0 def tot(number):


def sum(arg1,arg2): Sum=0
total=arg1+arg2 All Correction are in for c in range (1,number+1):
print("Total:",total) lines highlighted Sum+=c All Correction are
return total return Sum in lines
sum(10,20) lines print(tot(3)) highlighted
print("Total:",total) print(tot(6))
lines
Q10. What will be the output produce by the following code:

OUTPUT OUTPUT
1 1
1 10
1 1

OUTPUT OUTPUT
1 Hello there!
10
10
FIRST METHOD a=10 (SECOND METHOD)
a=10 y=5
y=5 def myfunc():
def myfunc(): global a make as global before a=2
a=2 assign first as local before use y=a
y=a a=2
print("y=",y,"a=",a) print("y=",y,"a=",a)
print("a+y=",a+y) print("a+y=",a+y)
ERROR return a+y return a+y
print("y=",y,"a=",a) print("y=",y,"a=",a)
print(myfunc( )) print(myfunc( ))
print("y=",y,"a=",a) print("y=",y,"a=",a)
OUTPUT OUTPUT
y= 5 a= 10 y= 5 a= 10
y= 2 a= 2 y= 10 a= 2
a+y= 4 a+y= 12
4 12
y= 5 a= 10 y= 5 a= 2

def addem( x, y, z):


print(“the answer is”, x+y+z)
return x+y+z return should be at last of function not before any syntax

5 times 5 = 25

25

Error in code
print(number1,’times’,number2,’=’,answer)
return(answer) #return should be at last of function

5 times 5 = 25 after correction


25 after correction

def minus(total,decrement):
output=total-decrement
print(output)
return (output)

def check( ):
N=int(input('Enter N:'))
i=3
answer=1+i**4/N
return answer
Q16.
Answer e(Global scope)

You might also like