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
    else:

        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]))

Output:

True
False

Explanation

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.

 


PROGRAMS

Latest Articles

Latest from djangocentral

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read