Python Reference Library →

Python sum() Function – Add Items of an Iterable

Let’s look at sum() function – add items of an iterable

  1. Notice

    sum() adds values from an iterable, with an optional starting value. The empty iterable returns that starting value.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
numbers = range(3)
print(sum(numbers, 10))
Live state

Top-level scope

numbers→Object 1
Object 1 · rangerange(0, 3)
This step

sum() adds values from an iterable, with an optional starting value. The empty iterable returns that starting value.

Picture sum() function – add items of an iterable

sum() adds values from an iterable, with an optional starting value. The empty iterable returns that starting value.

Image brief

Alt text
A diagram for Python sum() Function – Add Items of an Iterable. Instructions: numbers = range(3) print(sum(numbers, 10)). Displayed result: 13. The final example bindings are numbers --> object 1 (range). object 1: range(0, 3)
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python sum() Function – Add Items of an Iterable". Teaching focus: sum() adds values from an iterable, with an optional starting value. The empty iterable returns that starting value. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS numbers = range(3) print(sum(numbers, 10)) | v DISPLAYED RESULT 13 FINAL BINDINGS numbers --> object 1 (range) object 1: range(0, 3) Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python sum() Function – Add Items of an Iterable. Instructions: numbers = range(3) print(sum(numbers, 10)). Displayed result: 13. The final example bindings are numbers --> object 1 (range). object 1: range(0, 3)

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
numbers = range(3)
print(sum(numbers, 10))
Live state

Top-level scope

numbers→Object 1
Object 1 · rangerange(0, 3)
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
13

What do you expect this program to print?

What does this example display?

numbers = range(3)
print(sum(numbers, 10))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

13

Follow the printed values in order. The output is: 13

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

sum() adds values from an iterable, with an optional starting value. The empty iterable returns that starting value.

sum() adds values from an iterable, with an optional starting value. The empty iterable returns that starting value.

More examples and reference

Python sum() Function

The sum() function in Python returns the total (sum) of all items in an iterable, such as a list or tuple. It’s one of the most frequently used functions in real-world Python programs.

Syntax

sum(iterable, start=0)

Parameters:

  • iterable – A sequence like a list, tuple, or set containing numeric values.
  • start – (Optional) The value to start with. Default is 0.

Returns:

  • The sum of all items in the iterable plus the start value.

Example 1: Sum of a List

numbers = [10, 20, 30]
print(sum(numbers))
60

Example 2: Sum with Start Value

numbers = [1, 2, 3]
print(sum(numbers, 10))
16

Why? Because 1 + 2 + 3 = 6 and 6 + 10 = 16

Example 3: Sum of a Tuple

values = (5, 15, 25)
print(sum(values))
45

Use Case: Total Bill Calculation

items = [120.5, 250.0, 89.99]
total = sum(items)
print("Total Bill:", total)
Total Bill: 460.49

Common Mistakes

  • Using sum() with non-numeric values like strings or dictionaries will raise a TypeError.
  • Don’t use it for concatenating strings — use ''.join() instead.

Interview Tip

sum() is commonly used in problems that ask for totals, averages, or when working with filter() and map().

Summary

  • sum() adds all elements in an iterable.
  • You can use an optional start value.
  • Only use it with numbers; using it with strings or mixed types causes errors.

Practice Problem

Write a program that asks the user for 5 numbers and prints their sum.

numbers = []
for i in range(5):
    num = float(input("Enter number " + str(i+1) + ": "))
    numbers.append(num)

print("Sum =", sum(numbers))

What to remember

Connect what you noticed in the example with the idea below.

Remember

sum() adds values from an iterable, with an optional starting value. The empty iterable returns that starting value.

Watch out for this idea

sum([], 10) returns zero.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →