You might have heard about arithmetic sequences and geometric sequences in your mathematics classes. In this article, we will discuss arithmetic sequences. We will also implement programs to perform different operations on an arithmetic sequence in Python.

## What is An Arithmetic Sequence?

An arithmetic sequence is a sequence of numbers in which any two consecutive numbers have a fixed difference. This difference is also known as the common difference between the terms in the arithmetic sequence.

For example, 3,5,7,9,11,13,… is an arithmetic sequence with a common difference of 2 between consecutive terms.

## Nth Term In An Arithmetic Sequence

If we are given the first term A_{1} and the common difference D, we can write the second term as `A`

, the third term as_{1}+D` A`

, the fourth term as _{1}+2D`A`

, and so on. The Nth term will be written as _{1}+3D`A`

To find the Nth term of an arithmetic sequence in python, we can simply add the common difference (N-1) times to the first terms A_{1}+(N-1)D_{1} using a for loop as follows.

```
commonDifference = 2
print("Common Difference in the arithmetic sequence is:", commonDifference)
firstTerm = 3
print("First term in the arithmetic sequence is:", firstTerm)
# calculating 100th term
N = 100
nthTerm = firstTerm
for i in range(1, N):
nthTerm = nthTerm + commonDifference
print("100th term in the arithmetic sequence is:", nthTerm)
```

Output:

```
Common Difference in the arithmetic sequence is: 2
First term in the arithmetic sequence is: 3
100th term in the arithmetic sequence is: 201
```

Alternatively, we can directly calculate the Nth term using the formulae as follows.

```
commonDifference = 2
print("Common Difference in the arithmetic sequence is:", commonDifference)
firstTerm = 3
print("First term in the arithmetic sequence is:", firstTerm)
# calculating 100th term
N = 100
nthTerm = firstTerm + (N - 1) * commonDifference
print("100th term in the arithmetic sequence is:", nthTerm)
```

Output:

```
Common Difference in the arithmetic sequence is: 2
First term in the arithmetic sequence is: 3
100th term in the arithmetic sequence is: 201
```

## Sum Of N Terms In An Arithmetic Sequence In Python

To find the sum of N terms in an arithmetic expression, we can simply add each term using a for loop. In the for loop, we will first find each term using the formulae discussed above. After that, we will add the each term to calculate the sum of N terms as follows.

```
commonDifference = 2
print("Common Difference in the arithmetic sequence is:", commonDifference)
firstTerm = 3
print("First term in the arithmetic sequence is:", firstTerm)
# calculating sum of 50 terms
N = 50
sumOfTerms = 0
for i in range(1, N + 1):
ithTerm = firstTerm + (i - 1) * commonDifference
sumOfTerms = sumOfTerms + ithTerm
print("Sum of 50 terms in the arithmetic sequence is:", sumOfTerms)
```

Output:

```
Common Difference in the arithmetic sequence is: 2
First term in the arithmetic sequence is: 3
Sum of 50 terms in the arithmetic sequence is: 2600
```

Alternatively, we can also derive a mathematical expression for calculating the sum of N terms of the arithmetic sequence.

We know that the sum of N numbers will be equal to N * (average of all the terms). Here, we can find the average of all the terms very easily.

For an arithmetic sequence with the first term A_{1} and the Nth term A_{N}, the average of all the terms is defined as `(A`

. As A_{1}+A_{N})/2_{1} and common difference D will be given in the program, we can find `A`

._{N}= A_{1}+ (N-1)*D

Hence, the average of all the numbers in the arithmetic sequence will become` (2A`

._{1}+ (N-1)*D)/2

Subsequently, the sum of N terms of the arithmetic sequence will become `N*((2A`

._{1}+ (N-1)*D)/2)

We can calculate the sum of N terms in the arithmetic equation using this formula in python as follows.

```
commonDifference = 2
print("Common Difference in the arithmetic sequence is:", commonDifference)
firstTerm = 3
print("First term in the arithmetic sequence is:", firstTerm)
# calculating sum of 50 terms
N = 50
sumOfTerms = (N * (2 * firstTerm + (N - 1) * commonDifference)) // 2
print("Sum of 50 terms in the arithmetic sequence is:", sumOfTerms)
```

Output:

```
Common Difference in the arithmetic sequence is: 2
First term in the arithmetic sequence is: 3
Sum of 50 terms in the arithmetic sequence is: 2600
```

## Conclusion

In this article, we have discussed the basics and formulas of arithmetic sequences. We have also performed different operations like finding the Nth term and finding the sum of N terms of an arithmetic sequence in python. To learn more about numbers in python, you can read this article on decimal numbers in python. You might also like this article on complex numbers in python.

## Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.