Python Math Functions and Math Module
Python provides several built-in mathematical functions for basic
calculations. For advanced mathematical operations, Python also includes a
powerful
math module.
Built-in Math Functions
Finding Minimum and Maximum Values
The min() and
max()
functions help you quickly determine the smallest and largest values from a
collection of numbers.
Absolute Value
The
abs()
function converts a negative number into its positive form, while positive
numbers remain unchanged.
Power Function
The
pow()
function raises a number to a specified power.
Python Math Module
For more complex mathematical calculations, Python provides the
math module.
To access its features, you must import it first.
Square Root Calculation
The
math.sqrt()
function returns the square root of a number.
Rounding Numbers
-
math.ceil()rounds a number up to the nearest integer. -
math.floor()rounds a number down to the nearest integer.
Mathematical Constant π (Pi)
The
math.pi
constant provides an accurate value of π (pi), commonly used in geometry
and trigonometry.
