Skip to content

Operators

Operators are the symbols that perform operations on values — math, comparisons, logic.

Arithmetic operators

a, b = 10, 3

print("Add        :", a + b)    # 13
print("Subtract   :", a - b)    # 7
print("Multiply   :", a * b)    # 30
print("Divide     :", a / b)    # 3.333... (always returns float)
print("Floor div  :", a // b)   # 3 (drops the remainder)
print("Modulo     :", a % b)    # 1 (the remainder)
print("Power      :", a ** b)   # 1000 (10^3)
Operator Name Example
+ Addition 10 + 313
- Subtraction 10 - 37
* Multiplication 10 * 330
/ True division 10 / 33.333...
// Floor division 10 // 33
% Modulo (remainder) 10 % 31
** Exponent 2 ** 101024

Comparison operators

Return True or False:

a, b = 5, 10

print(a == b)    # equal              → False
print(a != b)    # not equal          → True
print(a < b)     # less than          → True
print(a > b)     # greater than       → False
print(a <= b)    # less or equal      → True
print(a >= b)    # greater or equal   → False

Logical operators

Combine boolean values:

age = 22
has_id = True

# AND — both must be true
print(age >= 18 and has_id)        # True

# OR — at least one must be true
print(age < 18 or has_id)          # True

# NOT — flip the boolean
print(not has_id)                  # False
Operator Meaning
and Both true
or At least one true
not Negate

Assignment operators

Shortcut to update a variable:

x = 10

x += 5    # same as x = x + 5
print(x)  # 15

x -= 3    # x = x - 3
print(x)  # 12

x *= 2    # x = x * 2
print(x)  # 24

x //= 5   # x = x // 5
print(x)  # 4

All arithmetic operators have an = shortcut version: +=, -=, *=, /=, //=, %=, **=.

Identity operators (is / is not)

== checks if two values are equal. is checks if two variables point to the same object in memory.

a = [1, 2, 3]
b = [1, 2, 3]      # different list with same values
c = a              # same list

print(a == b)      # True  — same values
print(a is b)      # False — different objects
print(a is c)      # True  — same object

# Use `is` mainly for None
x = None
print(x is None)   # True — the right way to check

Membership operators (in / not in)

Check if a value exists in a collection:

fruits = ["apple", "banana", "cherry"]

print("apple" in fruits)       # True
print("grape" in fruits)       # False
print("grape" not in fruits)   # True

# Works on strings too
print("py" in "python")        # True

Bitwise operators (advanced — skip on first read)

Work directly on the binary representation of integers.

a, b = 0b1100, 0b1010    # 12, 10

print(bin(a & b))   # AND   → 0b1000  (8)
print(bin(a | b))   # OR    → 0b1110  (14)
print(bin(a ^ b))   # XOR   → 0b0110  (6)
print(bin(~a))      # NOT   → -0b1101 (-13)
print(bin(a << 2))  # left  shift     → 0b110000 (48)
print(bin(a >> 2))  # right shift     → 0b11 (3)

Operator precedence

Operators evaluate in a fixed order. Highest first:

1. **              (exponent)
2. -x, +x, ~x      (unary)
3. *, /, //, %     (multiplication and division)
4. +, -            (addition and subtraction)
5. <<, >>          (bitwise shifts)
6. &               (bitwise AND)
7. ^               (bitwise XOR)
8. |               (bitwise OR)
9. ==, !=, <, >    (comparisons)
10. not
11. and
12. or

When in doubt, use parentheses:

# Without parens, hard to read
print(2 + 3 * 4 ** 2)           # 50

# With parens — same result, clearer intent
print(2 + (3 * (4 ** 2)))       # 50

# Parens can also change the answer
print((2 + 3) * 4)              # 20
print(2 + 3 * 4)                # 14

Practice

What does this print?

Expected: 14

print(2 + 3 * 4)

Make this print True (the two lists have equal contents)

Expected: True

a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)   # bug: `is` checks identity, not equality

Quiz — Quick check

What you remember

Q1. What is 10 // 3 in Python?

  • 3.333
  • 3
  • 1
  • 0.333

Why: // is floor division — it drops the decimal part. Regular / would give 3.333…; remainder is 10 % 3 = 1.

Q2. Which operator do you use to check if a list is empty?

  • ==
  • is None
  • not (e.g. if not my_list:)
  • len() = 0

Why: An empty list is falsy, so not my_list is True when it's empty. You can also write if len(my_list) == 0, but the not form is more idiomatic.

Q3. What does True and False or True evaluate to?

  • True
  • False
  • None
  • Error

Why: and binds tighter than or. So it's (True and False) or TrueFalse or TrueTrue.

Common doubts

When should I use is vs ==?

Use == to compare values — this is what you want 99% of the time. Use is to check identity (same object in memory) — almost always only for None, True, False. So: x == 5 ✓, x is None ✓, but x is 5 is unreliable and generally wrong.

What's the difference between / and //?

/ always returns a float — 10 / 2 is 5.0, not 5. // does floor division — drops everything after the decimal: 10 // 3 is 3. For negative numbers, // rounds toward negative infinity: -7 // 2 is -4, not -3.

Why does 0.1 + 0.2 equal 0.30000000000000004, not 0.3?

Floats can't represent 0.1 exactly in binary, just like 1/3 can't be written exactly in decimal. The tiny error accumulates. For money, use Decimal from the decimal module; for general comparisons, use math.isclose(a, b) instead of a == b.

What's next

Conditional Statements