Python - Functions - Maths

abs(): Returns the absolute value of a number.

x = -3.5
print(abs(x))  # Output: 3.5

pow(): Raises the first argument to the power of the second argument.

x = 2
y = 3
print(pow(x, y))  # Output: 8

round(): Rounds a number to the nearest integer, or to a specified number of decimal places.

x = 3.14159
print(round(x))  # Output: 3
print(round(x, 2))  # Output: 3.14

max(): Returns the largest of two or more numbers.

x = 5
y = 10
z = 7
print(max(x, y, z))  # Output: 10

min(): Returns the smallest of two or more numbers.

x = 5
y = 10
z = 7
print(min(x, y, z))  # Output: 5

sum(): Returns the sum of a sequence of numbers.

numbers = [1, 2, 3, 4, 5]
print(sum(numbers))  # Output: 15

sqrt(): Returns the square root of a number.

import math
x = 16
print(math.sqrt(x))  # Output: 4.0

sin(): Returns the sine of a given angle (in radians).

import math
x = math.pi / 2
print(math.sin(x))  # Output: 1.0

cos(): Returns the cosine of a given angle (in radians).

import math
x = math.pi / 2
print(math.cos(x))  # Output: 6.123233995736766e-17

tan(): Returns the tangent of a given angle (in radians).

import math
x = math.pi / 4
print(math.tan(x))  # Output: 0.9999999999999999