Python Program To Check Whether The Sequence Is In Arithmetic Progression

1 min read

A sequence is called to Arithmetic series when the difference between two consecutive numbers remains constant throughout the series. If we express the first term as a and the difference between the terms d, then we can generalize any arithmetic progressive series as [a, a+d, a+2d, a+3d …,a+kd]

In this article, we will create a Python program to check whether the given sequence is in Arithmetic Progression (A.P.) or not.

Python Program To Check Whether A Sequence Is In Arithmetic Progression

def progression(l):

    if len(l) == 1:
        return True

        diff = l[1] - l[0]
        for index in range(len(l) - 1):
            if not (l[index + 1] - l[index] == diff):
                return False
        return True

print(progression([7, 3, -1, -5]))
print(progression([3, 5, 7, 9, 10]))




In the above program first, we verifying whether the length of the series is greater than one because there must be at least 2 elements in any Arithmetic series. Next, we are calculating the difference between the first two elements of the sequence then we are examining whether the difference is constant throughout the list by iterating over each element of the list if this evaluates to true the given list is in Arithmetic Progression.



Latest Articles

Latest from djangocentral

Django 4.1 adds async-compatible interface to QuerySet

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - The Django core team has been progressively adding async suppor…
Read more →

3 min read

Making Django Admin Jazzy With django-jazzmin

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…
Read more →

4 min read