VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sequence-and-series-in-python/

⇱ Sequence and Series in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sequence and Series in Python

Last Updated : 23 Jul, 2025

Sequences and series are fundamental concepts in mathematics. A Sequence is an ordered list of numbers following a specific pattern, while a series is the sum of the elements of a sequence. This tutorial will cover arithmetic sequences, geometric sequences, and how to work with them in Python.

Arithmetic Sequence:

An arithmetic sequence is a sequence of numbers in which the difference between consecutive terms is constant. This difference is called the common difference (d).

Formula: an​ = a1​ + (nāˆ’1) * d

where:

  • an is the n-th term,
  • a1 is the first term,
  • d is the common difference,
  • n is the term number.

Sum of the first n terms: Sn​ = (n/2)​ * (2 * a1​ + (nāˆ’1) * d)

Python Implementation:

Output
Arithmetic Sequence: [2, 5, 8, 11, 14, 17, 20, 23, 26, 29]
Sum of Arithmetic Sequence: 155.0

Geometric Sequence:

A geometric sequence is a sequence of numbers where each term after the first is found by multiplying the previous term by a constant called the common ratio (r).

1. Working with Infinite Series

Formula: an​ = a1​ * r^(nāˆ’1)

Where:

  • an​ is the n-th term,
  • a1​ is the first term,
  • r is the common ratio,
  • n is the term number.

Sum of the first n terms (if r != 1): Sn​ = (a1/(​1āˆ’r^n))/(1āˆ’r)​

Python Implementation:

Output
Geometric Sequence: [2, 6, 18, 54, 162]
Sum of Geometric Sequence: 242.0

2. Working with Infinite Series

While working with infinite series, it's essential to know their convergence properties. For instance, an infinite geometric series converges if the absolute value of the common ratio is less than 1.

Sum of an infinite geometric series (|r| < 1): S = a1/(1āˆ’r)​​

Python Implementation:

Output
Sum of Infinite Geometric Series: 4.0

Harmonic Sequence:

A harmonic series is a series of numbers whose reciprocals form an arithmetic sequence. The n-th term of a harmonic sequence can be given by the reciprocal of the n-th term of an arithmetic sequence.

Formula: an​ = 1 / (a1​ + (nāˆ’1) * d)

where:

  • an is the n-th term,
  • a1 is the first term,
  • d is the common difference,
  • n is the term number.

Python Implementation:


Output
Harmonic Progression :
1 / 12 1 / 24 1 / 36 1 / 48 1 / 60 
Sum of the generated harmonic progression : 0.19
Sum of the generated harmonic progression using approximation : 0.19

Sequences and series are fundamental concepts in mathematics, and they have many applications in computer science, especially in algorithm design and numerical computations. By mastering these concepts, we can efficiently solve a wide range of problems in mathematics, data analysis, and algorithm development.


Comment
Article Tags: