The Python Language Reference, Release 3.9.5 6.7. Binary arithmetic operations 75 The binary arithmetic operations have the conventional priority levels. Note that some of these operations also apply to certain non-numeric types. Apart from the power operator, there are only two levels, one for multiplicative operators and one for additive operators: 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; a negative repetition factor yields an empty sequence. The @ (at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator. New in version 3.5. The$_{/}$(division) and$_{//}$ (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. 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 the$_{ZeroDivisionError}$ exception. 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}$ excep- tion. The arguments may be floating point numbers, e.g.,$_{3.14%0.7}$ equals$_{0.34}$ (since$_{3.14}$ equals$_{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$^{1}$. The floor division and modulo operators are connected by the following identity: x == (x//y)*y + (x%y) . Floor division and modulo are also connected with the built-in function$_{divmod()}$ :$_{divmod(x, y) == (x//}$ y, x%y) .$^{2}$. In addition to performing the modulo operation on numbers, the$_{%}$operator is also overloaded by string objects to perform old-style string formatting (also known as interpolation). The syntax for string formatting is described in the Python Library Reference, section old-string-formatting. The floor division operator, the modulo operator, and the$_{divmod()}$ function are not defined for complex numbers. Instead, convert to a floating point number using the$_{abs()}$ function if appropriate. The$_{+}$(addition) operator yields the sum of its arguments. The arguments must either both be numbers or both be sequences of the same type. In the former case, the numbers are converted to a common type and then added together. In the latter case, the sequences are concatenated. The$_{-}$(subtraction) operator yields the difference of its arguments. The numeric arguments are first converted to a common type. 1 While abs(x%y) < abs(y) is true mathematically, for floats it may not be true numerically due to roundoff. For example, and assuming a platform on which a Python float is an IEEE 754 double-precision number, in order that -1e-100 % 1e100 have the same sign as 1e100 , the computed result is -1e-100 + 1e100 , which is numerically exactly equal to 1e100 . The function math.fmod() returns a result whose sign matches the sign of the first argument instead, and so returns -1e-100 in this case. Which approach is more appropriate depends on the application. 2 If x is very close to an exact integer multiple of y, it’s possible for x//y to be one larger than (x-x%y)//y due to rounding. In such cases, Python returns the latter result, in order to preserve that divmod(x,y)[0] * y + x % y be very close to x . 6.7 Binary arithmetic operations m_expr ::= u_expr | m_expr "*" u_expr | m_expr "@" m_expr | m_expr "//" u_expr | m_expr "/" u_expr | m_expr "%" u_expr a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr