Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - Math Module

Python

python logo

In this tutorial we will learn about Math module and Mathematical Functions in Python.

Math Module

The math module is a standard module available in Python and it provides mathematical functions to developers.

To use the functions available in the math module we must start by first including the module using the import statement.

Write the following line to import the math module.

import math

Math Constants

Following are the commonly used math constants used in Python.

ConstantDescription
eThis represents the mathematical constant e.
piThis represents the mathematical constant pi.

In the following example we are printing the value of the mathematical constants.

# import module
import math

# output
print("e:", math.e)         # e: 2.718281828459045
print("pi:", math.pi)       # pi: 3.141592653589793

Math Functions

Following are some of the commonly used mathematical functions from the math module in Python.

FunctionDescription
math.ceil(x)Returns the smallest integer greater than or equal to x.
math.fabs(x)Returns the absolute value of x.
math.floor(x)Returns the largest number not greater that x.
math.log(x)Finds the natural log of x (where, x > 0).
math.log10(x)Finds the base 10 log of x (where, x > 0).
max(x1, x2, ...)Returns the maximum value from the given values.
min(x1, x2, ...)Returns the minimum value from the given values.
math.pow(x, y)Returns x to the power of y i.e. xy.
round(x, [,n])Returns rounded value of x upto n decimal place.
math.sqrt(x)Returns the square root of x.

In the following Python program we are using some of the Math functions.

# import module
import math

# output
print("ceil:", math.ceil(3.14))              # ceil: 4
print("fabs:", math.fabs(3.14))              # fabs: 3.14
print("fabs:", math.fabs(-3.14))             # fabs: 3.14
print("floor:", math.floor(-3.14))           # fabs: -4
print("log:", math.log(8))                   # log: 2.0794415416798357
print("log10:", math.log10(8))               # log10: 0.9030899869919435
print("max:", max(1, 5, 3, 4))               # max: 5
print("min:", min(9, 3, 1, 6))               # min: 1
print("pow:", math.pow(2, 10))               # pow: 1024
print("round:", round(3.14159))              # round: 3
print("round:", round(3.14159, 2))           # round: 3.14
print("sqrt:", math.sqrt(4))                 # sqrt: 2

Math Trigonometry Functions

Following are some of the commonly used trigonometry functions from the math module in Python.

FunctionDescription
math.acos(x)Returns the arc cosine of x radian.
math.asin(x)Returns the arc sine of x radian.
math.atan(x)Returns the arc tangent of x radian.
math.cos(x)Returns the cosine of x radian.
math.sin(x)Returns the sine of x radian.
math.tan(x)Returns the tangent of x radian.
math.degrees(x)Convert x from radian to degree.
math.radians(x)Convert x from degree to radian.

In the following Python program we are using some of the trigonometry functions.

# import module
import math

# output
print("acos:", math.acos(0.5))              # acos: 1.0471975511965976
print("asin:", math.asin(0.5))              # asin: 0.5235987755982988
print("atan:", math.atan(0.5))              # atan: 0.46364760900080615

print("cos:", math.cos(1.047197551))        # cos: 0.5000000001702587
print("sin:", math.sin(0.523598775))        # sin: 0.49999999948185797
print("tan:", math.tan(0.463647609))        # tan: 0.4999999999989924

print("degree:", math.degrees(3.1415926))   # degree: 179.99999692953102
print("radian:", math.radians(180))         # radian: 0.4999999999989924

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT