Python - Built in Functions Part 3: Mathematical and Logical Functions
These functions perform calculations and evaluate conditions.
Common Functions:
abs(): Returns the absolute value.
round(): Rounds a number to the nearest integer or specified decimal places.
pow(): Raises a number to a power.
min()/max(): Finds the smallest or largest value in an iterable.
all()/any(): Logical evaluation of iterables.
Examples:
Finding the absolute value:
num = -10
print(abs(num)) # Output: 10
Rounding a number:
num = 3.14159
print(round(num, 2)) # Output: 3.14
Logical evaluation with all():
conditions = [True, True, False]
print(all(conditions)) # Output: False
Explanation:
Mathematical and logical functions simplify calculations and decision-making in code. For example, abs() ensures non-negative values in computations, and all() evaluates whether all elements in a collection meet a specific condition.