• 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 / Calculating Sum of Squares in Python

Calculating Sum of Squares in Python

June 20, 2022 Leave a Comment

To find the sum of the squares of a list of numbers in Python, the easiest way is with a for loop.

def sum_of_squares(lst):
    sum = 0
    for x in lst:
        sum = sum + x ** 2
    return sum

print(sum_of_squares(range(10))) # range(0,1,2,3,4,5,6,7,8,9)
print(sum_of_squares([4,6,2,9,10]))

#Output:
285
237

You can also use sum() and list comprehension to find the sum of squares of a list of numbers in Python.

def sum_of_squares(lst):
    return sum([x ** 2 for x in lst])

print(sum_of_squares(range(10))) # range(0,1,2,3,4,5,6,7,8,9)
print(sum_of_squares([4,6,2,9,10]))

#Output:
285
237

If you want to calculate the sum of squares for the first N numbers, you can use the following formula.

def sum_of_squares_first_n(n):
    return (n * (n + 1) * (2 * n + 1)) // 6

print(sum_of_squares_first_n(5))

#Output:
55

When working with collections of numbers, the ability to easily summarize these collections can be useful.

One such calculation which is necessary sometimes is the sum of squares of a list of numbers.

The easiest way to get the sum of squares of a list of numbers in Python is with a for loop.

You can get the sum of squares of a list of numbers with a for loop by simply adding up the square of each number in a given list or range.

Below is an example which shows you how to use a for loop to get the sum of squares of a list in Python.

def sum_of_squares(lst):
    sum = 0
    for x in lst:
        sum = sum + x ** 2
    return sum

print(sum_of_squares(range(10))) # range(0,1,2,3,4,5,6,7,8,9)
print(sum_of_squares([4,6,2,9,10]))

#Output:
285
237

Using List Comprehension to Find Sum of Squares in Python

You can also use the sum() function and list comprehension to find the sum of squares of a list of numbers using Python.

For basic operations, wherever you use a for loop, it’s probable you can use list comprehension.

Below shows you how you can use list comprehension to get the sum of squares of a list in Python.

def sum_of_squares(lst):
    return sum([x ** 2 for x in lst])

print(sum_of_squares(range(10))) # range(0,1,2,3,4,5,6,7,8,9)
print(sum_of_squares([4,6,2,9,10]))

#Output:
285
237

Find Sum of Squares of First N Numbers with Formula in Python

For the first n numbers, there exists a formula which you can use which will give you the sum of the squares.

The sum of squares for the first n numbers is:

(n * (n + 1) * (2 * n + 1)) / 6

Below is a simple function which will get the sum of the squares of the first n numbers using Python. Note the use of integer division to return an integer value.

def sum_of_squares_first_n(n):
    return (n * (n + 1) * (2 * n + 1)) // 6

print(sum_of_squares_first_n(5))

#Output:
55

Hopefully this article has been useful for you to learn how to find the sum of squares in a list of numbers using Python.

Other Articles You'll Also Like:

  • 1.  Examples of Recursion in Python
  • 2.  Change Column Name in pandas DataFrame
  • 3.  Read Last N Lines of File in Python
  • 4.  Python tostring method – Convert an Object to String with str() Function
  • 5.  Using Python to Capitalize Every Other Letter of String
  • 6.  How to Find the Longest String in List in Python
  • 7.  Break Out of Function in Python with return Statement
  • 8.  How to Split List in Half Using Python
  • 9.  Convert Set to List in Python
  • 10.  Python ljust Function – Left Justify String Variable

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