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.
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).int(), float(), complex(), bin(), oct(), hex().abs(number)round(number, ndigits=0)min(iterable), max(iterable) or min(a, b, c, ...)sum(iterable, start=0)pow(base, exp, mod=None)divmod(a, b) → (a // b, a % b)int(x), float(x), complex(real, imag)bin(x), oct(x), hex(x)Returns the absolute (non-negative) value of integers, floats, or the magnitude of complex numbers.
print(abs(-10)) # 10
print(abs(3.5)) # 3.5
print(abs(3+4j)) # 5.0 (magnitude)
Rounds to the nearest integer by default. With ndigits, you can control decimal places or round to tens/hundreds by using a negative value.
print(round(4.567)) # 5
print(round(4.567, 2)) # 4.57
print(round(12345, -2)) # 12300
Works with iterables like lists or with multiple numeric arguments.
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() adds all values in an iterable and can start from a custom initial value.
numbers = [1, 2, 3, 4, 5]
print(sum(numbers)) # 15
print(sum(numbers, 10)) # 25
Use for exponentiation, especially when you also need the remainder (modular arithmetic).
print(pow(2, 3)) # 8
print(pow(5, 2)) # 25
print(pow(2, 3, 3)) # 2 (modulo 3)
Returns both integer division result and remainder in a single call.
print(divmod(17, 3)) # (5, 2)
print(divmod(22, 7)) # (3, 1)
Conversions between numeric types and representations in different bases.
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
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.abs() when working with distances, magnitude of vectors, or error values.round() with a negative ndigits rounds to tens, hundreds, etc.min() and max() work on lists, tuples, sets, and multiple arguments.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).bin(), oct(), and hex() to inspect or display numbers in different bases.-42 and -3.7 using abs().8.7654 to 2 decimal places, and then round it to the nearest hundred.[14, 7, 19, 3, 5].sum() to add numbers from 1 to 15 with a start value of 10.divmod() to get the quotient and remainder of 37 divided by 6.255 into binary, octal, and hexadecimal using bin(), oct(), and hex().4 + 5j and use abs() to find its magnitude.