Skip to content

Python math module | Python import math | Function | Operators

Python maths module is a standard module and is always available in python to do mathematical operations easily. To Import math in python is to give access to the mathematical functions, which are defined by the C standard. In this tutorial, you will learn about some important math module functions with examples in python.

To use Python math functions, you have to import the module using the import math line at the starting of the program to get the math class object. Using a math class object can access any math function in python.

Python math module | Python import math, Function , Operators examples

What is a module? Must read this tutorial – Python Modules | Import Custom and Built-in.

This tutorial, you will learn How to python import math and use with Latest examples.

Note: The functions under the math module do not support a complex number or you can’t use. For use a complex number with this function you have to use other modules in python.

Syntax

A Simple syntax of Python import math.

#get pi value 
import math
math.pi

Python import math gives access to the standard C library functions.

Let’s see an example of math function 

Once you have done the Python import math module, then you can access modules function like the math.sqrt(value) – square root of the number. It returns a float number value.

See this example how to use match square root function in python. We doing for value 25, so output should be 5.

import math

# Square root of number
sqrValue = math.sqrt(25)
print(sqrValue)

Output: 5.0

Some Functions in Python Math Module

If we are trying to cover all functions in python math module, then the list is very long. Python separated in math functions in a group, here it is –

  • Number-theoretic and representation functions
  • Power and logarithmic functions
  • Trigonometric functions
  • Angular conversion
  • Hyperbolic functions
  • Special functions
  • Constants

Every group has many functions so, Let’s see some important math functions. For a complete list of a function, you can read it on the official site, mentioned at the end of the tutorial.

METHODDESCRIPTION
piMathematical constant, the ratio of the circumference of a circle to its diameter (3.14159…)
pow(x, y)Returns x raise to the power of y
fabs(x)Return absolute value of x
max(x1, x2, …, Xn)Returns the largest value among supplied arguments (Use without math object class)
min(x1, x2, …, Xn)Returns the smallest value among supplied arguments (Use without math object class)
ceil(x)Returns the smallest integer greater than or equal to x.
floor(x)Returns the largest integer less than or equal to x
sqrt(x)Returns the square root of the number
sin(x)Returns sin of x where x is in radian
cos(x)Returns cosine of x where x is in radian
tan(x)Returns tangent of x where x is in radian

Example of some math functions 

These are some example code of Python math function, so you can learn to teach you how you can use math function.

# Math module functions

import math

sqrValue = math.pow(25.3, 2)
print("pi: ", math.pi)
print("pow: ", math.pow(3, 2))
print("ceil: ", math.ceil(7.24))
print("floor: ", math.floor(7.24))
print("sqrt: ", math.sqrt(9))
print("sin", math.sin(90))
print("cos", math.cos(90))
print("tan", math.tan(90))

Output screenshot:

Example of some math functions output and python import math

In the example used many operators, so for Python math operators operations must read this tutorial – Python Operators Overview with Examples

Reference: https://docs.python.org/3/library/math.html (Official website – Visit this page to learn about all the)

Do comment if any doubt, suggestion or examples you have.

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Python maths module, Function and import examples are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading