Python Math Module Functions Cheat Sheet
Quick reference guide for the Python math standard library. Learn how to use trigonometric, logarithmic, rounding, and arithmetic functions.
Rounding & Basic Arithmetic
Standard mathematical functions for rounding, signs, and basic mathematical operations.
| Method | Syntax | Description |
|---|---|---|
| ceil() | math.ceil(x) | Rounds number x up to the nearest integer. |
| floor() | math.floor(x) | Rounds number x down to the nearest integer. |
| trunc() | math.trunc(x) | Truncates fractional parts, returning only the integer part of x. |
| abs() | abs(x) | Built-in function that returns the absolute value of x. |
| fmod() | math.fmod(x, y) | Returns the precise floating-point remainder of x/y. |
| gcd() | math.gcd(a, b) | Returns the greatest common divisor of integers a and b. |
Powers, Roots & Logarithms
Exponential and logarithmic computations.
| Method | Syntax | Description |
|---|---|---|
| sqrt() | math.sqrt(x) | Returns the square root of number x. |
| pow() | math.pow(x, y) | Returns x raised to the power of y as a float. |
| exp() | math.exp(x) | Returns e raised to the power of x. |
| log() | math.log(x, [base]) | Returns the logarithm of x to the specified base (defaults to natural log). |
| log10() | math.log10(x) | Returns the base-10 logarithm of x. |
Frequently Asked Questions
What is the difference between math.ceil() and math.floor()?
math.ceil() rounds a number up to the nearest integer, whereas math.floor() rounds a number down to the nearest integer.
Why should I use math.isclose() instead of checking equality directly?
Floating-point arithmetic has precision limits. math.isclose() compares two values with a tolerance check to see if they are approximately equal.
Keep Learning
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Functions
Master Python functions. Learn how to write reusable code using def, define arguments, return values, and understand scope locally and globally.
How to Use Lambda Functions in Python
Learn how to use lambda functions in Python. Master anonymous functions, inline callbacks, and sorting list data with lambda keys.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.