What are the compound assignment & identity operators in Python?

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 as x = 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.

Compound assignment 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

x += 4

x = x + 4

-=

This operator subtracts and assigns the value to the variable

x -= 4

x = x - 4

*=

This operator multiplies and assigns the value to the variable

x *= 4

x = x * 4

/=

This operator divides and assigns the value to the variable

x /= 4

x = x / 4

//=

This operator does floor-division and assigns the value to the variable

x //= 4

x = x // 4

%=

This operator does mod and assigns the value to the variable

x %= 4

x = x % 4

**=

This operator does exponentiation and assigns value to the variable

x **= 4

x = x ** 4

&=

This operator does Bitwise operation AND and assigns value to the variable

x &= 4

x = x & 4

^=

This operator does Bitwise XOR and assigns value to the variable

x ^= 4

x = x ^ 4

<<=

This operator does Left shift and assigns value to the variable

x <<= 4

x = x << 4

>>=

This operator does Right shift and assign value to the variable

x >>= 4

x = x >> 4

Let's use a compound operator in an example to familiarize ourselves with how they're commonly used.

a = 10
#compound operator
a += 10
print(a)

The above expression is equivalent to a = a+10. In the same way, we can subtract, multiply, and divide a number.

Identity operator

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:

is

Returns True if both variables reference the same object

x is y

is not

Returns True if both variables reference different objects

x is not y

Let's use an identity operator in an example to see how it functions in programs.

# Example on Identity operator:
x = 10
y = 101
z = x is y
v = x is not y
print(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.

Conclusion

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.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What are the compound assignment operators in Python?

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.


What are the identity operators in Python?

Identity operators in Python are used to compare the memory location (or object identity) of two objects.

  • is: Returns True if both operands refer to the same object in memory.
  • is not: Returns True if both operands refer to different objects in memory.

What are assignments in Python?

In Python, identity operators are used to compare two objects’ memory locations, also known as object identities.

  • is: If both operands point to the same memory item, it returns True.
  • is not: If both operands point to distinct memory objects, it returns True.

Free Resources