Operators Lecture2-4
Operators Lecture2-4
The assignment operators are used to assign values to variables. The following
table lists all the arithmetic operators in Python:
Operator Function Example in Python Shell
>>> x = 5;
= >>> x
5
>>> x = 5
>>> x += 5
+= operator.iadd(a,b) 10
>>> import operator
>>> x = operator.iadd(5, 5)
10
>>> x = 5
>>> x -= 2
-= operator.isub(a,b) 3
>>> import operator
>>> x = operator.isub(5,2)
>>> x = 2
>>> x *= 3
*= operator.imul(a,b) 6
>>> import operator
>>> x = operator.imul(2, 3)
>>> x = 6
>>> x /= 3
/= operator.itruediv(a,b) 2
>>> import operator
>>> x = operator.itruediv(6, 3)
>>> x = 6
>>> x //= 5
//= operator.ifloordiv(a,b) 1
>>> import operator
>>> operator.ifloordiv(6,5)
>>> x = 11
>>> x %= 3
%= operator.imod(a, b) 2
>>> import operator
>>> operator.imod(11, 3)
2
>>> x = 11
>>> x &= 3
&= operator.iand(a, b) 1
>>> import operator
>>> operator.iand(11, 3)
1
>>> x = 3
>>> x |= 4
|= operator.ior(a, b) 7
>>> import operator
>>> operator.mod(3, 4)
7
>>> x = 5
>>> x ^= 2
^= operator.ixor(a, b) 7
>>> import operator
>>> operator.ixor(5, 2)
7
>>> x = 5
>>> x >>= 2
>>= operator.irshift(a, b) 1
>>> import operator
>>> operator.irshift(5, 2)
1
>>> x = 5
>>> x <<= 2
<<= operator.ilshift(a, b) 20
>>> import operator
>>> operator.ilshift(5, 2)
20
Bitwise Operators
A B A&B A|B A^B
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Special Operators
Identity Operators
The identity operators check whether the two objects have the same id value i.e.
both the objects point to the same memory location.
Operator Function Description Example in Python Shell
>>> x = 5; y = 6
>>> x is y
is operator.is_(a,b) True if both are False
true >>> import operator
>>> operator.is_(x,y)
False
>>> x = 5; y = 6
>>> x is not y
is not operator.is_not(a,b) True if at least True
one is true >>> import operator
>>> operator.is_not(x, y)
True