• Python

Command Palette

Search for a command to run...

Native Functions
  • Python Abs Function
  • Python Any All Functions
  • Python Zip Function
  • Python Sum Function
  • Python Filter Function
  • Python Max Min Functions
  • Python Map Function
  • Python Round Function
  • Python Sorted Function
Data Types
  • Python Integers
  • Python Floats
  • Python Complex Numbers
  • Python List Type
  • Python Tuple Type
  • Python String Type
  • Python Range Type
Collections.abc Module
  • Python Containers
  • Python Hashable
  • Python Iterable
  • Python Iterators
  • Python Sequence
  • Python Mutable Sequence

Create an Account

FREE

Join our community to access more courses.

Create Account

On this page

What does sum() do?Basic SyntaxExamples with lists and tuples of numbersUsing the start parameterSumming with different types (caution)Summary
      • Blog

      Python Sum Function

      What does sum() do?

      The sum() function adds up all the items in an iterable (like a list or tuple) and returns the total. You can also provide a starting value.

      Basic Syntax

      sum(iterable, /, start=0)
      
      • iterable: The sequence of numbers to add.
      • start: (Optional) The value to start with (default is 0).

      Examples with lists and tuples of numbers

      numbers = [1, 2, 3, 4]
      print(sum(numbers))  # Output: 10
      
      tuple_numbers = (5, 5, 5)
      print(sum(tuple_numbers))  # Output: 15
      

      Using the start parameter

      numbers = [1, 2, 3]
      print(sum(numbers, 10))  # Output: 16 (10 + 1 + 2 + 3)
      

      Summing with different types (caution)

      • sum() works with numbers (int, float, complex).
      • Do not use with non-numeric types (e.g., strings, lists) or mixed types.
      # sum(["a", "b"])  # TypeError!
      

      Summary

      • sum() adds up all items in an iterable, starting from start (default 0).
      • Works with numbers (int, float, complex).
      • Returns the total sum.

      Continue Learning

      Python Variables

      Popular

      Getting Started: Understanding Variables in Python In programming, we often need to store informatio

      Python Tuple

      For You

      Holding Fixed Groups: Understanding Tuples in Python In the previous article, we learned about Lists

      Python List

      For You

      Python Lists: A Complete Introduction Lists are one of the most versatile and commonly used data typ

      Personalized Recommendations

      Log in to get more relevant recommendations based on your reading history.