[Solved] How to write a python code on checking an arithmetic progression? [closed]


You can pair adjacent numbers, calculate the differences between the pairs, and determine that the list forms an arithmetic progression if the number of unique differences is no greater than 1:

from operator import sub
def progression(l):
    return len(set(sub(*p) for p in zip(l, l[1:]))) <= 1

so that:

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

outputs:

True
True
False

solved How to write a python code on checking an arithmetic progression? [closed]