How to Use Pi in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Pi in mathematics is a constant value equal to 3.141592653589793 that is used to represent the ratio of the circumference of a circle to its diameter. Pi being an irrational number, is usually represented as an approximation value since the decimal representation of the value doesn't end.

Introduction to Pi in Python

Python provides libraries, methods, and modules to find the value of pi. To calculate the value of pi we will use

  • math.pi
  • NumPy
  • math.radians

Using Math.pi

math is an inbuilt Python library that imports math.pi and prints the required value of pi.

Output:

Using NumPy for Pi

numpy.pi is a method to calculate the value of pi using NumPy.

Output:

Access Pi (π) Using Math.radians

We can also find the value of pi using the function radians() from the math library provided by Python. Passing the value for radian as 180, the value of pi is returned.

Output:

Example: Calculating the Circumference of a Circle

Output:

Conclusion

  • To find the value of pi using Python we use an inbuilt python library math that imports math.pi to calculate the value of pi.
  • We can also use numpy.pi to find the value of pi.
  • Using the function radian() with 180 passed as an argument also returns the value of pi in Python programming language.