Cyber Services / All lessons / Python Programming / Python Math Functions

Python Math Functions

Module 30 of 31 · 2 min read · Free to read

MODULES ↓ Command blocks have a COPY button

Ready to crunch some numbers? Python's got your back! This guide covers essential math functions to make your code super powerful.

From basic arithmetic to advanced trigonometry, you'll learn how to solve real-world problems with Python's math capabilities.

Basic Math Power-Ups

These built-in functions work without importing any modules - perfect for everyday calculations!

abs()
Gets the absolute value of a number (makes it positive!)
number = -5
absolute_value = abs(number)
print("Absolute value:", absolute_value)  # Output: 5
pow()
Raises a number to a power (equivalent to ** operator)
base = 2
exponent = 3
result = pow(base, exponent)
print("2^3 =", result)  # Output: 8
round()
Rounds a number to a specific number of decimal places
number = 3.14159
rounded = round(number, 2)
print("Rounded:", rounded)  # Output: 3.14
max() & min()
Finds the largest and smallest values in an iterable
numbers = [10, 5, 20, 1]
print("Max:", max(numbers))  # Output: 20
print("Min:", min(numbers))  # Output: 1
sum()
Calculates the total of all numbers in an iterable
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print("Sum:", total)  # Output: 15
divmod()
Returns both quotient and remainder of division as a tuple
quotient, remainder = divmod(17, 5)
print(f"17 ÷ 5 = {quotient} remainder {remainder}")  # Output: 3 remainder 2
int() & float()
Converts values to integers or floating-point numbers
x = 3.14
print("int:", int(x))    # Output: 3
print("float:", float(5)) # Output: 5.0

Trigonometry Functions

Add import math at the beginning of your code! Remember: Python trig functions use radians, not degrees.

Tip: Use math.radians() to convert degrees to radians, and math.degrees() to convert back!
math.sin(), math.cos(), math.tan()
Calculate sine, cosine, and tangent of an angle (in radians)
import math
angle_rad = math.radians(30)
print(f"sin(30°) = {math.sin(angle_rad):.2f}")  # Output: 0.50
math.degrees() & math.radians()
Convert between degrees and radians
import math
radians = math.pi / 2
print(f"{radians} rad = {math.degrees(radians)}°")  # Output: 90.0°

Number System Conversions

Convert numbers between different bases with these handy functions:

bin()
Converts an integer to its binary representation (base-2)
number = 10
binary = bin(number)
print(f"{number} in binary: {binary}")  # Output: 0b1010
hex()
Converts an integer to its hexadecimal representation (base-16)
number = 255
hexadecimal = hex(number)
print(f"{number} in hex: {hexadecimal}")  # Output: 0xff
oct()
Converts an integer to its octal representation (base-8)
number = 8
octal = oct(number)
print(f"{number} in octal: {octal}")  # Output: 0o10

Advanced Math Functions

Unlock powerful mathematical capabilities with Python's math module:

math.ceil() & math.floor()
Round numbers up (ceil) or down (floor) to nearest integer
import math
number = 4.7
print(f"ceil({number}) = {math.ceil(number)}")  # Output: 5
math.sqrt()
Calculates the square root of a number
import math
number = 25
sqrt = math.sqrt(number)
print(f"√{number} = {sqrt}")  # Output: 5.0
math.factorial()
Calculates the factorial of a non-negative integer
import math
number = 5
fact = math.factorial(number)
print(f"{number}! = {fact}")  # Output: 120
Modules (31) ← Python Programming