C Language Augmented Assignment
This chapter describes Python's built-in operators as well as the precedence rules used in the evaluation of expressions.
This chapter is from the book This chapter describes Python's built-in operators as well as the precedence rules used in the evaluation of expressions.
Operations on Numbers
The following operations can be applied to all numeric types:
Operation | Description |
xy | Addition |
xy | Subtraction |
xy | Multiplication |
xy | Division |
xy | Truncating division |
xy | Power (x^{y}) |
xy | Modulo (xy) |
x | Unary minus |
x | Unary plus |
The truncating division operator (also known as floor division) truncates the result to an integer and works with both integers and floating-point numbers. As of this writing, the true division operator () also truncates the result to an integer if the operands are integers. Therefore, is , not . However, this behavior is scheduled to change in a future version of Python, so you will need to be careful. The modulo operator returns the remainder of the division xy. For example, is . For floating-point numbers, the modulo operator returns the floating-point remainder of xy, which is xxy)y. For complex numbers, the modulo () and truncating division operators () are invalid.
The following shifting and bitwise logical operators can only be applied to integers and long integers:
Operation | Description |
xy | Left shift |
xy | Right shift |
xy | Bitwise AND |
xy | Bitwise OR |
xy | Bitwise XOR (exclusive OR) |
x | Bitwise negation |
The bitwise operators assume that integers are represented in a 2's complement binary representation. For long integers, the bitwise operators operate as if the sign bit is infinitely extended to the left. Some care is required if you are working with raw bit- patterns that are intended to map to native integers on the hardware. This is because Python does not truncate the bits or allow values to overflow—instead, a result is promoted to a long integer.
In addition, you can apply the following built-in functions to all the numerical types:
Function | Description |
x | Absolute value |
xy | Returns xy |
xymodulo | Returns xymodulo |
xn | Rounds to the nearest multiple of ^{n} (floating-point numbers only) |
The function returns the absolute value of a number. The function returns the quotient and remainder of a division operation. The function can be used in place of the operator, but also supports the ternary power-modulo function (often used in cryptographic algorithms). The function rounds a floating-point number, x, to the nearest multiple of 10 to the power minus n. If n is omitted, it's set to . If x is equally close to two multiples, rounding is performed away from zero (for example, is rounded to and is rounded to ).
When working with integers, the result of an expression is automatically promoted to a long integer if it exceeds the precision available in the integer type. In addition, the Boolean values and can be used anywhere in an expression and have the values and , respectively.
The following comparison operators have the standard mathematical interpretation and return a Boolean value of for true, for false:
Operation | Description |
xy | Less than |
xy | Greater than |
xy | Equal to |
xy | Not equal to (same as ) |
xy | Greater than or equal to |
xy | Less than or equal to |
Comparisons can be chained together, such as in wxyz. Such expressions are evaluated as wxxyyz. Expressions such as xyz are legal, but are likely to confuse anyone else reading the code (it's important to note that no comparison is made between x and z in such an expression). Comparisons other than equality involving complex numbers are undefined and result in a .
Operations involving numbers are valid only if the operands are of the same type. If the types differ, a coercion operation is performed to convert one of the types to the other, as follows:
- If either operand is a complex number, the other operand is converted to a complex number.
- If either operand is a floating-point number, the other is converted to a float.
- If either operand is a long integer, the other is converted to a long integer.
- Otherwise, both numbers must be integers and no conversion is performed.
6.3.1 Augmented Assignment statements
Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement:
- augmented_assignment_stmt: target augop expression_list augop: "+=" | "-=" | "*=" | "/=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|=" target: identifier | "(" target_list ")" | "[" target_list "]" | attributeref | subscription | slicing
(See section for the syntax definitions for the last three symbols.)
An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once.
An augmented assignment expression like can be rewritten as to achieve a similar, but not exactly equal effect. In the augmented version, is only evaluated once. Also, when possible, the actual operation is performed in-place, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead.
With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. Similarly, with the exception of the possible in-place behaviour, the binary operation performed by augmented assignment is the same as the normal binary operations.
See About this document... for information on suggesting changes.
0 thoughts on “C Language Augmented Assignment”