What will be the output of the following Python code? [code] def sum(*args): '''Function returns the sum of all values''' r =

Nguyễn Hào

New member
What will be the output of the following Python code?
Code:
def sum(*args):
   '''Function returns the sum 
   of all values'''
   r = 0
   for i in args:
      r += i
   return r
print sum.__doc__
print sum(1, 2, 3)
print sum(1, 2, 3, 4, 5)
A.
6
15
B.
6
100
C.
123
12345
D. None of the mentioned
 
Answer: a
Explanation: We use the * operator to indicate, that the function will accept arbitrary number of arguments. The sum() function will return the sum of all arguments. The first string in the function body is called the function documentation string. It is used to document the function. The string must be in triple quotes.
 

Tìm kiếm

Back
Top