Operators are special symbols in Python that carry out arithmetic or logical computations. The value that the operator operates on is called the operand.
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 shot, we will only be talking about the compound assignment operator and the identity operator.
The compound assignment operator performs the operation of the binary operator on both operands, and stores the result of that operation in the left operand (must be a modifiable value).
+=
-=
*=
/=
//=
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 compare objects
Operator | Description |
---|---|
is |
Returns true if both the variables are the same object. |
is not |
Returns true if both the objects are not the same object. |
# 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.