← Back to Chapters

Python Built-in Math Functions

? Python Built-in Math Functions

⚡ Quick Overview

Python provides several built-in functions that make common mathematical operations very easy and fast to perform. Functions like abs(), round(), min(), max(), sum(), pow(), and divmod() cover basic numeric processing needs without importing any extra modules.

? Key Concepts

  • abs(x) – absolute value (distance from zero) of a number.
  • round(x, ndigits) – rounds a number to the given decimal places or to tens/hundreds with negative ndigits.
  • min() / max() – find the smallest or largest value in iterables or among many arguments.
  • sum(iterable, start) – adds all elements and an optional starting value.
  • pow(base, exp, mod) – power calculation with optional modulo.
  • divmod(a, b) – returns a tuple (quotient, remainder).
  • Helper conversions: int(), float(), complex(), bin(), oct(), hex().

? Syntax and Behavior

  • ? Absolute value: abs(number)
  • ? Rounding: round(number, ndigits=0)
  • ? Min/Max: min(iterable), max(iterable) or min(a, b, c, ...)
  • Sum: sum(iterable, start=0)
  • Power: pow(base, exp, mod=None)
  • ? Divmod: divmod(a, b)(a // b, a % b)
  • ? Conversions: int(x), float(x), complex(real, imag)
  • ? Number bases: bin(x), oct(x), hex(x)

? Code Examples

? abs() – Absolute Value

Returns the absolute (non-negative) value of integers, floats, or the magnitude of complex numbers.

? View abs() example
print(abs(-10))      # 10
print(abs(3.5))       # 3.5
print(abs(3+4j))      # 5.0 (magnitude)

? round() – Rounding Numbers

Rounds to the nearest integer by default. With ndigits, you can control decimal places or round to tens/hundreds by using a negative value.

? View round() example
print(round(4.567))       # 5
print(round(4.567, 2))   # 4.57
print(round(12345, -2))  # 12300

? min() and max() – Minimum & Maximum

Works with iterables like lists or with multiple numeric arguments.

? View min() and max() example
numbers = [3, 7, 1, 9]
print(min(numbers))      # 1
print(max(numbers))      # 9

print(min(4, 2, 9, 1))   # 1
print(max(4, 2, 9, 1))   # 9

➕ sum() – Sum of Numbers

sum() adds all values in an iterable and can start from a custom initial value.

? View sum() example
numbers = [1, 2, 3, 4, 5]
print(sum(numbers))         # 15
print(sum(numbers, 10))     # 25

⚡ pow() – Power with Optional Modulo

Use for exponentiation, especially when you also need the remainder (modular arithmetic).

? View pow() example
print(pow(2, 3))        # 8
print(pow(5, 2))        # 25
print(pow(2, 3, 3))     # 2 (modulo 3)

? divmod() – Quotient and Remainder

Returns both integer division result and remainder in a single call.

? View divmod() example
print(divmod(17, 3))    # (5, 2)
print(divmod(22, 7))    # (3, 1)

? Other Useful Conversion Functions

Conversions between numeric types and representations in different bases.

? View conversion examples
print(int(7.89))          # 7 → convert to integer
print(float(7))            # 7.0 → convert to float
print(complex(2, 3))      # (2+3j) → complex number
print(bin(10))             # 0b1010 → binary
print(oct(10))             # 0o12 → octal
print(hex(10))             # 0xa → hexadecimal

? What the examples produce

  • abs(-10)10, because absolute value ignores the sign.
  • round(4.567, 2)4.57, rounded to 2 decimal places.
  • min([3, 7, 1, 9])1, the smallest value in the list.
  • sum([1, 2, 3, 4, 5], 10)25, since 1+2+3+4+5+10 = 25.
  • pow(2, 3, 3)2, because 2³ = 8 and 8 % 3 = 2.
  • divmod(17, 3)(5, 2), meaning 17 = 5 × 3 + 2.
  • bin(10)0b1010, the binary representation of 10.

? Tips & Best Practices

  • Use abs() when working with distances, magnitude of vectors, or error values.
  • Remember that round() with a negative ndigits rounds to tens, hundreds, etc.
  • min() and max() work on lists, tuples, sets, and multiple arguments.
  • Use the start parameter of sum() for cumulative totals or to include a base value.
  • pow(base, exp, mod) is very useful in algorithms involving modular arithmetic (e.g., cryptography).
  • Use bin(), oct(), and hex() to inspect or display numbers in different bases.

? Practice Tasks

  • Compute the absolute value of -42 and -3.7 using abs().
  • Round 8.7654 to 2 decimal places, and then round it to the nearest hundred.
  • Find the minimum and maximum of the list [14, 7, 19, 3, 5].
  • Use sum() to add numbers from 1 to 15 with a start value of 10.
  • Use divmod() to get the quotient and remainder of 37 divided by 6.
  • Convert the number 255 into binary, octal, and hexadecimal using bin(), oct(), and hex().
  • Create a complex number 4 + 5j and use abs() to find its magnitude.