In Python, compound assignment operators combine the assignment operator (=
) with an arithmetic operator. They take a variable, apply an arithmetic operation to it, and then return the result to the variable.
Key takeaways:
Operators perform arithmetic and logical operations on operands.
Compound assignment operators combine operations and assignment in one step (e.g.,
x += 3
is the same asx = x + 3
).They streamline code and improve efficiency.
Identity operators (
is
,is not
) check if two variables reference the same memory object.These operators ensure object identity, not just value equality.
In Python, operators are unique symbols that perform logical or arithmetic operations. The operand is the value on which the operator acts.
For example: 2+3 = 5
. Here, +
is the operator that performs addition.
There are many operators in Python. Some of them are:
However, in this Answer, we will only be talking about the compound assignment operator and the identity operator.
The compound assignment operator performs an operation (typically binary) on both operands and stores the result in the left operand. However, certain operators, like bitwise shift assignments, may follow different semantics.
Operator | Description | Example | Equivalent To |
| This operator adds and assigns the value to the variable |
|
|
| This operator subtracts and assigns the value to the variable |
|
|
| This operator multiplies and assigns the value to the variable |
|
|
| This operator divides and assigns the value to the variable |
|
|
| This operator does floor-division and assigns the value to the variable |
|
|
| This operator does mod and assigns the value to the variable |
|
|
| This operator does exponentiation and assigns value to the variable |
|
|
| This operator does Bitwise operation AND and assigns value to the variable |
|
|
| This operator does Bitwise XOR and assigns value to the variable |
|
|
| This operator does Left shift and assigns value to the variable |
|
|
| This operator does Right shift and assign value to the variable |
|
|
Let's use a compound operator in an example to familiarize ourselves with how they're commonly used.
a = 10#compound operatora += 10print(a)
The above expression is equivalent to a = a+10
.
In the same way, we can subtract, multiply, and divide a number.
The identity operator is used to determine whether two variables reference the same object in memory, rather than just having equal values. In Python, the two identity operators are:
| Returns True if both variables reference the same object |
|
| Returns True if both variables reference different objects |
|
Let's use an identity operator in an example to see how it functions in programs.
# Example on Identity operator:x = 10y = 101z = x is yv = x is not yprint(z)print(v)
z
returns False
because x
is not the same object as y
.
v
returns True
because x
is not the same object as y
.
Master the art of writing clean code with "Clean Code in Python." From Python basics to advanced concepts like decorators and async programming, gain the skills to build efficient, maintainable applications.
In Python, compound assignment operators provide a concise way to perform arithmetic or bitwise operations and update variables simultaneously, improving code readability and efficiency. They are particularly useful for iterative processes, calculations, and managing states in a program. On the other hand, identity operators allow developers to check if two variables refer to the same memory object, which is crucial when working with mutable objects and ensuring object identity rather than value equality.
Haven’t found what you were looking for? Contact Us