To know the bitwise operation, you can read here.
1 . Arithmetic operators.
These operators are used to perform mathematical operations.
Operator | Name | Example |
+ | Addition | 3 + 2 = 5 |
– | Subtraction | 3 – 2 = 1 |
* | Multiplication | 3 * 2 = 6 |
/ | Division | 3 / 2 = 1.5 |
% | Modulus | 3 % 2 = 1 |
** | Exponentiation | 3 ** 2 = 9 |
// | Floor Division | 3 // 2 = 1 |
2. Assignment Operators
These operators are used to assign values to variables. It works from right to left.
Operator | Example |
= | x = 5 (Assigning value 5 to x) |
+= | x += 3 or x = x +3 |
-= | x -= 5 or x = x – 5 |
*= | x *= 5 or x = x * 5 |
/= | x /= 5 or x = x / 5 |
%= | x %= 5 or x = x % 5 |
//= | x //= 5 or x = x // 5 |
**= | x **= 5 or x = x ** 5 |
&= | x &= 5 or x = x & 5 |
|= | x |=5 or x = x | 5 |
^= | x ^= 5 or x = x ^ 5 |
>>= | x >>= 3 or x = x >> 5 |
<<= | x <<= 5 or x = x << 5 |
3. Comparison Operator
Comparison operators are used to comparing two values.
Operator | Name | Example | Description |
== | Equal | x == y | Return True if both the operands have the same value. |
!= | Not Equal | x != y | Return True if both the operand have different values. |
> | Greater than | x > y | Return True if the value of x is greater than the value of y. |
< | Less than | x < y | Return True if the value of x is less than value of y. |
>= | Greater than or equal to | x >= y | Return True if the value of x is greater than or the same as the value of y. |
<= | Less than or equal to | x <= y | Return True if the value of x is lesser than or the same as the value of y. |
4. Logical Operator
Logical operator are used to combine conditional statements like if else, elif.
Operator | Example | Description |
and | x < 5 and x > 2 | Returns True if both statements are true |
or | x < 5 or x < 4 | Returns True if any one of the statements is true. |
not | not(x < 5 and x < 10) | Reverse the result, returns False if the result is true. |
5. Membership Operators
Membership operators are used to test if a sequence is presented in an object.
Operator | Example | Description |
in | x in y | Return True if a sequence with the specified value is present in the object. |
not in | x not in y | Returns True if a sequence with the specified value is not present in the object |
6. Bitwise operators
Operator | Name | Description |
& | AND | Set each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if any one of the two bits is 1 |
^ | XOR | Sets each bit to 1 if only two of its bits is 1 |
~ | NOT | Inverse all the bits |
<< | Zero Fill Left Shift | Shift left by pushing zeros in from right and let the leftmost bits fall off |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
7. Identity Operators
Identity operator is used to compare to object. It will check whether they are actually the same object having the same memory location.
Operator | Example | Description |
is | x is y | Returns True if both x and y having same memory location |
is not | x is not y | Returns True if both x and y have at different memory location |
Please share and comment.
Contribute article and earn : contribute.articles@tech-bloggers.in
1 thought on “Operators in Python”