Skip to main content
Albero Academy

Python Operators

Master arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators with practical examples.

Chapter 2Python14 min readBeginner friendly
  • #Python
  • #Operators
  • #Arithmetic

Operators are how you do real work in Python — adding numbers, comparing values, building decisions. This chapter covers the seven categories every Python developer uses every day.

Arithmetic operators

The seven arithmetic operators in Python:

Python
a = 17
b = 5
print(a + b) # 22 addition
print(a - b) # 12 subtraction
print(a * b) # 85 multiplication
print(a / b) # 3.4 float division
print(a // b) # 3 integer division (floor)
print(a % b) # 2 modulus (remainder)
print(a ** b) # 1419857 exponentiation

Comparison operators

Comparisons always return a bool — either True or False.

Python
x, y = 10, 20
print(x == y) # False equal
print(x != y) # True not equal
print(x < y) # True
print(x > y) # False
print(x <= 10) # True
print(y >= 25) # False

Logical operators

Combine conditions with and, or, not:

Python
age = 22
has_id = True
# you can enter if you're 18+ AND have ID
if age >= 18 and has_id:
print("Welcome in.")
# you skip the queue if you have a member card OR a VIP pass
member = False
vip = True
if member or vip:
print("Skip the queue.")
# inversion
print(not member) # True

Assignment operators

Shorthand for "operate then reassign". Two patterns you'll use the most:

Python
total = 0
total += 50 # equivalent to total = total + 50
total += 30
total -= 10
total *= 2
print(total) # 140

Membership & identity

Two pairs that confuse beginners but are actually simple:

Python
fruits = ["apple", "mango", "banana"]
# membership: is value in collection?
print("mango" in fruits) # True
print("kiwi" not in fruits) # True
# identity: do two names point at the SAME object?
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True — same list in memory
print(a is c) # False — different lists with equal contents
print(a == c) # True — values are equal

Key takeaways

  1. 01Arithmetic: +, -, *, /, //, %, ** — note that / is float division, // is integer division.
  2. 02Comparison operators always return True or False.
  3. 03Use and / or / not to combine conditions; Python short-circuits left-to-right.
  4. 04Compound assignment (+=, -=, *=, /=) keeps your code readable.
  5. 05Use == for "values equal", is for "same object in memory" (commonly is None).