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.
Example: Find the minimum and maximum values from a set of numbers.
Absolute Value
The abs() function converts a negative number into its positive
form, while positive numbers remain unchanged.
Example: Get the absolute value of a number.
Power Function
The pow() function raises a number to a specified power.
Example: Calculate 3 raised to the power of 4.
Python Math Module
For more complex mathematical calculations, Python provides the
math module. To access its features, you must import it first.
Syntax: import math
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.
