0% found this document useful (0 votes)
43 views2 pages

Python Operators and Bitwise Shifts Guide

The document outlines various types of Python operators, including arithmetic, relational, logical, and bitwise operators, along with their meanings and examples. It also explains how left and right shifts work on negative numbers and provides a list of operator precedence from highest to lowest. Each operator is illustrated with examples demonstrating their functionality.

Uploaded by

patelkris301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Python Operators and Bitwise Shifts Guide

The document outlines various types of Python operators, including arithmetic, relational, logical, and bitwise operators, along with their meanings and examples. It also explains how left and right shifts work on negative numbers and provides a list of operator precedence from highest to lowest. Each operator is illustrated with examples demonstrating their functionality.

Uploaded by

patelkris301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.)

You might also like