Python Tutorial

How To Do Math in Python 3 with Operators

Comprehensive, original guide to python operators: arithmetic, unary, floor division, modulo, exponentiation, operator precedence (PEMDAS), and compound assignment with examples for Python 3.

Drake Nguyen

Founder · System Architect

3 min read
How To Do Math in Python 3 with Operators
How To Do Math in Python 3 with Operators

Introduction

Working with numbers is central to programming. Whether youre calculating screen dimensions, handling currency, tracking time, or manipulating coordinates in a game, youll rely on python operators to perform these tasks. This guide explains the common python arithmetic operators, demonstrates how math operators in python behave with integers and floats, and shows best practices for Python 3 operators.

Prerequisites

To follow the examples, have Python 3 installed and an interactive shell or code editor ready. Examples use basic numeric types: integers (whole numbers) and floats (numbers with a decimal point). Understanding the difference between integer vs float python is helpful when predicting results from division and modulo operations.

Overview of Operators

An operator is a symbol or token that tells Python to perform a computation. Below are the most-used math operators in Python and what they Netalith.

  • x + y — addition
  • x - y — subtraction
  • +x, -x — unary identity and negation (python unary operators)
  • x * y — multiplication
  • x / y — true division (always returns a float in python 3)
  • x // y — floor division (python floor division)
  • x % y — modulo / remainder (python modulo operator)
  • x ** y — exponentiation (python exponent operator)

Addition and Subtraction

These operators behave as in math. Use Python as a quick calculator to test expressions. Results follow numeric types: if any operand is a float, the result is a float.

# addition
print(1 + 5)           # 6

# variables
a = 88
b = 103
print(a + b)           # 191

# mixed sign
c = -36
d = 25
print(c + d)           # -11

# floats
e = 5.5
f = 2.5
print(e + f)           # 8.0

Unary Arithmetic Operations

The unary + and - apply to a single value. +x returns the value unchanged; -x flips the sign. These are useful for clarifying intent or adjusting signs programmatically.

i = 3.3
print(+i)              # 3.3

j = -19
print(+j)              # -19

print(-i)              # -3.3
print(-j)              # 19

Multiplication and Division

Use * for multiplication and / for true division (python 3 operators always return float for /). For whole-number quotients use floor division // to get the integer part of the quotient.

# multiplication
k = 100.1
l = 10.1
print(k * l)          # may show floating-point precision

# true division (always float in Python 3)
m = 80
n = 5
print(m / n)          # 16.0

# floor division
print(100 // 40)      # 2 (floor division)

Modulo (Remainder)

The modulo operator % returns the remainder after division. It is handy when checking divisibility, cycling indexes, or implementing wrap-around logic.

o = 85
p = 15
print(o % p)          # 10 (remainder)

# floats also supported
q = 36.0
r = 6.0
print(q % r)          # 0.0

Exponentiation (Power)

The ** operator raises a base to an exponent. This is the python exponentiation operator used for powers and many math formulas.

print(5 ** 3)        # 125
s = 52.25
t = 7
print(s ** t)         # large float

Operator Precedence

Python evaluates expressions according to operator precedence (similar to PEMDAS/BODMAS). Exponents and multiplication/division are evaluated before addition/subtraction. Use parentheses to make the intended order explicit.

u = 10 + 10 * 5      # multiplication first -> 60
print(u)

u = (10 + 10) * 5     # parentheses change order -> 100
print(u)
Remember: operator precedence in python follows common math rules, but using parentheses improves readability and reduces errors (python operator precedence pemdas).

Assignment and Compound Assignment Operators

The equals sign assigns values. Compound assignment operators combine an operation and assignment (+=, -=, *=, /=, //=, **=, %=) and are frequently used in loops and accumulators.

w = 5
w += 1                # same as w = w + 1
print(w)              # 6

# common compound examples
y = 4
y *= 2                # y = y * 2
y -= 1                # y = y - 1
y //= 3               # floor divide then assign
y %= 3                # remainder then assign
print(y)

Practical Notes

  • Division differences: use / for float results and // when you want integer-style division (difference between / and // in python 3).
  • Floating-point arithmetic can show precision artifacts; consider the decimal module for exact decimal arithmetic.
  • When mixing int and float, Python promotes to float (python integer division vs float division).

Conclusion

Mastering python operators—especially python arithmetic operators and assignment operators—lets you perform reliable numeric computations in scripts and applications. Practice examples like those above in an interactive shell to become comfortable with operator behavior, precedence, and the difference between integers and floats in Python.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.