• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Calculate Sum of List of Numbers

Using Python to Calculate Sum of List of Numbers

August 19, 2022 Leave a Comment

To calculate the sum of a list of numbers in Python, the easiest way is with the sum() function.

l = [1,2,3,4,5,6,7]

print(sum(l))

#Output:
28

You can also use a for loop to sum the numbers of a list.

l = [1,2,3,4,5,6,7]

s = 0

for num in l:
    s = s + num

print(s)

#Output:
28

When working with collections of data in Python, the ability to summarize the data easily is valuable.

One such case is if you want to get the sum of a list of numbers.

To calculate the sum of a list of numbers in Python, the easiest way is with the sum() function.

sum() returns the sum of a list of numbers.

Below shows you how to get the sum of a list of numbers in Python with sum().

l = [1,2,3,4,5,6,7]

print(sum(l))

#Output:
28

Using For Loop to Get Sum of List in Python

Another way you can add numbers of a list together is with a for loop.

To add the numbers of a list up, initialize a variable which will keep the running sum and then add each element to the running sum.

Below shows you how to get the sum of a list of numbers in Python with a for loop.

l = [1,2,3,4,5,6,7]

s = 0

for num in l:
    s = s + num

print(s)

#Output:
28

Hopefully this article has been useful for you to learn how to get the sum of a list in Python.

Other Articles You'll Also Like:

  • 1.  How to Clear Turtle Screen in Python with clear() Function
  • 2.  Sum Columns Dynamically with pandas in Python
  • 3.  Print Multiple Variables in Python
  • 4.  Python Get Yesterday’s Date
  • 5.  Python Negative Infinity – How to Use Negative Infinity in Python
  • 6.  Not Equal Operator != in Python
  • 7.  How to Iterate Through Lines in File with Python
  • 8.  Count Number of Files in Directory with Python
  • 9.  rfind Python – Find Last Occurrence of Substring in String
  • 10.  Get Substring from String in Python with String Slicing

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy