PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 8, 2022

[FIXED] Why I need to add an extra parenthesis for sum() function?

 December 08, 2022     python, python-3.x, syntax     No comments   

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.

Check this

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing