Python online course

Python Arithmetic Operators

Learn Python arithmetic operators for addition, subtraction, multiplication, division, remainders, powers, and floor division with practical examples.

What are arithmetic operators?

An arithmetic operator is a symbol used to perform a mathematical operation on one or more values. The values are called operands. An expression is code that Python evaluates to produce a value.

For example, in 3 + 2, + is the operator, while 3 and 2 are the operands. Python evaluates the expression and produces 5.

Expressions can contain numeric literals, variables, and the results of other expressions:

price = 12
quantity = 3
total = price * quantity

print(total)  # 36

Python arithmetic operators are built in, so no import or configuration is required. You can try these expressions in the Python interactive prompt or in a script.

Python arithmetic operators

OperatorNameWhat it doesExample expressionResult
+AdditionAdds two values3 + 25
-SubtractionSubtracts the right operand from the left3 - 21
*MultiplicationMultiplies values3 * 412
/True divisionDivides and returns a floating-point result9 / 33.0
%ModulusReturns the remainder after division20 % 32
**ExponentiationRaises the left operand to a power3 ** 481
//Floor divisionDivides and rounds down9 // 24

Addition with +

The addition operator adds two numeric values:

result = 3 + 2
print(result)  # 5

Variables can be operands too:

apples = 4
more_apples = 3
total_apples = apples + more_apples

print(total_apples)  # 7

Subtraction with -

The subtraction operator subtracts the right operand from the left operand:

result = 3 - 2
print(result)  # 1

Operand order matters. 3 - 2 is 1, but 2 - 3 is -1.

Multiplication with *

The multiplication operator multiplies numeric operands:

result = 3 * 4
print(result)  # 12

A variable-based calculation can calculate a total price:

price = 8.50
quantity = 4
total = price * quantity

print(total)  # 34.0

True division with /

The / operator divides the left operand by the right operand. It is called true division because it returns a float, even when the result has no fractional part.

print(9 / 3)  # 3.0
print(9 / 2)  # 4.5

An int is a whole-number numeric type, such as 4 or -2. A float is a floating-point number that can represent fractional values, such as 4.5 or 3.0.

Modulus with %

The modulus operator returns the remainder after division. The remainder is the amount left over when the divisor does not divide evenly.

For 20 % 3, the divisor is 3. Three fits into twenty six complete times, giving a quotient of 6. Six groups of three use 18, leaving 2:

3 * 6 + 2 = 20
20 % 3 == 2

More examples:

print(5 % 2)   # 1
print(13 % 5)  # 3

Modulus is useful for checking whether a number is even or odd. An even number has no remainder when divided by two:

number = 14

if number % 2 == 0:
    print("even")
else:
    print("odd")

Modulus can also repeat values in cycles. For example, position % 4 always produces a repeating range of remainder values from 0 through 3 for nonnegative positions.

Exponentiation with **

The exponentiation operator raises the left operand, called the base, to the power of the right operand:

result = 3 ** 4
print(result)  # 81

3 ** 4 means 3 * 3 * 3 * 3, which is 81. It is different from multiplication: 3 * 4 is 12, while 3 ** 4 is 81.

Floor division with //

The floor division operator divides two values and rounds the quotient down to the next lower integer value. “Down” means toward negative infinity, not simply toward zero.

print(9 // 2)  # 4

Because 9 / 2 is 4.5, floor division produces 4. Float operands can also be used, and the result can remain a float:

print(9.0 // 2)  # 4.0

For negative results, rounding down can move to the next smaller integer:

print(-9 // 2)  # -5

Comparing division operators

ExpressionMeaningResultResult type
9 / 2True division4.5float
9 // 2Floor-divided quotient4int
9 % 2Remainder after division1int

Use / when you need the ordinary quotient, including fractional values. Use // when you specifically need the quotient rounded down, and use % when you need what remains.

Expression evaluation and operator precedence

Python follows operator precedence, the rules that determine which operations are evaluated first. At a beginner level, the order is:

  1. Parentheses
  2. Exponentiation: **
  3. Multiplication, division, floor division, and modulus: *, /, //, %
  4. Addition and subtraction: +, -

For operators at the same level, Python generally evaluates from left to right. Parentheses make the intended calculation explicit:

first = 2 + 3 * 4
second = (2 + 3) * 4

print(first)   # 14
print(second)  # 20

In the first expression, multiplication happens before addition. In the second, parentheses make the addition happen first.

Arithmetic results can be integers or floats. Addition, subtraction, and multiplication of integers usually produce integers, while true division always produces a float. Operations involving a float commonly produce a float.

Common mistakes and safe use

Division by zero

The right operand of /, //, or % cannot be zero. Python raises a ZeroDivisionError:

# Raises ZeroDivisionError
result = 10 / 0

Check that the divisor is nonzero before calculating:

divisor = 0

if divisor != 0:
    result = 10 / divisor
else:
    print("The divisor must not be zero")

Confusing / and //

/ performs true division and can produce decimals. // performs floor division. Choosing the wrong operator can produce an unexpected result.

Confusing % with a percentage

In Python arithmetic, % is the modulus operator. It returns a remainder; it does not directly mean “percent.” For example, 13 % 5 is 3.

Using ^ for powers

Python uses ** for exponentiation. The ^ symbol is a bitwise XOR operator, not the power operator:

print(3 ** 2)  # 9
print(3 ^ 2)   # 1, bitwise XOR

Unexpected results from mixed expressions

If multiplication or division occurs before addition or subtraction, the result may differ from a left-to-right reading. Use parentheses around the operation that should happen first.

Arithmetic in a small program

These assignments show several arithmetic expressions in a program:

result = 3 + 2
remainder = 20 % 3
power = 3 ** 4
whole_groups = 9 // 2

print(result)       # 5
print(remainder)    # 2
print(power)        # 81
print(whole_groups) # 4

Arithmetic operators also work naturally with variables:

distance = 150
hours = 3
speed = distance / hours

print(speed)  # 50.0

Beyond basic arithmetic operators

Python's built-in operators cover common calculations. For more advanced mathematical tasks, the standard library includes modules such as math. The module importing lesson explains how to make library functionality available. Keep the basic operators in this lesson in mind as the foundation for larger calculations.