• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Arithmetic Sequence In Python

Arithmetic Sequence In Python

Author: Aditya Raj
Last Updated: January 9, 2022

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 A1 and the common difference D, we can write the second term as A1+D, the third term as A1+2D, the fourth term as A1+3D, and so on. The Nth term will be written as A1+(N-1)D 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 A1 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 A1 and the Nth term AN, the average of all the terms is defined as (A1+AN)/2. As A1 and common difference D will be given in the program, we can find AN= A1+ (N-1)*D.

Hence, the average of all the numbers in the arithmetic sequence will become (2A1+ (N-1)*D)/2.

Subsequently, the sum of N terms of the arithmetic sequence will become N*((2A1+ (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.

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us