Here is class 12 computer science Unit 3 [Type B] solutions for Sumita Arora back exercise assignment. Below includes both textual and video solutions wherever required. View all the answers in assignment for chapter 3 and for all chapters here.Watch all tutorials for chapter 3: Python Revision Tour.
Indentation and invalid keyword selection that means syntax error is present in the both cases. (a) Corrected Code total =0 def sum(arg1, arg2): total = arg1+arg2 print("Total :",total) return total sum(10,20) print("Total :",total) Output: Total : 30 Total : 0 (b) Corrected Code def Tot(Number): Sum = 0 for C in range(1, Number+1): Sum+=C return Sum print(Tot(3)) print(Tot(6)) Output: 6 21
Firstly the line 9 will execute and a variable with value 5 will be initialized. After that in line 10 calcSquare(n) will be called and the respective function will execute means Line 5 will be called. Then on moving line 6 power function will call and the flow jump to line 1 and result of power function stored in variable a. Then flow will jump to the line 6 and then 7 and then to line 10 to initialize the result variable. Then, the Line 11 will be executed to provide output. Flow cycle is 9 → 10 → 5 → 6 → 1 → 2 → 3 → 7 → 10 → 11
def addEm(x, y, z): print(x+y+z)
Solution : This function will return nothing as of no return keyword.
def addEm(x, y, z): return (x+y+z) print(x+y+z)
Solution: This function will print depend on case: If x, y, z will be numeric value than a numeric value will be printed. If string will be passed as argument than a string concatenation will be result.
(a) 1 1 1 (b) 1 10 1 (c) 1 10 10 (d) Hello there!
a =10 y = 5 def myfunc(): y=a a=2 print("y=", y, "a=", a) print("a+y=", a+y) return a+y print("y=", y, "a=", a) print(myfunc()) print("y=", y, "a=", a)
Output: y= 5 a= 10 UnboundLocalError: local variable 'a' referenced before assignment Note: The unbound error occur because during call of myfunc() local variable is used before initializing the same in local scope.
def addEm(x,y,z): return x+y+z print("the answer is", x+y+z)
The print command is initialized after return command and return command is termination of any function call. Hence, the print() command will never run in such call.
def fun(): return None
def multiply(number1, number2): answer = number1*number2 print(number1, 'times', number2, "=", answer) return(answer) output = multiply(5,5) (i) When the code above is executed, what prints out? (ii) What is variable output equal to after the code is executed?
return(answer)
(i) Output: 5 times 5 = 25 (ii) output = 25
def multiply(number1, number2): answer = number1*number2 return(answer) print(number1, 'times', number2, "=", answer) output = multiply(5,5) (i) When the code above is executed, what prints out? (ii) What is variable output equal to after the code is executed?
(i) Output: Nothing (ii) output = 25
(a)Correct code: def minus(total, decrement): # semicolon was not given output = total-decrement print(output) return output (b) Correct code: def check(): # define → def and semicolon absent N =input('Enter N:') i =3 answer =1+i**4/N return answer # Return → return (c) Correct code def alpha(n, string='xyz', k=10): return beta(string) return n def beta(string): # Semicolon absent return string == str(n) print(alpha("Valentine Day")) # wrong semicolon present print(beta(string='true')) print(alpha(n=5, "Good bye")) # wrong semicolon present
1. def sum(a, b, c, d): 2. result = 0 3. result =result+a+b+c+d 4. return result 5. 6. def length(): 7. return 4 8. 9. def mean(a, b, c, d): 10. return float(sum(a, b, c, d))/length() 11. 12. print(sum(a, b, c, d), length(), mean(a, b, c, d))
Solution:
Till line 10 is called means a local environment being created and all the three function sum(), length(), mean() being called as by line 12 respectively. Which shows that from the sum() variable a, b, c, d will globally access by function and acted upon local variable result and returned. In case of length() no variable acceptance and declaration will be done. In case of mean() variable a, b, c, d will globally access by function and calculated portion will return.
12 → 1 → 2 → 3 → 4 → 12 → 6 → 7 → 12 → 9 → 10
def func1(): a=1 b=2 def func2(): c=3 d=4 e=5
Variable a and b having same local scope for function func1(). Variable c and d having the same local scope for function func2().
def follow_num(n): return n+1 follow_num(4) follow_num(6) follow_num(8) follow_num(2+1) follow_num(4-3*2) follow_num(-3-2) Output: 5 7 9 4 -1 -4
# code 1. def follow_num(n): 2. return n+1 #non-void code 3. def follow_num_non(n): 4. print(n+1) 5. return n+1 6. 7. follow_num(5) 8. follow_num_non(4) Flow control 7 → 1 → 2 → 8 → 3 → 4 → 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]) return n[0], n[1], n[2], n[3] L = [23,35,47] m1, m2, m3, m4 =increment(L) print(L) print(m1, m2, m3, m4) print(L[3] == m4)
output: (i) [1, 2, 3, [4]] [1, 2, 3, [4]] (ii) [23, 35, 47, [49]] 23 35 47 [49] True
Clear Doubts with Computer TutorIn case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in
You cannot copy content of this page