Arithmetic Progression in Python – A Complete Guide

FeaImg Arithmetic Progression

Hey Folks! In this tutorial, we will understand what an Arithmetic Progression is and how to implement the same in the Python programming language.


Introduction to Arithmetic Progression (A.P.)

The Arithmetic Series is a term series in which the next item is generated by adding a common difference to the preceding item.

The A.P. series is a number sequence in which the difference between any two consecutive numbers is always the same. This distinction is known as a common difference.

Arithmetic Progression Series is calculated mathematically as follows:

Sum of A.P. Series : Sn = n/2(2a + (n – 1) d)
Tn term of A.P. Series: Tn = a + (n – 1) d


Code Implementation of Arithmetic Progression in Python

Let’s get into the implementation of the Arithmetic Progression using Python. We’ll take two examples of the same to help you understand this concept better.

1. Print first n terms of the Arithmetic Progression

There are a number of steps involved to achieve the n AP terms. The steps are as follows:

Step 1 – Take the input of a ( the first term ), d( the step ), and n ( the number of terms )
Step 2 – Take a loop from 1 to n+1 and compute the nth term in every iteration and keep printing the terms.

# 1. Take input of 'a','d' and 'n'
a = int(input("Enter the value of a: "))
d = int(input("Enter the value of d: "))
n = int(input("Enter the value of n: "))

# 2. Loop for n terms
for i in range(1,n+1):
    t_n = a + (i-1)*d
    print(t_n)

2. Get Sum of first n terms in Arithmetic Progression

There are a number of steps involved to achieve the sum of first n AP terms. The steps are as follows:

Step 1 – Take the input of a ( the first term ), d( the step ), and n ( the number of terms )
Step 2 – Use the formula mentioned above to compute the sum of the first ‘n’ terms.

# 1. Take input of 'a','d' and 'n'
a = int(input("Enter the value of a: "))
d = int(input("Enter the value of d: "))
n = int(input("Enter the value of n: "))

S_n = (n/2)*(2*a + (n-1)*d)
print("Sum of first n terms: ", S_n)
Enter the value of a: 1
Enter the value of d: 2
Enter the value of n: 5
Sum of first n terms:  25.0

Conclusion

Congratulations! You just learned how to implement Arithmetic Progression in Python. Hope you enjoyed it! 😇

Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:

  1. Memoization in Python – A Brief Introduction
  2. Introduction to Anagrams in Python
  3. Python Wonderwords module – A brief Introduction

Thank you for taking your time out! Hope you learned something new!! 😄