Python Operators and Bitwise Shifts
1. Arithmetic Operators
Operator Meaning Example Output
+ Addition 5+3 8
- Subtraction 10 - 4 6
* Multiplication 6*2 12
/ Division (float) 7/2 3.5
// Floor Division 7 // 2 3
% Modulus (remainder) 7%2 1
** Exponentiation 2 ** 3 8
2. Relational (Comparison) Operators
Operator Meaning Example Output
== Equal to 5 == 5 True
!= Not equal 5 != 3 True
> Greater than 7>2 True
< Less than 3<2 False
>= Greater or equal 5 >= 5 True
<= Less or equal 4 <= 7 True
3. Logical Operators
Operator Meaning Example Output
and True if both conditions are true 5 > 3 and 10 > 5 True
or True if at least one condition is true 5 > 3 or 2 > 10 True
not Reverses result not(5 > 3) False
4. Bitwise Operators
Operator Meaning Example Output
& Bitwise AND 6&3 2
| Bitwise OR 6|3 7
^ Bitwise XOR 6^3 5
~ Bitwise NOT ~6 -7
<< Left shift (multiply by 2^n) 5 << 1 10
>> Right shift (divide by 2^n) 5 >> 1 2
5. Left and Right Shift on Negative Numbers
In Python, shifting works as multiplying/dividing by powers of 2. For negative numbers, the same
rule applies because Python uses infinite-precision integers.
Example:
-5 << 1 = -10 (multiplied by 2)
-5 << 2 = -20 (multiplied by 4)
-20 >> 1 = -10 (floor division by 2)
-20 >> 2 = -5 (floor division by 4)
6. Operator Precedence (Highest to Lowest)
1. Parentheses ( )
2. Exponentiation **
3. Unary +, -, ~, not
4. Multiplication, Division, Floor Division, Modulus (*, /, //, %)
5. Addition, Subtraction (+, -)
6. Bitwise Shifts (<<, >>)
7. Bitwise AND (&)
8. Bitwise XOR (^)
9. Bitwise OR (|)
10. Relational/Comparison (<, <=, >, >=, ==, !=)
11. Logical AND (and)
12. Logical OR (or)
13. Membership & Identity (in, not in, is, is not)
14. Assignment (=, +=, -=, etc.)