Operators in Python

Operators are symbols that perform operations on values or variables.
Python provides different types of operators for various purposes.

Why use operators?

  • Operators allow us to perform calculations, comparisons, and logical decisions in code.
  • They are essential for data processing, control flow, and decision-making.
  • Operators are widely used in mathematical computations, validations, and algorithm implementation.

Examples of real-time usage:

  • Arithmetic: Calculating total price, tax, or discounts
  • Comparison: Checking if a score meets a pass mark
  • Logical: Combining multiple conditions for access control
  • Assignment: Storing computed values in variables
  • Bitwise: Manipulating bits in low-level data or flags
  • Membership: Checking if an item exists in a list, set, or string
  • Identity: Verifying if two objects refer to the same memory location

We will cover:

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Logical Operators
  4. Assignment Operators
  5. Bitwise Operators
  6. Membership Operators (innot in)
  7. Identity Operators (isis not)

Arithmetic Operators

# ---- Arithmetic Operators ----

a = 10
b = 3

print("a + b =", a + b)   # Addition
print("a - b =", a - b)   # Subtraction
print("a * b =", a * b)   # Multiplication
print("a / b =", a / b)   # Division (float result)
print("a // b =", a // b) # Floor Division (integer result)
print("a % b =", a % b)   # Modulus (remainder)
print("a ** b =", a ** b) # Exponentiation (power)

a + b = 13
a – b = 7
a * b = 30
a / b = 3.3333333333333335
a // b = 3
a % b = 1
a ** b = 1000

Comparison Operators

# ---- Comparison Operators ----
# Always return True or False

x = 5
y = 10

print("x == y:", x == y)   # Equal to
print("x != y:", x != y)   # Not equal to
print("x > y:", x > y)     # Greater than
print("x < y:", x < y)     # Less than
print("x >= y:", x >= y)   # Greater than or equal to
print("x <= y:", x <= y)   # Less than or equal to

x == y: False
x != y: True
x > y: False
x < y: True
x >= y: False
x <= y: True

Logical Operators

# ---- Logical Operators ----
# Used to combine conditions (returns True/False)

p = True
q = False

print("p and q:", p and q)   # True if both are True
print("p or q:", p or q)     # True if at least one is True
print("not p:", not p)       # Reverses the value (True -> False, False -> True)

p and q: False
p or q: True
not p: False

Assignment Operators

# ---- Assignment Operators ----
# Used to assign values with operations

num = 10
print("Initial num:", num)

num += 5   # same as num = num + 5
print("After += 5:", num)

num -= 3   # same as num = num - 3
print("After -= 3:", num)

num *= 2   # same as num = num * 2
print("After *= 2:", num)

num /= 4   # same as num = num / 4
print("After /= 4:", num)

num %= 3   # same as num = num % 3
print("After %= 3:", num)

num **= 2  # same as num = num ** 2
print("After **= 2:", num)

Initial num: 10
After += 5: 15
After -= 3: 12
After *= 2: 24
After /= 4: 6.0
After %= 3: 0.0
After **= 2: 0.0

Bitwise Operators

# ---- Bitwise Operators ----
# Work at the binary level

a = 6   # binary: 110
b = 3   # binary: 011

print("a & b =", a & b)   # AND -> 110 & 011 = 010 (2)
print("a | b =", a | b)   # OR  -> 110 | 011 = 111 (7)
print("a ^ b =", a ^ b)   # XOR -> 110 ^ 011 = 101 (5)
print("~a =", ~a)         # NOT -> inverts bits (result depends on signed integer representation)
print("a << 1 =", a << 1) # Left shift (110 -> 1100 = 12)
print("a >> 1 =", a >> 1) # Right shift (110 -> 011 = 3)

a & b = 2
a | b = 7
a ^ b = 5
~a = -7
a << 1 = 12
a >> 1 = 3

Membership Operators

# ---- Membership Operators ----
# Check if a value is inside a collection (list, string, tuple, etc.)

nums = [1, 2, 3, 4, 5]

print("2 in nums:", 2 in nums)       # True (2 is in the list)
print("10 not in nums:", 10 not in nums) # True (10 is not in the list)

word = "python"
print("'p' in word:", 'p' in word)   # True
print("'x' in word:", 'x' in word)   # False

2 in nums: True
10 not in nums: True
‘p’ in word: True
‘x’ in word: False

Identity Operators

# ---- Identity Operators ----
# Compare memory locations of objects (whether two variables refer to the same object)

x = [1, 2, 3]
y = [1, 2, 3]
z = x

print("x is y:", x is y)       # False (different objects, same values)
print("x == y:", x == y)       # True  (values are equal)
print("x is z:", x is z)       # True  (z refers to the same object as x)
print("x is not y:", x is not y) # True

x is y: False
x == y: True
x is z: True
x is not y: True

Summary

We covered all types of operators:

  • Arithmetic
  • Comparison
  • Logical
  • Assignment
  • Bitwise
  • Membership (innot in)
  • Identity (isis not)

Next notebook: 04_strings.ipynb