Mathematical Operations

Want to learn more? I recommend these Python books: Python for Data Analysis, Python Data Science Handbook, and Introduction to Machine Learning with Python.

Import the math module

import math

Display the value of pi.

math.pi
3.141592653589793

Display the value of e.

math.e
2.718281828459045

Sine, cosine, and tangent

math.sin(2 * math.pi / 180)
0.03489949670250097

Exponent

2 ** 4, pow(2, 4)
(16, 16)

Absolute value

abs(-20)
20

Summation

sum((1, 2, 3, 4))
10

Minimum

min(3, 9, 10, 12)
3

Maximum

max(3, 5, 10, 15)
15

Floor

math.floor(2.949)
2

Truncate (drop decimal digits)

math.trunc(32.09292)
32

Truncate (integer conversion)

int(3.292838)
3

Round to an integrer

round(2.943), round(2.499)
(3, 2)

Round by 2 digits

round(2.569, 2)
2.57