Loading lessons...
Math
Python has a set of built-in math functions plus a full math module.
Built-in math functions
These work without any import:
python
x = min(5, 10, 25)
### 5
y = max(5, 10, 25)
### 25
print(abs(-7.25))
### 7.25
print(pow(4, 3))
### 64 (4 to the power 3)
The math module
Import it for the full toolkit:
python
import math
Square root and ceiling
python
print(math.sqrt(64))
### 8.0
print(math.ceil(1.4))
### 2
print(math.floor(1.4))
### 1
Constants
python
print(math.pi)
### 3.141592653589793
print(math.e)
### 2.718281828459045
Trigonometry
python
print(math.sin(0))
### 0.0
print(math.cos(0))
### 1.0
print(math.tan(0))
### 0.0
Logarithm and others
python
print(math.log(2))
### natural log
print(math.log10(100))
### 2.0
print(math.gcd(12, 8))
### 4
print(math.factorial(5))
### 120
TL;DR
- Built-ins: min, max, abs, pow.
- math module: sqrt, ceil, floor, pi, e, trig, log.
import mathunlocks the whole toolkit.- Constants pi and e come from the module.