Question

Consider the following Python program: def fun(x, y): return x + y # [2] # [1] a = fun(2, 3) b = fun(2, 3) print a, b WhatNac Mina Mert - O midtermoview.coe [Compatibility Mode] Worci Review View Help O tel me what you want to do X Share file Home

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the explanation of how all the above functions works along with the screenshots of code and output.

1. def fun(X,Y):

return X*Y

a = fun(2,3)

b = fun("2",3)

print(a , b)

Explanation :

- 1. def fon (x, y): return xxy a : fon (2,3) be fun (2,3) print (a,b) Solution : 16 222 How ? 1. When the program starts

2. After a = ton (2,3), the interpreter goes to b = (23) string int 2 3 Now fon (2, 3) will call def for (x, y) ton (2,

3. The cursor moves to print (a,b). As the two variables are seperated by () comma, they will be printed as it is. Output : 6

SCREENSHOT(CODE AND OUTPUT)

When you run the above code the output looks like below.

1 def fun (X,Y): return X*Y s L a = fun (2, 3) 6 b = fun(2,3) P CÓ print (a, b) 8 9 10 Shell >>> %Run Python1.py 6 222 >>>|

1(b) . If we change print(a , b) to print(a + b) , then it will show an error because

The output of fun(2,3) which is in a is of int type ( a = 6)

The output of fun("2",3) which is in b is of string type.(b = "222")

When we try to print a + b as string cannot be concatenated with int directly it will show an error

SCREENSHOT

def fun (X,Y): return X*Y a = fun(2, 3) HNM to 00 b = fun(2,3) print (a + b) 10 Shell >>> %Run Python1.py Traceback (most r

1(c) If we remove a = fun(2,3) and change return X*Y to return X + Y it will also generate an error . To understand this see the below trace back.

1.c) def fun (x, y) return x + y d ba: fon(2, 3) print (a + b) solution ; Error. 1. The program starts at Now, fon (2, 3

because, X = 2 is of string type, Y = % is of integer type So, x + y is not possible as string came be concatenated directl

SCREENSHOT

def fun(X,Y): return X + Y Homto b = fun(2,3) 8 print (a + b) 10 Shell >>> %Run Python1.py Traceback (most recent call last

2(a) The explanation in the form of stack for 2 (a ) is as follows

☺ det fun (nm): return mo a) fun ( fun (1, 2), 3) When the interpreter goes to ton ( fun (1, 2), 3) it will evaluate inner f:: So, finally the output of fon ( fun (1,2), 3) will be 21. This flow of execution takes place for every function ie; (b, c)

For 2(b) & 2(c)

Explanation

26 fun (fun (1,2), funt 3, fun (4) fur (5,6)), - ))) The execution flow of this will also be as same as above with some diffe

= 1 m n + 6-5 m D * 1 - 4 ( 8 o foo( 4 m ) = -3 a fun (-3,7) →7-(-3) = 10 0 ton ( 3 , 10) = 10-3 = 7 = 6 + Final result. w fu

om Mo The 7 will have fun ( 7,8) = 8-7. → tunaton ( 4, tun (5, 6)), ton (7,8 )) funcs,6) - 6-5 = 1 9 → tun (4,1) = 1-4 -3 io

SCREENSHOT

def fun(n,m): return m - n Hem tin print(fun(fun(1,2),3) = {0}.format(fun(fun(1,2),3))) 6 print(fun(fun(1,2), fun(3, fun(f

For third one the output and explanations are as below.

113) fun Cicl) 1-1=0 Final rewlt. ☺ def alpha (x,y): I retun x + beta (x,x) def beta (x,y) : returo y - x. of execution For a2,3) step - step - step - det alpha (x,y) *= 2, Y = 3 x + beta (yp, x) 2 + beta (3,216 beta (3,2) Ly return 2-3 =(-1) step -

SCREENSHOT

def alpha(X, Y): return X + beta(Y,X) Humin def beta(X, Y): return Y - X 7 print(alpha(2,3) Shell >>> %Run Python3.py 1 >>>|

After changing return Y - X to X - Y

def alpha(X, Y): return X + beta(Y,X) o u AwNN def beta(X, Y): return X - Y 7 print(alpha(2,3) Shell >>> %Run Python3.py 3 >>

Add a comment
Know the answer?
Add Answer to:
Consider the following Python program: def fun(x, y): return x + y # [2] # [1]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT