Python includes core math functions globally so you do not need to import a module for everyday calculations. Functions like min(), max(), abs(), round(), sum(), and pow() work directly on integers, floats, and sequences.
For trigonometry, logarithms, or square roots, Python moves those functions into the math standard library module covered in the math module lesson.
You can pass multiple arguments or an entire iterable into min() and max(), and use pow(base, exp, mod) for fast modular exponentiation.
readings = [-4.2, 18.7, 9.4, 22.1]
lowest = min(readings) # -4.2
highest = max(readings) # 22.1
spread = abs(highest - lowest) # 26.3
average = round(sum(readings) / len(readings), 2)
print(lowest, highest, spread, average)
round() Behaves in Python 3Python 3 uses round half to even (also called banker's rounding) to prevent systematic upward bias across large datasets. That means round(2.5) returns 2 and round(3.5) returns 4.
round(2.5) to return 3: Python rounds .5 ties to the nearest even integer; use decimal.Decimal with ROUND_HALF_UP when financial rules require half-up rounding.sum() on strings: sum(["a", "b"]) raises TypeError; use "".join(...) for strings.divmod(a, b): Writing both a // b and a % b computes division twice; q, r = divmod(a, b) returns both in one step.pow(x, y, z) do?pow(x, y, z) computes (x ** y) % z cleanly and fast without building huge intermediate numbers, which is ideal for cryptography and hashing.
Guard against zero division: avg = sum(nums) / len(nums) if nums else 0.0.
abs() work on complex numbers?Yes. abs(3 + 4j) returns the magnitude 5.0 as a float.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.