Python Logical Operators
Python Logical Operators take two conditional expressions as operands and return the logical operation as a result. For example
Condition1 = True
Condition2 = False
Condition1 and Condition2 (will return false)
Logical operators that Python supports are
Operators | Operations | Examples |
---|---|---|
and | Results True if both of the operand expressions are True Else returns False. | 3==3 and 4==5 (Returns False) 1<2 and 4>2 (Returns True) |
or | Returns True if either of the operand expressions is True Else returns False. |
3==3 or 4==5 (Returns True) 1<2 or 4>2 (Returns True) |
not | Returns the inverted expression result. |
not True (returns False) not 4==2+1 (returns True) |
Bitwise Operators
This performs operations on their operands that change the state of their constituent bits (1s and 0s).
These are the bitwise operators available in Python are:
Operators | Operations | Examples |
---|---|---|
& | Returns 1 if both the operand bits are 1 Else returns 0. | 10101 & 10000 = 10000 11111 & 11001 = 11001 |
| | Returns 1 if either of the Operand bits is 1 Else returns 0. | 10101 | 10000 = 10101 11111 | 11001 = 11111 |
^ | Returns 1 if both the operand bits are different (either 1 and 0 or 0 and 1). Returns 0 if both bits are the same. | 10101 ^ 10000 = 00101 11111 ^ 11001 = 00110 |
~ | Returns the inverted Operand | ~11111 = 00000 ~01010 = 10101 |
>> | Shifts all the binary bits of the left operand to the right side as per the number of positions given by the right operand. | 8 >> 1 = 4 (1000 >> 1 = 0100) 12 >> 3 = 1 (1100 >> 3 = 0001) |
<< | Shifts all the binary bits of the left operand to the left side as per the number of positions given by the right operand. | 8 << 1 = 16 (1000 << 1 = 10000) 12 << 3 = 96 (1100 << 3 = 1100000) |