Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
type() function in Python
Next article icon

sum() function in Python

Last Updated : 02 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list.

Python
arr = [1, 5, 2] print(sum(arr)) 

Output
8 


Sum() Function in Python Syntax

Syntax : sum(iterable, start)

  • iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers.
  • start : this start is added to the sum of  numbers in the iterable. If start is not given in the syntax , it is assumed to be 0.

Possible two more syntaxes

sum(a) : a is the list , it adds up all the numbers in the list a and takes start to be 0, so returning only the sum of the numbers in the list.
sum(a, start) : this returns the sum of the list + start The sum

Python Sum() Function Examples

Get the sum of the list in Python .

Python
numbers = [1,2,3,4,5,1,4,5]  Sum = sum(numbers) print(Sum)  Sum = sum(numbers, 10) print(Sum) 

Output
25 35 

Here below we cover some examples using the sum function with different datatypes in Python to calculate the sum of the data in the given input

  • Sum Function on a Dictionary
  • Sum Function on a Set
  • Sum Function on a Tuple
  • The sum in Python with For Loop
  • Error and Exceptions
  • Practical Application

Python Sum Function on a Dictionary

In this example, we are creating a tuple of 5 numbers and using sum() on the dictionary in Python.

Python
my_dict = {'a': 10, 'b': 20, 'c': 30} total = sum(my_dict.values()) print(total) 

Output
60 

Python Sum Function on a Set

In this example, we are creating a tuple of 5 numbers and using sum() on the set in Python.

Python
my_set = {1, 2, 3, 4, 5} total = sum(my_set) print(total)  

Output
15 

Python Sum Function on a Tuple

In this example, we are creating a tuple of 5 numbers and using sum() on the tuple in Python.

Python
my_tuple = (1, 2, 3, 4, 5) total = sum(my_tuple) print(total)  

Output
15 

The sum in Python with For Loop

In this, the code first defines a list of numbers. It then initializes a variable called total to 0. The code then iterates through the list using a for loop, and for each number in the list, it adds that number to the total variable. Finally, the code prints the total value, which is the sum of the numbers in the list.

Python
# Define a list of numbers numbers = [10, 20, 30, 40, 50]  # Initialize a variable to store the sum total = 0  # Iterate through the list and add each number to the total for num in numbers:     total += num  # Print the sum of the numbers print("The sum of the numbers is:", total) 

Output
The sum of the numbers is: 150 

Error and Exceptions

TypeError : This error is raised when there is anything other than numbers in the list . In the given example we are using a list of strings rather than an integer.

Python
arr = ["a"]  # start parameter is not provided Sum = sum(arr) print(Sum)  # start = 10 Sum = sum(arr, 10) print(Sum) 

Output :

Traceback (most recent call last):
File "/home/23f0f6c9e022aa96d6c560a7eb4cf387.py", line 6, in
Sum = sum(arr)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Practical Application

Problems where we require the sum to be calculated to do further operations such as finding out the average of numbers.

Python
numbers = [1,2,3,4,5,1,4,5]  # start = 10 Sum = sum(numbers) average= Sum/len(numbers)  print (average) 

Output
3.125 


Next Article
type() function in Python

S

Striver
Improve
Article Tags :
  • Python
  • python
  • Python-Built-in-functions
Practice Tags :
  • python
  • python

Similar Reads

  • memoryview() in Python
    The memoryview() function in Python is used to create a memory view object that allows us to access and manipulate the internal data of an object without copying it. This is particularly useful for handling large datasets efficiently because it avoids the overhead of copying data. A memory view obje
    5 min read
  • Python min() Function
    Python min() function returns the smallest value from a set of values or the smallest item in an iterable passed as its parameter. It's useful when you need to quickly determine the minimum value from a group of numbers or objects. For example: [GFGTABS] Python a = [23,25,65,21,98] print(min(a)) b =
    4 min read
  • Python next() method
    Python's next() function returns the next item of an iterator. Example Let us see a few examples to see how the next() method in Python works. C/C++ Code l_iter = iter(l) print(next(l_iter)) Output1 Note: The .next() method was a method for iterating over a sequence in Python 2. It has been replaced
    4 min read
  • Python oct() Function
    Python oct() function takes an integer and returns the octal representation in a string format. In this article, we will see how we can convert an integer to an octal in Python. Python oct() Function SyntaxSyntax : oct(x) Parameters: x - Must be an integer number and can be in either binary, decimal
    2 min read
  • ord() function in Python
    Python ord() function returns the Unicode code of a given single character. It is a modern encoding standard that aims to represent every character in every language. Unicode includes: ASCII characters (first 128 code points)Emojis, currency symbols, accented characters, etc.For example, unicode of
    2 min read
  • pow() Function - Python
    pow() function in Python is a built-in tool that calculates one number raised to the power of another. It also has an optional third part that gives the remainder when dividing the result. Example: [GFGTABS] Python print(pow(3,2)) [/GFGTABS]Output9 Explanation: pow(3, 2) calculates 32 = 9, where the
    2 min read
  • Python print() function
    The python print() function as the name suggests is used to print a python object(s) in Python as standard output. Syntax: print(object(s), sep, end, file, flush) Parameters: Object(s): It can be any python object(s) like string, list, tuple, etc. But before printing all objects get converted into s
    2 min read
  • Python range() function
    The Python range() function returns a sequence of numbers, in a given range. The most common use of it is to iterate sequences on a sequence of numbers using Python loops. Example In the given example, we are printing the number from 0 to 4. [GFGTABS] Python for i in range(5): print(i, end="
    7 min read
  • Python reversed() Method
    reversed() function in Python lets us go through a sequence like a list, tuple or string in reverse order without making a new copy. Instead of storing the reversed sequence, it gives us an iterator that yields elements one by one, saving memory. Example: [GFGTABS] Python a = ["nano",
    4 min read
  • round() function in Python
    Python round() function is a built-in function available with Python. It will return you a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer. In this
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences