Skip to content

Write a Python program to sum all the items in a list | Example code

  • by

If the given list has a number then use the sum() function to sum all the items in a list. You can write your own Python program to sum all the items in a list using a while loop or for loop with a + operator.

Python program to sum all the items in a list

Simple example code to add all the numbers using python’s sum() function

a = [4, 5, 89, 5, 33, 2244, 56]
a_total = sum(a)

print("Sum:", a_total)

Output:

Write a Python program to sum all the items in a list

Using for loop

items = [4, 5, 89, 5, 33, 2244, 56]
sum_numbers = 0

for x in items:
    sum_numbers += x

print(sum_numbers)

Output: 2436

Using while() loop

items = [4, 5, 89, 5, 56]
total = 0
ele = 0

while ele < len(items):
    total = total + items[ele]
    ele += 1

print(total)

Output: 159

Do comment if you have any doubts or suggestions on this Python list topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading