• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / pi in Python – Using Math Module and Leibniz Formula to Get Value of pi

pi in Python – Using Math Module and Leibniz Formula to Get Value of pi

January 28, 2022 Leave a Comment

To get the value of pi in Python, the easiest way is use the Python math module constant pi. math.pi returns the value 3.141592653589793.

import math

print(math.pi) 

#Output: 
3.141592653589793

You can also use the numpy module to get the value of pi.

import numpy as np

print(np.pi) 

#Output: 
3.141592653589793

In Python, we can easily get the value of pi for use in trigonometry
with the Python math module.

For example, let’s say we want to find the sine of a number. The Python sin() function takes a number in radians and then finds the sine of the number.

To find the sine of a number, we can use pi to make our lives easy.

To get the value of pi in your Python code, you just need to import the math module and call it as shown below.

import math

print(math.pi)

Then, if you want to use it like in our example with finding the sine of a number, you can use the pi constant in the following way.

import math

print(math.sin(math.pi/3))

#Output:
0.8660254037844386

Using numpy to Get Value of pi in Python

Another module which has the mathematical constant pi is numpy.

You can do the following to get the value of pi with numpy.

import numpy as np

print(np.pi) 

#Output: 
3.141592653589793

Finding pi in Python Without the Math Module

With the Python math module, we can get a pretty good estimate of pi.

However, you may want to calculate pi for yourself without the math module to get more digits and a more accurate representation of pi.

In this case, we can use the Leibniz formula.

Below is a function which will let you calculate pi to more digits than just 15 digits like in the math module. In this example, we use the Python decimal module to calculate pi to 100 digits.

import decimal
import math

decimal.getcontext().prec = 100

def leibniz(n):
    pi = decimal.Decimal(1)
    for i in range (1,n):
        pi += ((-1) ** i ) * (decimal.Decimal(1) / (2 * i + 1))
    return pi * 4

print(leibniz(1000))

#Output: 
Decimal('3.140592653839792925963596502869395970451389330779724489367457783541907931239747608265172332007670214')

Hopefully this article has been helpful for you to understand how to find the value of pi from the Python math module.

Other Articles You'll Also Like:

  • 1.  Using Python to Repeat Characters in String
  • 2.  Get Year from Date in Python
  • 3.  Examples of Recursion in Python
  • 4.  Find Maximum of Three Numbers in Python
  • 5.  Check if List is Subset of Another List in Python
  • 6.  pandas Drop Rows – Delete Rows from DataFrame with drop()
  • 7.  PROC MIXED Equivalent in Python for Least Squared Means ANOVA
  • 8.  How to Serialize a Model Object with a List of Lists Using Django Rest Framework in Python
  • 9.  pandas Absolute Value – Get Absolute Values in a Series or DataFrame
  • 10.  Remove Specific Word from String in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy