Python Operators and Operator Precendence

Operators in Python are special symbols used to perform operations on variables and values. Python provides several categories of operators, including:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Identity Operators
  • Membership Operators
  • Bitwise Operators

1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations:

OperatorDescriptionExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 31.666
//Floor division5 // 31
%Modulus (remainder)5 % 32
**Exponentiation5 ** 3125
x = 10
y = 4

print(x + y)   # 14
print(x - y)   # 6
print(x * y)   # 40
print(x / y)   # 2.5
print(x // y)  # 2
print(x % y)   # 2
print(x ** y)  # 10000

2. Assignment Operators

Assignment operators are used to assign values to variables:

OperatorExampleEquivalent
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
//=x //= 3x = x // 3
**=x **= 3x = x ** 3
x = 10
x += 5   # x becomes 15
print(x)

x *= 2   # x becomes 30
print(x)

3. Comparison Operators

Comparison operators compare two values and return a boolean result (True or False):

OperatorMeaningExampleResult
==Equal5 == 5True
!=Not equal5 != 3True
>Greater than5 > 3True
<Less than5 < 3False
>=Greater than or equal5 >= 5True
<=Less than or equal5 <= 3False
print(10 == 10)  # True
print(10 != 5)   # True
print(10 > 5)    # True
print(5 < 2)     # False

4. Logical Operators

Logical operators combine boolean expressions:

OperatorDescriptionExampleResult
andTrue if both statements are True(5 > 2) and (3 < 4)True
orTrue if one statement is True(5 < 2) or (3 < 4)True
notReverses the resultnot(5 > 2)False
print((4 > 2) and (5 > 3))  # True
print((4 > 6) or (5 > 3))   # True
print(not(4 > 2))           # False

5. Identity Operators

Identity operators determine if two variables point to the same object:

OperatorDescriptionExampleResult
isTrue if both variables are same objectx is yTrue or False
is notTrue if both variables are not same objectx is not yTrue or False
x = [1, 2, 3]
y = [1, 2, 3]
z = x

print(x is y)     # False (different objects)
print(x is z)     # True (same object)
print(x is not y) # True

6. Membership Operators

Membership operators check if a value exists within an object (e.g., string, list, tuple):

OperatorDescriptionExampleResult
inTrue if value is found3 in [1, 2, 3]True
not inTrue if value is not found4 not in [1,2,3]True
numbers = [1, 2, 3, 4]

print(2 in numbers)      # True
print(5 not in numbers)  # True

7. Bitwise Operators

Bitwise operators perform operations at the bit-level:

OperatorMeaningExampleResult (binary)
&Bitwise AND5 & 31 (0101 & 0011 = 0001)
||5 | 37
^Bitwise XOR5 ^ 36 (0101 ^ 0011 = 0110)
~Bitwise NOT~5-6
<<Left Shift5 << 220 (0101 → 10100)
>>Right Shift5 >> 21 (0101 → 0001)
print(5 & 3)   # 1
print(5 | 3)   # 7
print(5 ^ 3)   # 6
print(~5)      # -6
print(5 << 2)  # 20
print(5 >> 2)  # 1

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top