29 lines
3.1 KiB
Markdown
29 lines
3.1 KiB
Markdown
## Binary Arithmetic Operations
|
|
|
|
Binary arithmetic operations have the conventional levels of precedence. Some of these operations also apply to certain non-numeric types. Aside from the exponentiation operator, there are two levels: one for multiplicative operators and another for additive ones:
|
|
|
|
```javascript
|
|
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
|
|
```
|
|
|
|
The `*` (multiplication) operator produces the product of its arguments. The arguments can both be numbers, or one argument must be an integer and the other a sequence. In the first case, the numbers are converted to a common type and then multiplied. In the second case, sequence repetition occurs; a negative repetition factor produces an empty sequence.
|
|
|
|
The `@` (matrix multiplication) operator is intended for matrix multiplication. No built-in type in Python implements this operator.
|
|
|
|
The `/` (division) and `//` (floor division) operators produce the quotient of their arguments. Numeric arguments are converted to a common type. Division between integers produces a floating-point number, while floor division between integers results in an integer; the result is that of a mathematical division with the “floor” function applied to the result. Division by zero raises a `ZeroDivisionError` .
|
|
|
|
The `%` (modulus) operator produces the remainder of the division of the first argument by the second. Numeric arguments are converted to a common type. A zero argument on the right raises a `ZeroDivisionError` . Arguments can be floating-point numbers, e.g., `3.14 % 0.7` is equal to `0.34` (since `3.14` is equal to `4 * 0.7 + 0.34` ). The modulus operator always produces 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.
|
|
|
|
The floor division and modulus operators are connected by the following identity: `x == (x // y) * y + (x % y)` . Floor division and modulus are also connected by the built-in function `divmod()` : `divmod(x, y) == (x // y, x % y)` .
|
|
|
|
In addition to performing the modulus operation on numbers, the `%` operator is also overloaded by string objects for old-style string formatting (also known as interpolation). The syntax for string formatting is described in the Python Library Reference, section Old-Style String Formatting.
|
|
|
|
The floor division operator, the modulus 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 produces the sum of its arguments. The arguments must both be numbers or both be sequences of the same type. In the first case, the numbers are converted to a common type and then added. In the second case, the sequences are concatenated.
|
|
|
|
The `-` (subtraction) operator produces the difference between its arguments. Numeric arguments are converted to a common type.
|