Python Operators

1. Arithmetic Operators

a = 10
b = 3

print(a + b)   # Addition → 13
print(a - b)   # Subtraction → 7
print(a * b)   # Multiplication → 30
print(a / b)   # Division → 3.333...
print(a // b)  # Floor Division → 3
print(a % b)   # Modulus → 1
print(a ** b)  # Exponentiation → 1000

Used for mathematical computations.


2. Assignment Operators

x = 10
x += 5   # x = x + 5
x -= 2   # x = x - 2
x *= 3   # x = x * 3
x /= 2   # x = x / 2

print(x)  # Output: 19.5

Short-hand syntax for updating variable values.


3. Comparison (Relational) Operators

Return boolean results (True / False).


4. Logical Operators

Combine conditional expressions.


5. Bitwise Operators

Operate at the binary level.


6. Membership Operators

Test whether a value exists in a sequence.


7. Identity Operators

Check memory identity, not equality.


8. Unary Operators

Operate on a single operand.


9. Ternary (Conditional) Operator

Compact conditional assignment syntax.


10. Operator Precedence

Precedence Order:

  1. ** (Exponentiation)

  2. *, /, //, %

  3. +, -

  4. Comparison

  5. Logical (and, or, not)

Understanding precedence prevents logical bugs.


Last updated