Cao Yi

Math in Python

Index

Python has excellent built-in support for mathematics, from simple arithmetic to advanced functions via the math module.

Basic Operations

Python supports the four basic arithmetic operations directly:

Addition, Subtraction, Multiplication, Division

>>> 2 + 3
5
>>> 2 - 3
-1
>>> 2 * 3
6
>>> 2 / 3
0.6666666666666666

These operators work with both integers and floats. Division (/) always returns a float.

Important: You cannot divide by zero:

>>> 2 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

Floor Division and Modulo

>>> 7 // 3     # floor division
2
>>> 7 % 3      # remainder
1

Exponentiation (**)

Use ** for powers (not ^ — that’s bitwise XOR in Python):

>>> 2 ** 3
8
>>> 2 ** 3.14
8.815240927012887
>>> 2 ^ 3      # This is NOT exponentiation!
2

Other Built-in Functions

>>> abs(-5)
5
>>> round(3.14159, 2)
3.14
>>> max(1, 5, 3)
5

The math Module

For more advanced functions, import the math module:

>>> import math

Note: Python uses American English spelling (math, not maths):

>>> import maths
Traceback (most recent call last):
  ...
ModuleNotFoundError: No module named 'maths'

Common Functions

Power and roots:

>>> math.sqrt(2)          # square root
1.4142135623730951
>>> math.pow(2, 3)        # same as 2**3 but returns float
8.0

Logarithms:

>>> math.log(math.e)      # natural log (base e)
1.0
>>> math.log10(100)       # base 10
2.0
>>> math.log2(8)          # base 2
3.0

Trigonometry (all angles in radians):

>>> math.sin(math.pi)     # sin(π) ≈ 0 (floating-point result)
1.2246467991473532e-16
>>> math.sin(0)
0.0
>>> math.cos(0)
1.0
>>> math.pi               # constant π
3.141592653589793
>>> math.e                # constant e
2.718281828459045

Tip: Convert degrees to radians with math.radians():

>>> math.sin(math.radians(90))   # sin(90°)
1.0

Important Note on Precision

Python uses floating-point numbers (IEEE 754 double precision). math.pi is a very good approximation, but not the exact mathematical value of π. For calculations involving exact multiples of π (e.g., sin(n * π)), small floating-point errors can appear:

>>> math.sin(math.pi)   # Should be 0, but isn't exactly
1.2246467991473532e-16

For high-precision needs, consider the mpmath or sympy libraries.

Advanced Math

For more powerful mathematics:

Example with NumPy (for matrices):

import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B)        # matrix multiplication

For numerical integration, use scipy.integrate.

Summary