Skip to main content

Arithmetic Operators in Python: Complete Guide

Python's arithmetic operators let you perform mathematical calculations with numbers. The seven core operators—addition (+), subtraction (-), multiplication (*), division (/), floor division (//), modulus (%), and exponentiation (**)—handle everything from basic sums to complex power calculations. Understanding operator precedence (PEMDAS/BODMAS) is essential to write predictable code.

Key Takeaways

  • Use +, -, * for basic math; / returns floats, // returns integers
  • Modulus (%) gives remainders; exponentiation (**) computes powers
  • Python follows PEMDAS/BODMAS: parentheses, exponents, multiply/divide, add/subtract (left to right)
  • Operator precedence can be overridden with explicit parentheses
  • All arithmetic operators work with integers, floats, and mixed numeric types

What Are the Standard Python Arithmetic Operators?

Python provides six primary arithmetic operators for basic math operations. The addition operator (+) combines two numbers, subtraction (-) finds the difference, and multiplication (*) produces a product. Each operator works with both integers and floating-point numbers, automatically promoting the result type when needed (integer operations stay integers; operations mixing floats return floats).

OperatorNameExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52.0
%Modulus10 % 31
//Floor Division10 // 33
**Exponentiation2 ** 38

How Do Division Operators Work in Python?

Python provides two division operators that behave differently. The true division operator (/) always returns a floating-point result, even when dividing two integers evenly. Floor division (//) divides and rounds down to the nearest integer, useful for operations requiring whole numbers. This distinction is crucial: in Python 2, / on integers performed floor division, but Python 3 changed this for consistency.

# True division (/) returns a float
print(10 / 3) # Output: 3.3333333333333335
print(10 / 2) # Output: 5.0
print(-7 / 2) # Output: -3.5

# Floor division (//) rounds down to nearest integer
print(10 // 3) # Output: 3
print(10 // 2) # Output: 5
print(-7 // 2) # Output: -4 (rounds toward negative infinity)

Floor division always rounds toward negative infinity, so -7 // 2 yields -4, not -3. This behavior matters in loops and data indexing.

What Is the Modulus Operator and When Do You Use It?

The modulus operator (%) returns the remainder after division, fundamental for divisibility checks, cycling through ranges, and extracting digits. When you divide 10 % 3, you get 1 because 3 goes into 10 three times with 1 left over. Modulus is essential in loops for every-nth-element patterns and in data structures like hash tables.

# Basic modulus examples
print(10 % 3) # Output: 1
print(10 % 5) # Output: 0 (10 is evenly divisible by 5)
print(-10 % 3) # Output: 2 (Python uses floored division rules)

# Practical use: check if number is even
if num % 2 == 0:
print("Even")

# Practical use: cycle through a range
for i in range(10):
if i % 3 == 0:
print(f"{i} is divisible by 3")

# Practical use: get the last digit of a number
last_digit = 12345 % 10 # Output: 5

What Does Exponentiation Do and How Is It Different From Multiplication?

Exponentiation (**) raises a base number to a power, meaning repeated multiplication. 2 ** 3 equals 8 because 2 * 2 * 2 = 8. Unlike multiplication (which combines two values once), exponentiation repeats the multiplication. This operator is crucial for calculations involving growth rates, geometric sequences, and scientific formulas.

# Exponentiation examples
print(2 ** 3) # Output: 8 (2 * 2 * 2)
print(5 ** 2) # Output: 25 (5 * 5, aka "5 squared")
print(10 ** 3) # Output: 1000 (10 * 10 * 10, aka "10 cubed")

# Negative exponents give fractions
print(2 ** -1) # Output: 0.5 (1/2)
print(2 ** -2) # Output: 0.25 (1/4)

# Square root using fractional exponent
print(9 ** 0.5) # Output: 3.0
print(27 ** (1/3)) # Output: 3.0 (cube root)

How Does Python Order Operations When Multiple Operators Are Used?

Python follows PEMDAS/BODMAS, the standard order of operations: parentheses, exponentiation, multiplication/division (left to right), then addition/subtraction (left to right). This ensures 2 + 3 * 4 evaluates to 14, not 20. When operators have equal precedence, Python evaluates left to right, so 10 - 5 - 2 equals 3, not 7.

# Example 1: Standard precedence
result = 2 + 3 * 4 # Multiply first: 3 * 4 = 12, then add: 2 + 12 = 14
print(result) # Output: 14

# Example 2: With exponentiation
result = 2 + 3 ** 2 # Exponent first: 3 ** 2 = 9, then add: 2 + 9 = 11
print(result) # Output: 11

# Example 3: Left-to-right for equal precedence
result = 10 - 5 - 2 # Left to right: (10 - 5) - 2 = 5 - 2 = 3
print(result) # Output: 3

# Example 4: Parentheses override precedence
result = (2 + 3) * 4 # Parentheses first: 2 + 3 = 5, then multiply: 5 * 4 = 20
print(result) # Output: 20

# Example 5: Complex expression
result = (10 + 5) * 2 ** 2 / (3 - 1) # (15) * 4 / 2 = 60 / 2 = 30.0
print(result) # Output: 30.0

Complete Operator Precedence Reference

Python's precedence levels from highest to lowest:

  1. () – Parentheses (explicit grouping)
  2. ** – Exponentiation (right-associative: 2 ** 3 ** 2 = 2 ** (3 ** 2) = 512)
  3. *, /, //, % – Multiplication, division, floor division, modulus (left-associative)
  4. +, - – Addition and subtraction (left-associative)

Frequently Asked Questions

What's the difference between / and // in Python?

The / operator (true division) always returns a float, even when dividing evenly: 10 / 2 = 5.0. The // operator (floor division) returns an integer by rounding down: 10 // 3 = 3. In Python 2, / on integers performed floor division, but Python 3 changed it for consistency across numeric types.

How do I compute square roots and fractional powers in Python?

Use the exponentiation operator with a fractional exponent: 9 ** 0.5 gives 3.0 (square root), and 8 ** (1/3) gives 2.0 (cube root). For more advanced functions, import the math module: import math; math.sqrt(9) returns 3.0. The math module also provides math.pow(), though the ** operator is preferred for clarity and performance.

When should I use parentheses in arithmetic expressions?

Use parentheses to override the default order of operations or to clarify intent, even when not strictly necessary. For example, (10 + 5) * 2 explicitly groups the addition first. In complex expressions like (10 + 5) * 2 ** 2 / (3 - 1), parentheses improve readability and reduce errors. Python's precedence is predictable, but parentheses signal intent to future readers.

Can I use arithmetic operators with strings or other types?

The + operator concatenates strings: "Hello" + " " + "World" = "Hello World". The * operator repeats strings: "a" * 3 = "aaa". Other operators (-, /, %, **, //) raise TypeError on strings. You cannot subtract strings, for instance. For numeric operations on non-numeric types, convert first: int("42") + 5 = 47.

How does Python handle negative numbers with modulus?

Python's modulus always returns a result with the same sign as the divisor (using floored division). So -10 % 3 = 2 (not -1), because Python computes it as -10 = 3 * (-4) + 2. This differs from some other languages. To get the mathematical remainder, use abs(a) % abs(b) and adjust the sign manually if needed, or use the math.remainder() function in Python 3.7+.

Further Reading