Introduction
Operators are the symbols carryout the arithmetic or logical computations on the operands.
Types of operators:
There are 7 types of operators. They are
- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators
- Assignment operators
- Identity operators
- Membership operators
Arithmetic operators
Operator | Description | Example |
---|---|---|
+ | Add two operands or unary plus | x + y+ 2 |
– | Subtract right operand from the left or unary minus | x – y- 2 |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
// | Floor division. Divide left operand by right operand and round the quotient to nearest lowest integer. | x//y |
% | Divide the left operand by right operand and returns the remainder | x %y |
1 2 3 4 5 6 7 8 | x=16 y=6 print("x+y=",x+y) print("x-y=",x-y) print("x*y=",x*y) print("x/y=",x/y) print("x//y=",x//y) print("x%y=",x%y) |
Output:
x+y= 22 x-y= 10 x*y= 96 x/y= 2.6666666666666665 x//y= 2 x%y= 4
Comparison operators
Comparison operators compare given two operands or values and return True or False.
Operator | Description | Example |
---|---|---|
> | Greater than. Returns True if left operand is greater than the right | x > y |
>= | Greater than or equal to. Returns True if left operand is greater than or equal to the right | x >= y |
< | Less than. Returns True if left operand is less than the right | x < y |
<= | Less than or equal to. Returns True if left operand is less than or equal to the right | x <= y |
== | Equal to. Returns True if both operands are equal | x == y |
!= | Not equal to. Returns True if operands are not equal | x != y |
1 2 3 4 5 6 7 8 | x=10 y=15 print("x>y is:",x>y) print("x<y is:",x<y) print("x>=y is:",x>=y) print("x<=y is:",x<=y) print("x==y is:",x==y) print("x!=y is:",x!=y) |
x>y is: False x<y is: True x>=y is: False x<=y is: True x==y is: False x!=y is: True
Logical operators
Logical operators are used to club two conditions and to evaluate them. The conditions can be negated using logical not operator.
Operator | Description | Example |
---|---|---|
and | Returns True if both the conditions are true | x and y |
or | Returns True if either of the conditions is true | x or y |
not | Returns True if condition is false (negate the operand) | not x |
1 2 3 | print((5>2)and (6>3)) print((2<5)or (6>3)) print(not(5==5)) |
True True False
Bitwise operators
Bitwise operators act on binary equivalent of the operands as if they were strings of binary digits. They operate bit by bit.
For example 2 in binary is 0010, 7 in binary is 0111.
In the below table let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
Operator | Description | Example |
---|---|---|
& | Bitwise AND | x & y = 0 (0000 0000) |
| | Bitwise OR | x | y = 14 (0000 1110) |
~ | Bitwise NOT | ~x = -11 (1111 0101) (In 2’s complement format) |
^ | Bitwise XOR | x ^ y = 14 (0000 1110) |
>> | Bitwise right shift | x >> 2 = 2 (0000 0010)(Right most two bits dropped and 0 at two left most bits added) |
<< | Bitwise left shift | x << 2 = 40 (0010 1000)(Left most two bits dropped and 0 at two right most bits added) |
Example
1 2 3 4 5 6 7 8 9 10 | x=10 y=4 print("x:",x) print("y:",y) print("x&y:",x&y) print("x|y:",x|y) print("~x:",~x) print("x^y:",x^y) print("x>>2:",x>>2) print("x<<2:",x<<2) |
x: 10 y: 4 x&y: 0 x|y: 14 ~x: -11 x^y: 14 x>>2: 2 x<<2: 40
Assignment operators
Assignment operators are used to assign a value to a variable. Simple assignment operator(=)assigns the value to its right to variable to its left.
1 2 | x=10 print("x:",x) |
Output:
x:10
Compound assignment operators
Operator | Example | Same As |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x – 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
//= | x //= 5 | x = x // 5 |
**= | x **= 5 | x = x ** 5 |
&= | x &= 5 | x = x & 5 |
|= | x |= 5 | x = x | 5 |
^= | x ^= 5 | x = x ^ 5 |
>>= | x >>= 5 | x = x >> 5 |
<<= | x <<= 5 | x = x << 5 |
Identity operators
Identity operators “is” and “is not” are used to check if the operands or objects to the left and right of these operators are referring to a value stored in the same memory location and returns True or False.
Operator | Description | Example |
---|---|---|
is | True if the operands are identical (refer to the same object) | x is True |
is not | True if the operands are not identical (do not refer to the same object) | x is not True |
1 2 3 4 5 6 7 8 9 | n1=10 n2=10 print(n1 is n2) s1="Good morning" s2="Good morning" print(s1 is not s2) l1=[1,2,3] #refer list copy behaviour l2=[1,2,3] print(l1 is l2) |
True False False
Membership operators
Membership operators “in” and “not in” are used to check if the given value is present in the sequence of values and returns True or False.
Operator | Description | Example |
---|---|---|
in | True if value/variable is found in the sequence | 5 in x |
not in | True if value/variable is not found in the sequence | 5 not in x |
In dictionary, membership operators will return True if the key is present. Presence of value in a Dictionary cannot be checked using membership operators.
Example
1 2 3 4 | a=[1,2,3,4] print(5 in a) square={1:1,2:4,3:9,4:16} print(4 in a) |
False True