Issue
if sum()
is a function in python, why does it need an extra parenthesis to work? Like other functions why sum()
is not working with single parenthesis?
It's not working when it's like this:
num1 = int(input())
num2 = int(input())
total = sum(num1, num2)
print(total)
It is working fine when I'm adding an extra parenthesis. Why this is happening?
total = sum((num1, num2))
Solution
The sum
function in python takes in a tuple
or a list
of numbers as the first parameter ( because it should be iterable) and returns the sum of the elements.
sum ( num1, num2,...) #Gives Type Error since int is non iterable
sum ((num1,num2,...)) #works
sum([num1,num2,...] #works
Answered By - vvk24 Answer Checked By - David Goodson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.