Robotics & Artificial Intelligence
Predict the output of the following code:
def Add(num1, num2):
sum = num1 + num2
sum = Add(20, 30)
print(sum)
Answer
Output
None
Explanation
The function Add() is called with the values 20 and 30. Inside the function, the statement sum = num1 + num2 calculates the addition and stores the result in the local variable sum. However, the function does not contain a return statement to send the value back to the main program. Therefore, Python automatically returns None. In the main program, the same variable name sum is used as sum = Add(20, 30), so the value None gets stored in it. Hence, the print(sum) statement displays None.
Related Questions
Predict the data type of the given Python statement:
p='10' print(type(int(p)))Predict the data type of the given Python statement:
n=0.0025 print(type(n))Predict the output of the following code:
def Sum(a, b): return a+5, b*5 # main program result = Sum(4, 5) print(result)Predict the output of the following code:
def Disp_Max(a, b): if a > b: print(a, 'is maximum') elif a == b: print(a, 'is equal to', b) else: print(b, 'is maximum') Disp_Max(22, 14)