abs() / round() / divmod() / pow()
Built-in functions for computing absolute values, rounding, division with remainder, and exponentiation. These cover the basic arithmetic operations you commonly need in numerical calculations.
Syntax
# Returns the absolute value. abs(number) # Rounds a number to the specified number of decimal places. round(number, ndigits) # Returns the quotient and remainder as a tuple. divmod(dividend, divisor) # Computes exponentiation. pow(base, exponent) pow(base, exponent, modulus) # base^exponent mod modulus (fast modular exponentiation) # The ** operator also computes exponentiation. base ** exponent
Function List
| Function | Description |
|---|---|
| abs(number) | Returns the absolute value of a number. Works with integers, floats, and complex numbers. |
| round(number, ndigits) | Rounds a number to the specified number of decimal places. If ndigits is omitted, rounds to the nearest integer. |
| divmod(a, b) | Returns a tuple (quotient, remainder) from dividing a by b. |
| pow(base, exponent) | Raises base to the power of exponent. If a third argument (modulus) is provided, computes modular exponentiation efficiently. |
| base ** exponent | The exponentiation operator. Produces the same result as pow(base, exponent). |
Sample Code
# Get the absolute value with abs().
print(abs(-42)) # Outputs '42'.
print(abs(-3.14)) # Outputs '3.14'.
print(abs(3+4j)) # Outputs the norm of the complex number: '5.0'.
# Round numbers with round().
print(round(3.14159, 2)) # Outputs '3.14'.
print(round(3.14159, 4)) # Outputs '3.1416'.
print(round(2.5)) # Outputs '2' (banker's rounding).
print(round(3.5)) # Outputs '4' (banker's rounding).
print(round(1234, -2)) # Outputs '1200' (rounded to the nearest hundred).
# Get the quotient and remainder at once with divmod().
quotient, remainder = divmod(17, 5)
print(quotient) # Outputs '3' (17 ÷ 5 = 3 remainder 2).
print(remainder) # Outputs '2'.
# Use divmod() to convert seconds into hours, minutes, and seconds.
total_seconds = 3661
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
print(f'{hours}h {minutes}m {seconds}s') # Outputs '1h 1m 1s'.
# Compute exponentiation with pow().
print(pow(2, 10)) # Outputs '1024'.
print(2 ** 10) # Also outputs '1024'.
print(pow(2, -1)) # Outputs '0.5'.
# Use the third argument of pow() for modular exponentiation (useful in cryptography).
print(pow(2, 10, 1000)) # Outputs '24' (2^10 mod 1000).
Details
abs() converts a negative number to its positive counterpart. It is commonly used to measure the magnitude of a difference. For complex numbers, it returns the norm (magnitude) of the vector formed by the real and imaginary parts.
round() uses Python's banker's rounding (round half to even), so values exactly halfway between two integers (x.5) are rounded toward the nearest even number. For example, round(2.5) returns 2 and round(3.5) returns 4. This differs from the conventional rounding rule (round half up). If you need standard rounding, use the decimal module.
divmod() computes both the quotient and remainder in a single call, making it convenient for unit conversions such as breaking a total number of seconds into hours, minutes, and seconds. The three-argument form pow(a, b, c) computes modular exponentiation efficiently for large numbers and is used in cryptographic algorithms such as RSA.
For mathematical functions such as square root, ceiling, and floor, see math.sqrt() / math.ceil() / math.floor().
If you find any errors or copyright issues, please contact us.