Python Operators

All 7 operator categories โ€” arithmetic, comparison, logical, bitwise, assignment, identity, and membership โ€” with every example run in the notebook.

Overview

Operators are used to perform operations on variables and values. Python has 7 categories of operators:

  1. Arithmetic operators
  2. Comparison operators
  3. Logical operators
  4. Bitwise operators
  5. Assignment operators
  6. Identity operators
  7. Membership operators

This note walks through each category with the exact examples run in the notebook.

Key Concepts

1. Arithmetic Operators

Setup: x = 5, y = 2

OperatorExpressionResultMeaning
+print(x + y)7Addition
-print(x - y)3Subtraction
*print(x * y)10Multiplication
/print(x / y)2.5True division (always returns float)
%print(x % y)1Modulus (remainder)
**print(x ** y)25Exponentiation (5ยฒ)
//print(x // 2)2Floor division (drops decimal part)
x = 5
y = 2

print(x + y)    # 7
print(x - y)    # 3
print(x * y)    # 10
print(x / y)    # 2.5
print(x % y)    # 1
print(x ** y)   # 25
print(x // 2)   # 2
๐Ÿ’ก Tip: / vs // โ€” / gives a precise float result, // gives only the whole number part (rounded down, not truncated โ€” matters for negative numbers).

2. Comparison (Relational) Operators

Setup: x = 5, y = 2 (continued from above)

OperatorExpressionResultMeaning
>print(x > y)TrueGreater than
<print(x < y)FalseLess than
>=print(x >= y)TrueGreater than or equal to
<=print(x <= y)FalseLess than or equal to
==print(x == y)FalseEqual to (value comparison)
!=print(x != y)TrueNot equal to
print(x > y)    # True
print(x < y)    # False
print(x >= y)   # True
print(x <= y)   # False
print(x == y)   # False
print(x != y)   # True
โš ๏ธ Don't confuse == with =. = is assignment; == is comparison.

3. Logical Operators

Setup: x = True, y = False

OperatorExpressionResultMeaning
orprint(x or y)TrueTrue if at least one operand is True
andprint(x and y)FalseTrue only if both operands are True
notprint(not y)TrueInverts the boolean value
x = True
y = False

print(x or y)   # True
print(x and y)  # False
print(not y)    # True

Important Points:

4. Bitwise Operators

Setup: x = 2, y = 3 (binary: x = 010, y = 011)

OperatorExpressionResultMeaning
&print(x & y)2Bitwise AND
|print(x | y)3Bitwise OR
>>print(x >> 2)0Right shift by 2 bits
<<print(y << 3)24Left shift by 3 bits
~print(~x)-3Bitwise NOT (complement)
x = 2   # binary: 010
y = 3   # binary: 011

print(x & y)   # 2   โ†’ 010 & 011 = 010
print(x | y)   # 3   โ†’ 010 | 011 = 011
print(x >> 2)  # 0   โ†’ shifts bits right, drops off the end
print(y << 3)  # 24  โ†’ 011 << 3 = 011000 (binary) = 24
print(~x)      # -3  โ†’ bitwise complement: ~x = -(x+1)

How each works (bit-level):

๐Ÿ’ก Tip: Left shift by n โ‰ˆ multiply by 2โฟ; right shift by n โ‰ˆ floor-divide by 2โฟ.

5. Assignment Operators

Basic Assignment

a = 3
print(a)
# Output: 3

Compound Assignment Operators

OperatorMeaningExampleEquivalent to
+=Add and assigna += 3a = a + 3
-=Subtract and assigna -= 3a = a - 3
*=Multiply and assigna *= 3a = a * 3
&=Bitwise AND and assigna &= 3a = a & 3
a += 3        # same as: a = a + 3
print(a)
# Output: 6   (started from a = 3)

a -= 3
a *= 3
a &= 3
๐Ÿ’ก Tip: Compound assignment operators make code shorter and slightly more efficient than writing the full expression.

โš ๏ธ No Increment/Decrement Operators in Python

a++
++a
SyntaxError: invalid syntax

6. Identity Operators (is, is not)

Purpose: Check whether two variables point to the same object in memory โ€” not whether their values are equal.

ExpressionMeaning
a is bTrue if a and b reference the same object
a is not bTrue if a and b reference different objects

Example 1 โ€” Integers

a = 3
b = 3
print(a is b)
# Output: True

Why True? Python caches/interns small integers, so a and b may point to the same object in memory.

Example 2 โ€” Short Strings

a = "Hello"
b = "Hello"
print(a is b)
# Output: True

Why True? Python interns short, simple string literals, so both variables reference the same string object.

Example 3 โ€” Lists

a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)
# Output: False

Why False? Lists are mutable โ€” even with identical contents, a and b are separate objects. Python never interns lists.

Example 4 โ€” Strings with hyphens

a = "Hello-world"
b = "Hello-world"
print(a is b)
# Output: False

print(a is not b)
# Output: True

Why False? Strings with certain characters (like -) are generally not interned by Python, even though the values are equal.

โš ๏ธ Important takeaway: Use == to compare values, and is only when you need to compare object identity (e.g., x is None). String/int interning is a CPython implementation detail โ€” don't rely on it in real code.

7. Membership Operators (in, not in)

Purpose: Check whether a value exists within a sequence (string, list, tuple, etc.)

ExpressionMeaning
value in sequenceTrue if value exists in sequence
value not in sequenceTrue if value does NOT exist in sequence

Example 1 โ€” String membership

x = "Delhi"
print("D" in x)
# Output: True

Example 2 โ€” String non-membership

x = "Delhi"
print("D" not in x)
# Output: False

Example 3 โ€” List membership

x = [1, 2, 3]
print(5 in x)
# Output: False

Full Operator Summary Table

CategoryOperators
Arithmetic+, -, *, /, %, **, //
Comparison>, <, >=, <=, ==, !=
Logicaland, or, not
Bitwise&, |, ~, >>, <<
Assignment=, +=, -=, *=, &=, etc.
Identityis, is not
Membershipin, not in

Important Points

Quick Revision