• 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
  • VBA
  • 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.  How to Check if a Letter is in a String Using Python
  • 2.  Python isfinite() Function – Check if Number is Finite with math.isfinite()
  • 3.  Python Trig – Using Trigonometric Functions in Python for Trigonometry
  • 4.  Union of Lists in Python
  • 5.  Sorting a List of Tuples by Second Element in Python
  • 6.  Print Object Attributes in Python using dir() Function
  • 7.  Python Get Operating System Information with os and platform Modules
  • 8.  Using Python to Replace Backslash in String
  • 9.  Remove Specific Word from String in Python
  • 10.  pandas ewm – Calculate Exponentially Weighted Statistics in DataFrame

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy