In this article, we are going to calculate the sum of squares in python. We are going to learn different ways to calculate the sum of squares in python. Using for loop, while loop, and using functions to calculate the sum of squares. At the end of the article, we will get a clear idea about this topic. Let us move on to the topic.

**The addition of all the squared numbers is known as the sum of squares. For example, if we need the sum of squares of the first 10 natural numbers. First, we have to calculate the sum of the first 10 natural numbers. Now we have to square the result. This process is known as the sum of squares in python. Squared terms may contain any of the following terms: natural numbers, consecutive numbers, first n numbers, first n even numbers, first n odd numbers. The formula for calculating square numbers is:**

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

For example, N=5, the sum of the square is:

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

(5*6*11)/6

330/6

The sum of squares is: 55

**First, we will calculate the sum of squares of n natural numbers**

## Method 1: Using functions to find the sum of squares in python

By using functions, there are two methods available to find the sum of squares in python. One using a loop, another one without using the loop.

### Code 1

```
def square(num) :
sum = 0
for i in range(1, num+1) :
sum= sum + (i * i)
return sum
num = 6
print("Sum of square is:",square(num))
```

**Explanation**

Create a function named square. Initialize sum is zero. Create a for loop to iterate till the end of the loop. We are giving the num value as 6. So it will iterate till it reaches 6. Once it reached 6. It will come out of the loop and execute the result.

**Output**

Sum of square is: 91

### Code 2

```
def square(num) :
return (num * (num + 1) * (2 * num + 1)) // 6
num = 5
print("Sum of square is:",square(num))
```

**Explanation**

Create a function named square. Using the formula to calculate the square of the numbers. Printing the result.

**Output**

Sum of square is: 55

## Method 2: Using for loop to find the sum of squares in python

Now we will discuss how to calculate the sum of squares using for loop. By using for loop, we can find the sum of squares in two different methods.

### Code 1

```
num= int(input("Enter value of num: "))
sum = 0
for i in range(1, num+1):
sum = sum+(i*i)
print("Sum of squares = ", sum)
```

**Explanation**

Getting the num value from the user. **Initialize sum** is equal to zero. Creating for loop to calculate the result. Inside for loop, we have declared sum=sum+(i*i). That is calculating the square of i and adding it to the sum. Finally, printing the sum of squares.

**Output**

Enter value of num: 3 Sum of squares = 14

### Code 2

```
num=int(input("Enter value of num: "))
sum=int((num * (num+1) * ((2*num) + 1))/6)
print("Sum of squares =",sum)
```

**Explanation**

Getting the num value from the user. Using the formula to calculate the square of the numbers. Printing the result.

**Output**

Enter value of num: 7 Sum of squares = 140

## Method 3: Using while loop to find the sum of squares in python

```
num = int(input("Enter num value : "))
sum = 0
while num>0:
sum = sum + (num*num)
num = num-1
print("Sum of square =",sum)
```

**Explanation**

Getting the value of the num from the user. Initializing the sum is equal to zero, inside a loop, calculating a square of num and add it to sum.

**Output**

Enter num value : 4 Sum of square = 30

## Method 4: Using a list to find sum of squares in python

```
lst=[1,2,3,4,5,6]
sum=0
for i in lst:
sum=sum+(i*i)
print("Sum of squares =",sum)
```

**Explanation**

Create a list with some elements. Initialize a sum as 0. Creating a for loop to iterate till the end of the list. Inside for loop, we have declared sum=sum+(i*i). That is calculating the square of i and adding it to the sum. Finally, printing the sum of squares.

**Output**

Sum of squares = 91

## Sum of squares of n even natural numbers

**The formula for the sum of squares in python of n even natural number is:**

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

For example, if we take n=4

2 * 4(4+1)(2(4)+1)/3

(2*4*5*9)/3

360/3

The Sum of n even natural number is 120

### Code

```
def square(num):
sum = 0
for i in range (0, num + 1):
sum=sum+(2 * i)*(2 * i)
return sum
result = square(4)
print (result)
```

**Explanation**

Create a function named square. Initialize sum is equal to zero. Creating a for loop to iterate. Inside the loop giving sum=sum+(2 * i)*(2 * i). That is known as multiplying (2*i) twice and adding it to the sum. Finally, returning a result.

**Output**

120

## Sum of squares of n odd numbers

Suppose the num value is 5. It will sum the five squares of odd numbers. Here it calculates 1^{2}+3^{2}+5^{2}+ 7^{2}+ 9^{2}

It becomes 1+9+25+49+81

The sum of all the squares is 165.

### Code

```
def square(num):
sum = 0
for i in range(1, num + 1):
sum = sum+(2 * i - 1) * (2 * i - 1)
return sum
num=5
print(square(num))
```

**Explanation**

Create a function named square. Initialize sum is equal to zero. Create a for loop to iterate till the end of the loop. Declaring num is equal to 5. Printing the square of odd numbers.

**Output**

165

## Sum of squares using recursion

```
def sum(n):
if n==1:
return 1
return n**2+sum(n-1)
print(sum(5))
```

**Explanation**

Create a function named **sum if** the n value is equal to 1. It will return 1 because 1X1 is 1. If it is greater than 1, it will calculate n**2+sum(n-1). Now we will calculate the sum of squares of 5.

**Output**

55

## Residual sum of squares

Another name for the residual sum of squares is a sum of square residuals. This is a statistical technique. The technique is useful to measure the amount of variance in data. The difference between the observed and predicted value is known as the residual sum of squares. The formula for the residual sum of squares is:

Σ(e_{i})^{2}

**Steps to be followed**

- First to enter the data using pandas
- After that fix the regression model
- Finally calculate the residual sum of squares

## Code

```
import pandas as pd
data_frame = pd.DataFrame({'no.of.hours': [1,2,3,4],
'match': [3,5,2,1],
'score': [76, 78, 85, 88]})
import statsmodels.api
b = data_frame['score']
a = data_frame[['no.of.hours', 'match']]
a = statsmodels.api.add_constant(a)
result = statsmodels.api.OLS(b, a).fit()
print("Residual sum of square is:",result.ssr)
```

**Output**

Residual sum of square is: 1.0638297872340425

## FAQs Related to the Sum of Squares in Python

**1. What is the formula to calculate the sum of squares of n natural numbers?**

(N*(N +1)*(2*N+1))/6 is the formula to calculate the sum of squares of n natural numbers.

**2. What is the formula to calculate the sum of squares of first n even natural numbers?**

2 * n(n+1)(2n+1)/3 is the formula to calculate the sum of squares of first n even natural numbers.

## Conclusion

Here we have learned how to calculate the sum of squares of n natural numbers. We have also learned how to calculate the sum of squares of n, even natural numbers. And also the sum of squares of n odd numbers.

We hope this is easy to understand. Try to solve the programs on your own. Learn with us 🙂