Python Arithmetic Operators: +, -, *, /, //, %, and **

Python arithmetic operators let you add, subtract, multiply, divide, take remainders, and raise numbers to powers. They are the basic operators for numeric calculations, and in Python 3 they follow a few rules that can surprise beginners, especially around division and precedence. This article shows how each operator works, how Python chooses the result type, and the mistakes that often cause wrong answers.

Difficulty: Beginner

Helpful to know first: basic Python syntax, variables, and how integers and floating-point numbers store values.

How Python Arithmetic Operators Work

Python arithmetic operators are symbols that tell Python to do a calculation on numbers. The most common ones are +, -, *, /, //, %, and **.

Here is the smallest useful example:

Python
print(2 + 3)   # Output: 5
print(7 - 4)   # Output: 3
print(6 * 5)   # Output: 30

Each operator works on the values to its left and right. Python evaluates the expression, then returns the result as a new value. That result can be stored in a variable, printed, or used in a larger expression.

Arithmetic operators do not change the original numbers. They produce a new result value.

Using Each Arithmetic Operator in Python

Addition with +

Addition combines numbers. It works with integers and floats, and it keeps the expected numeric meaning.

Python
total = 10 + 4
print(total)  # Output: 14

You will also see + used with strings, but that is not arithmetic. With numbers, it means addition.

Subtraction with -

Subtraction removes one value from another.

Python
distance_left = 18 - 5
print(distance_left)  # Output: 13

It also works with negative numbers, which is useful for temperature changes, balances, and offsets.

Multiplication with *

Multiplication repeats addition conceptually, but Python performs it directly.

Python
print(6 * 7)  # Output: 42

A common beginner surprise is that * also repeats sequences such as strings and lists. For numbers, it is multiplication.

True division with /

Division with / always returns a float in Python 3, even when the result is a whole number.

Python
print(8 / 2)  # Output: 4.0
print(7 / 2)  # Output: 3.5

This is one of the most important Python arithmetic rules. If you want a floating-point answer, use /.

Floor division with //

Floor division divides and then rounds down to the nearest whole number. For positive numbers, that often looks like dropping the decimal part. For negative numbers, it rounds toward negative infinity, which is not the same as truncating.

Python
print(7 // 2)   # Output: 3
print(-7 // 2)  # Output: -4

Use // when you want the quotient as an integer-like result and you accept floor behavior for negatives.

Remainder with %

The modulo operator gives the remainder after division. It is useful for checking whether a number is even, wrapping values around a range, and working with repeating cycles.

Python
print(7 % 2)  # Output: 1
print(12 % 5) # Output: 2

Modulo and floor division are related. Python guarantees that for integers, a == (a // b) * b + (a % b) when b is not zero.

Exponentiation with **

Exponentiation raises a number to a power.

Python
print(2 ** 3)  # Output: 8
print(5 ** 0)  # Output: 1

This operator has higher precedence than multiplication and division, so it often changes how an expression is grouped.

How Python Chooses the Result Type

Python does not always keep the same numeric type after a calculation. The type depends on the operators and the values involved.

Python
print(type(3 + 2))    # Output: <class 'int'>
print(type(3 / 2))    # Output: <class 'float'>
print(type(3 // 2))   # Output: <class 'int'>
print(type(3.0 // 2)) # Output: <class 'float'>

When both inputs are integers, many operators return an integer. True division always returns a float. Floor division returns an integer only when both inputs are integers and the result has no fractional part in its internal representation.

If you need to keep decimal precision for money or scientific data, think carefully before switching between int and float.

Operator Precedence and Grouping in Python

Python follows precedence rules when several arithmetic operators appear in one expression. Exponentiation happens before multiplication, division, floor division, and modulo. Those happen before addition and subtraction.

Python
print(2 + 3 * 4)   # Output: 14
print((2 + 3) * 4) # Output: 20
print(2 ** 3 * 4) # Output: 32

Parentheses let you override the default order. When an expression looks confusing, parentheses make the meaning clear to both Python and readers.

Write parentheses for clarity whenever the result would be easy to misread. Clear code matters more than relying on precedence rules.

Worked Arithmetic Examples in Real Python Code

Calculating a shopping total

To compute a price, multiply quantity by unit cost and then add tax or other charges. This is a normal use of arithmetic operators in real programs.

Python
price_per_item = 12.50
quantity = 3
tax_rate = 0.08

subtotal = price_per_item * quantity
tax = subtotal * tax_rate
total = subtotal + tax

print(round(total, 2))  # Output: 40.5

This example shows how arithmetic expressions are often chained together. Python computes each intermediate value before producing the final result.

Finding how many full groups fit

Floor division is useful when you need a whole number of groups, such as boxes, pages, or hours.

Python
items = 27
per_box = 5

boxes_needed = items // per_box
leftover = items % per_box

print(boxes_needed)  # Output: 5
print(leftover)      # Output: 2

This pair of operators is common in scheduling and pagination. One tells you the number of complete groups, and the other tells you what is left over.

Wrapping around a range

Modulo is the usual way to keep a value inside a fixed range.

Python
hours = 23
added_hours = 5

new_hour = (hours + added_hours) % 24
print(new_hour)  # Output: 4

Without modulo, the hour would keep growing. With modulo, the result stays inside the 0–23 range.

Repeating a short numeric pattern

Exponentiation and multiplication often appear together in formulas. For example, area calculations and powers use arithmetic operators in a direct way.

Python
side = 9
area = side ** 2
print(area)  # Output: 81

This is easier to read than writing side * side when the goal is clearly “square this value.”

How Python Evaluates Arithmetic Expressions in Detail

Python evaluates arithmetic expressions from left to right within the same precedence level. That means 8 - 3 - 2 is treated as (8 - 3) - 2, not 8 - (3 - 2).

Python
print(8 - 3 - 2)   # Output: 3
print(8 - (3 - 2)) # Output: 7

Exponentiation is a little different: it groups from right to left. So 2 ** 3 ** 2 means 2 ** (3 ** 2).

Python
print(2 ** 3 ** 2)  # Output: 512

That result often surprises beginners who expect (2 ** 3) ** 2, which would be 64. If you need a different order, write parentheses.

Division by zero raises an exception instead of producing a number.

Python
print(10 / 0)

That code stops with ZeroDivisionError: division by zero. The same is true for // and % when the right-hand side is zero.

In Python, integer arithmetic is not limited by fixed-size overflow in normal use. Large integers can grow as needed, although very large calculations still use more time and memory.

Predict the Output

Try these short examples before opening the answers. They test the mistakes beginners make most often with division, precedence, and floor division.

Question 1

Python
print(9 // 2)
print(9 / 2)

What does this print?

Show answer

9 // 2 prints 4 because floor division rounds down. 9 / 2 prints 4.5 because true division returns a float.

Terminal
# Output:
# 4
# 4.5

Question 2

Python
print(2 ** 3 * 4)

What does this print?

Show answer

Exponentiation happens before multiplication, so Python calculates 2 ** 3 first, which is 8. Then it multiplies by 4 to get 32.

Terminal
# Output: 32

Find the Bug

One of the most common arithmetic bugs is using the wrong division operator when you need a whole number count. Find the bug in this code that is supposed to count full boxes.

Python
items = 27
per_box = 5
boxes = items / per_box
print(f"Boxes needed: {boxes}")
Show answer

The bug is /. It returns a float, so the code prints 5.4 instead of the number of full boxes. For counting complete boxes, use floor division with //.

Python
items = 27
per_box = 5
boxes = items // per_box
print(f"Boxes needed: {boxes}")  # Output: Boxes needed: 5

The fixed version works because // gives the quotient as a whole-number result for this problem.

Common Mistakes with Python Arithmetic Operators

Using true division when you need a whole number

Beginners often expect / to behave like integer division. In Python 3, it does not.

Problem: This code produces a float, which is wrong when you need a count or index.

Python
page_size = 10
items = 23
pages = items / page_size
print(pages)  # Output: 2.3

Fix: Use // for whole-number quotient logic.

Python
page_size = 10
items = 23
pages = items // page_size
print(pages)  # Output: 2

The corrected version works because floor division returns the number of full groups.

Forgetting that floor division rounds down for negative numbers

Many people expect // to simply remove the decimal part. That is only true for positive numbers.

Problem: This code assumes -7 // 2 becomes -3, but Python floors the result to -4.

Python
print(-7 // 2)  # Output: -4

Fix: If you need truncation toward zero, convert after true division with care, or redesign the calculation so negative values are handled explicitly.

Python
print(int(-7 / 2))  # Output: -3

The corrected version works because int() truncates toward zero, which is different from floor division.

Misreading operator precedence

When operators are mixed, Python may group them differently from what the eye expects.

Problem: This code calculates 2 + (3 * 4), not (2 + 3) * 4.

Python
print(2 + 3 * 4)  # Output: 14

Fix: Use parentheses when the intended order matters.

Python
print((2 + 3) * 4)  # Output: 20

The corrected version works because parentheses force Python to evaluate the addition first.

Dividing by zero

Arithmetic expressions fail immediately if the right-hand side of division, floor division, or modulo is zero.

Problem: Python cannot divide by zero, so this raises an exception instead of returning a value.

Python
print(10 % 0)

Fix: Check the divisor before the calculation.

Python
divisor = 0
if divisor != 0:
    print(10 % divisor)
else:
    print("Cannot divide by zero")  # Output: Cannot divide by zero

The corrected version works because it avoids the invalid operation entirely.

When to Use Each Operator, and When to Use Something Else

Choose the operator that matches the meaning of your problem, not just the one that produces a number.

GoalUse this operatorWhen it is the right choice
Add values+You want the total or combined amount.
Subtract values-You want the difference or change.
Multiply values*You want repeated scaling or a product.
Get a decimal result/You need the exact quotient as a float.
Get whole groups//You need the number of complete groups.
Get the leftover%You need the remainder or a wrapped value.
Raise to a power**You need squaring, cubing, or another exponent.

Sometimes there is a better tool than arithmetic. For example, if you are formatting money, the decimal module can avoid some floating-point surprises. If you need to count or group items, // and % are usually enough.

The arithmetic operators themselves are simple. Most mistakes come from choosing the wrong operator for the meaning of the problem.

Interview Questions About Python Arithmetic Operators

Why does Python 3 use / for float results?

Python 3 made / true division so division always behaves consistently and returns a real quotient. If you want whole-number division, Python gives you //.

Why does -7 // 2 equal -4?

Because floor division rounds down toward negative infinity. The floor of -3.5 is -4, not -3.

Why is 2 ** 3 ** 2 not 64?

Exponentiation groups from right to left. Python reads it as 2 ** (3 ** 2), which is 2 ** 9 or 512.

Try It Yourself

Use arithmetic operators to calculate a small order total.

  • Set price = 8.75 and quantity = 4.
  • Compute the subtotal with multiplication.
  • Apply a 5% tax using addition and multiplication.
  • Print the subtotal and the final total, rounded to 2 decimal places.

Expected output:

Python
Subtotal: 35.0
Total: 36.75

Hint: Store the subtotal in a variable first, then calculate tax from that subtotal.

Show solution
Python
price = 8.75
quantity = 4
tax_rate = 0.05

subtotal = price * quantity
tax = subtotal * tax_rate
total = subtotal + tax

print(f"Subtotal: {subtotal:.1f}")
print(f"Total: {total:.2f}")  # Output: Subtotal: 35.0 / Total: 36.75

Summary

  • +, -, *, /, //, %, and ** are Python's main arithmetic operators.
  • / always returns a float in Python 3.
  • // does floor division, not simple truncation.
  • % gives the remainder and is useful for wrapping values and checking divisibility.
  • Exponentiation with ** binds more tightly than multiplication and division.
  • Parentheses are the safest way to make arithmetic expressions read the way you intend.

Next, learn Python comparison operators so you can test the results of these calculations with ==, <, and >.