Python Math: Operators and Numbers Explained
Table of Contents
Python has excellent built-in support for math operations — from basic arithmetic to exponents to bitwise logic. You don’t need to import anything to do most math in Python, which is one reason it’s so popular for everything from backend development to data science.
All the content from our Boot.dev courses are available for free here on the blog. This one is the “Computing” chapter of Learn to Code in Python. If you want to try the far more immersive version of the course, do check it out!
How Do Integers and Floats Work in Python?
Numbers without a decimal point are integers — whole numbers, positive or negative. Numbers with a decimal point are floats.
my_int = 5
my_float = 5.5
Basic arithmetic works the way you’d expect:
2 + 1 # 3 (addition)
2 - 1 # 1 (subtraction)
2 * 2 # 4 (multiplication)
3 / 2 # 1.5 (division)
One gotcha: division with / always returns a float, even if both operands are integers. 6 / 3 gives you 2.0, not 2.
What Is Floor Division in Python?
Floor division is like normal division except the result is rounded down to the nearest integer. Use the // operator:
7 // 3 # 2 (rounded down from 2.333)
-7 // 3 # -3 (rounded down from -2.333)
Note that “rounded down” means toward negative infinity, not toward zero. That’s why -7 // 3 is -3, not -2.
How Do Exponents Work in Python?
Python has a built-in exponent operator ** — something most languages require a math library for:
3 ** 2 # 9 (three squared)
2 ** 10 # 1024 (two to the tenth power)
This is cleaner than calling a function and works with both integers and floats.
How Do You Update a Variable in Place?
It’s common to change a variable’s value based on its current value. You could write it out:
score = 4
score = score + 1 # score is now 5
But Python provides in-place operators as a shorthand:
score = 4
score += 1 # 5
score -= 2 # 3
score *= 3 # 9
score /= 3 # 3.0
Python doesn’t have ++ or -- like JavaScript or Go. Use += 1 and -= 1 instead.
What Is Scientific Notation in Python?
For very large or very small numbers, you can use scientific notation with the letter e:
print(16e3) # 16000.0
print(7.1e-2) # 0.071
The number after e tells Python how many places to move the decimal point — right for positive, left for negative. Numbers in scientific notation are always floats.
For readability with large integers, Python lets you use underscores as visual separators:
num = 16_000_000
print(num) # 16000000
How Do Logical Operators Work in Python?
Python’s logical operators — and, or, and not — work on boolean values:
True and True # True
True and False # False
True or False # True
False or False # False
not True # False
not False # True
and requires both inputs to be True. or requires at least one. not flips the value. You can nest them with parentheses:
(True or False) and False # False
The parenthesized expression evaluates first (True), then True and False gives False.
What Are Binary Numbers in Python?
Binary numbers are base-2 — they use only 0 and 1 instead of the ten symbols in decimal. Each position represents a power of 2 (ones, twos, fours, eights) instead of a power of 10.
In Python, use the 0b prefix to write a binary literal:
print(0b0001) # 1
print(0b0101) # 5
print(0b1010) # 10
To convert a binary string to an integer, use the built-in int() function with base 2:
num = int("101", 2)
print(num) # 5
How Do Bitwise Operators Work in Python?
Bitwise operators apply logical operations to each bit of a number, column by column. The two most common are & (AND) and | (OR).
Bitwise AND (&) returns 1 only where both bits are 1:
0b0101 & 0b0111 # 0b0101 → 5
Bitwise OR (|) returns 1 where either bit is 1:
0b0101 | 0b0010 # 0b0111 → 7
A practical use case: storing multiple boolean permissions in a single integer. Each bit represents a permission, and you use & to check if a specific permission is set, or | to combine permissions from multiple sources.
can_read = 0b100
can_write = 0b010
can_execute = 0b001
user_perms = can_read | can_write # 0b110
has_read = user_perms & can_read # 0b100 (truthy — has permission)
If you’re curious about how bitwise operations show up in the real world, check out our post on why XOR is important in cryptography.
What Should You Learn After Python Math?
Now that you’re comfortable with numbers and operators, the next step is learning comparisons and conditionals — using operators like ==, <, and > to make decisions in your code. From there, you’ll move into loops and lists where math operations really start to shine.
If you want to keep building your Python fundamentals, check out Learn to Code in Python on Boot.dev — or explore the full backend learning path.
Frequently Asked Questions
What is the difference between / and // in Python?
The / operator performs standard division and always returns a float. The // operator performs floor division, which rounds the result down to the nearest integer.
Does Python have a built-in exponent operator?
Yes. Use ** to raise a number to a power. For example, 3 ** 2 evaluates to 9. Most other languages require a math library for this.
What is the difference between an integer and a float in Python?
An integer is a whole number like 42 or -7 with no decimal point. A float has a decimal part like 3.14 or -0.5. Division with / always returns a float, even if both operands are integers.
How do you write binary numbers in Python?
Use the 0b prefix followed by the binary digits. For example, 0b0101 is 5 in decimal. You can use the int() function with base 2 to convert a binary string to an integer.
Related Articles
Python Functions: How to Define and Call Them
Mar 01, 2026 by lane
Functions allow you to reuse and organize code. Instead of copying and pasting the same logic everywhere, you define it once and call it whenever you need it. They’re the most important tool for writing DRY code.
Python Variables: A Complete Beginner Guide
Feb 28, 2026 by lane
Variables are how we store data as our program runs. You’re probably already familiar with printing data by passing it straight into print(), but variables let us save that data so we can reuse it and change it before printing it. This guide covers everything you need to know about Python variables: creating them, naming them, and understanding the basic data types they can hold. If you’re just getting started, you might also want to know why Python is worth learning in the first place.
Queue Data Structure in Python: Ordering at Its Best
Oct 22, 2023 by lane
A queue is an efficient collection of ordered items. New items can be added to one side, and removed from the other side.
Understanding Stacks: Python Implementation of a Core Data Structure
Oct 06, 2023 by lane
A stack is an abstract data type that serves as a collection of elements. The name “stack” originates from the analogy of items physically stacked on top of each other. Just like a stack of plates at a buffet, plates can be added, removed, and viewed from the top. However, plates further down are not immediately accessible.