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) # 36Python 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
| Operator | Name | What it does | Example expression | Result |
|---|---|---|---|---|
+ | Addition | Adds two values | 3 + 2 | 5 |
- | Subtraction | Subtracts the right operand from the left | 3 - 2 | 1 |
* | Multiplication | Multiplies values | 3 * 4 | 12 |
/ | True division | Divides and returns a floating-point result | 9 / 3 | 3.0 |
% | Modulus | Returns the remainder after division | 20 % 3 | 2 |
** | Exponentiation | Raises the left operand to a power | 3 ** 4 | 81 |
// | Floor division | Divides and rounds down | 9 // 2 | 4 |
Addition with +
The addition operator adds two numeric values:
result = 3 + 2
print(result) # 5Variables can be operands too:
apples = 4
more_apples = 3
total_apples = apples + more_apples
print(total_apples) # 7Subtraction with -
The subtraction operator subtracts the right operand from the left operand:
result = 3 - 2
print(result) # 1Operand order matters. 3 - 2 is 1, but 2 - 3 is -1.
Multiplication with *
The multiplication operator multiplies numeric operands:
result = 3 * 4
print(result) # 12A variable-based calculation can calculate a total price:
price = 8.50
quantity = 4
total = price * quantity
print(total) # 34.0True 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.5An 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 == 2More examples:
print(5 % 2) # 1
print(13 % 5) # 3Modulus 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) # 813 ** 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) # 4Because 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.0For negative results, rounding down can move to the next smaller integer:
print(-9 // 2) # -5Comparing division operators
| Expression | Meaning | Result | Result type |
|---|---|---|---|
9 / 2 | True division | 4.5 | float |
9 // 2 | Floor-divided quotient | 4 | int |
9 % 2 | Remainder after division | 1 | int |
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:
- Parentheses
- Exponentiation:
** - Multiplication, division, floor division, and modulus:
*,/,//,% - 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) # 20In 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 / 0Check 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 XORUnexpected 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) # 4Arithmetic operators also work naturally with variables:
distance = 150
hours = 3
speed = distance / hours
print(speed) # 50.0Beyond 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.