Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division. In Python (>3.5) there are 7 arithmetic operators defined:

OperatorNameExampleOutput (x=7, y=2)
+Additionx + y9
-Subtractionx - y5
*Multiplicationx * y14
/Divisionx / y3.5
//Floor divisionx // y3
%Modulusx % y1
**Exponentiationx ** y49

Addition

The + (addition) operator yields the sum of its arguments.

The arguments must either both be numbers or both be sequences of the same type. Only in the former case, the numbers are converted to a common type and a arithmetic addition is performed. In the latter case, the sequences are concatenated, e.g.

a = [1, 2, 3]
b = [4, 5, 6]
assert (a + b) == [1, 2, 3, 4, 5, 6]

Subtraction

The - (subtraction) operator yields the difference of its arguments. The numeric arguments are first converted to a common type. Note: In contrast to the addition, the subtraction operator cannot be applied to sequences.

Multiplication

The * (multiplication) operator yields the product of its arguments. The arguments must either both be numbers, or one argument must be an integer and the other must be a sequence. In the former case, the numbers are converted to a common type and then multiplied together. In the latter case, sequence repetition is performed; e.g.

a = [1, 2]
assert (3 * a) == [1, 2, 1, 2, 1, 2]

Note: a negative repetition factor yields an empty sequence; e.g.

a = 3 * [1, 2]
assert (-2 * a) == []

Division & Floor division

The / (division) and // (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Note: Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the floor function applied to the result. Division by zero raises a ZeroDivisionError exception.

Modulus

The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may even be floating point numbers, e.g.,

import math 
assert math.isclose(3.14 % 0.7, 0.34)
# since
assert math.isclose(3.14, 4*0.7 + 0.34) 

The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand.

Note: As you may have noticed in the listing above, we did not use the comparison operator == to test the equality of two floats. Instead we imported the math package and used the built-in isclose function. If you want to read more detailed information about float representation errors the following blog post can be recommended.

Exponentiation

The ** (power) operator has the same semantics as the built-in pow() function, when called with two arguments: it yields its left argument raised to the power of its right argument. The numeric arguments are first converted to a common type. The result type is that of the arguments after coercion; if the result is not expressible in that type (as in raising an integer to a negative power) the result is expressed as a float (or complex). In an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands), e.g.

assert 2**2**3 == 2**(2**3) == 2**8 == 256