Mastering Math in Python: From Basics to Built-In Functions
Learn how to do math in Python using basic operators, built-in functions, and the math module. Perfect for beginners and leveling up your code.
Imagine you're playing your favorite video game. You hold your pistol, shotgun, or any other weapon of choice. The fight is fierce, and you've found just the right moment to click and shoot. At that moment, you stand up from cover, shoot, and hit your target multiple times. One of those hits is a headshot, another misses, and some others hit different parts of the enemy's body.
How does the game know what to consider a headshot, a miss, or a regular shot? The answer: Mathematics.
If you've never dipped your toes into how your favorite games are made, you might not realize the irreplaceable role math plays in making the game as good as it is. And it’s not just games — math powers everything from salary calculators to flight software.
So today, we're going to honor math and help you master the ways it can be used in Python, which is the focus of these posts for the near future.
Let’s dive in.
Basic Math Operators
Python comes with built-in math operators. You’ve got addition, subtraction, multiplication, and division:
res = 1 + 1 # Addition
res = 5 - 2 # Subtraction
res = 3 / 2 # Division
res = 3 // 2 # Floor Division
res = 3 * 2 # Multiplication
Need the remainder or want to raise numbers to a power?
res = 3 % 2 # Modulo – returns the remainder (1)
res = 2 ** 3 # Power – 2 raised to the 3rd power = 8
If you’ve already installed Python on your computer, open a terminal, type python
, and try these out. You’ll learn faster by messing around with real numbers.
3 / 3
will return 1.0
, not 1
.Order of Operations (PEMDAS in Python)
Just like in school math, the order in which operations are evaluated matters:
res1 = 3 + 2 * 5 # 13
res2 = (3 + 2) * 5 # 25
Python follows the standard PEMDAS order: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction — from left to right.
So if your math expression gets a little complex, add parentheses where needed. Your future self will thank you.
Working With Different Number Types
Python handles several number types:
- Integers (
int
) — whole numbers:1
,100
,-42
- Floats (
float
) — numbers with decimals:1.0
,3.14
,-0.5
- Complex (
complex
) — rarely used unless you're doing advanced math:1 + 2j
You can mix types, and Python will often convert them automatically:
res = 1 + 1.5 # 2.5 (int + float = float)
The Math Module
The math
module gives you access to tons of useful tools that go beyond the basics.
Yes, you could calculate a square root manually:
square_root = 16 ** 0.5
But this is way cleaner:
import math
num = 16
result = math.sqrt(num)
Other goodies include:
math.floor()
,math.ceil()
,math.trunc()
math.exp()
,math.log()
math.pi
,math.e
, and more
You can find the full list in the official docs, and I recommend exploring them as you grow.
Rounding Numbers
Sometimes you don’t want every decimal point — just a clean, usable number.
round(3.14159) # 3
round(3.14159, 2) # 3.14
import math
math.floor(3.7) # 3
math.ceil(3.1) # 4
round()
gets you the closest number.floor()
always rounds down.ceil()
always rounds up.
Useful for UI displays, pricing, or mechanics like “how many items per row.”
Absolute Values and Sign Handling
Need to ignore negative signs and just get the value?
abs(-5) # 5
abs(5) # 5
Need to copy the sign of one number onto another?
import math
math.copysign(3, -1) # -3.0
math.copysign(3, 1) # 3.0
This is handy in simulations or physics-based animations where direction matters.
Dealing With Infinity and NaN
Python supports special float values for things that "break" regular math:
float('inf') # Positive infinity
float('-inf') # Negative infinity
float('nan') # Not a Number
Check them like this:
import math
math.isinf(float('inf')) # True
math.isnan(float('nan')) # True
These show up more than you’d think — especially when using external data, or doing weird math.
Bonus: Build-in Functions You Might Overlook
Here are some built-ins that save time and make your code cleaner:
sum([1, 2, 3]) # 6
max([1, 2, 3]) # 3
min([1, 2, 3]) # 1
pow(2, 3) # 8
divmod(9, 4) # (2, 1)
divmod() is a hidden gem. It gives you both the quotient and the remainder at once — great for packing, pagination, and layout math.
When to Go Beyond: A Peek at What's Next
Everything you've seen so far is built into Python. But if you find yourself working on advanced use cases, here are a few tools you’ll want to explore:
decimal
— for high-precision decimal math (great for money)fractions
— lets you use fractions like1/3
without rounding issuesnumpy
— the go-to library for fast, powerful number crunchingsympy
— symbolic math (algebra, calculus, solving equations)
I'll cover those in a future post. For now, you've got a solid foundation.
Thanks For Reading!
Whether you're building a game, crunching numbers in your app, or just trying to figure out what 3 / 2
really means — math is part of the magic. Python makes it simple, and now so can you.
Play with these tools. Break things. Tweak numbers and see what happens.
You're not just learning Python — you're leveling up your skills as a developer and a builder.