Skip to content
Kuza Ndoto Academy

Operators

Beginner → IntermediateChapter 2 · Page 12 of 564 min readUpdated 15 Nov 2026

Operators are symbols and keywords that perform an action on values, such as adding numbers, comparing scores or combining conditions. You've already met some; this page gathers all the main groups in one place so you can read and write any expression with confidence.

Arithmetic operators

These do maths. They're covered in depth on Numbers & Arithmetic.

OperatorMeaningExampleResult
+ - *Add, subtract, multiply6 * 742
/Division7 / 23.5
//Floor division7 // 23
%Remainder7 % 21
**Power7 ** 249

+ and * also work on strings: "ab" + "cd" joins them and "ab" * 3 repeats them.

Assignment operators

= assigns a value. The compound assignment operators update a variable using its current value, so x += 5 is short for x = x + 5.

x = 10
x += 5      # 15
x *= 2      # 30
x //= 4     # 7
x %= 4      # 3
x **= 2     # 9
print(x)

wallet = 1000
wallet -= 350     # bought lunch and fare
print(wallet)
9 650

There are compound versions for every arithmetic operator: +=, -=, *=, /=, //=, %= and **=. Python has no ++ or -- operators; write count += 1 instead.

Comparison operators

Comparisons ask a question and always answer with True or False.

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than5 > 8False
<Less than5 < 8True
>=Greater than or equal to5 >= 5True
<=Less than or equal to8 <= 5False
age = 20
print(18 <= age <= 25)      # chained: is age between 18 and 25?
print("apple" < "banana")    # strings compare alphabetically
print("Zebra" < "apple")     # capitals come before lowercase
print(0.1 + 0.2 == 0.3)      # float rounding strikes again
True True True False

Python lets you chain comparisons as you would in maths: 18 <= age <= 25 means 18 <= age and age <= 25. Strings compare character by character using their Unicode codes, and every capital letter has a smaller code than every lowercase letter, which is why "Zebra" < "apple" is True.

Common mistake

Confusing = with ==. One equals sign assigns; two compare. Writing if score = 100: is a SyntaxError. And never test floats for exact equality: compare round(a, 2) == round(b, 2) or use math.isclose(a, b).

Logical operators

and, or and not combine or flip Boolean values.

OperatorResult is True when…ExampleResult
andboth sides are trueTrue and FalseFalse
orat least one side is trueTrue or FalseTrue
notthe value is falsenot TrueFalse
age = 19
has_ticket = True
is_banned = False

print(age >= 18 and has_ticket)
print(age < 18 or has_ticket)
print(not is_banned)
True True True

Short-circuiting

Python stops evaluating as soon as it knows the answer. In a and b, if a is false, b is never checked; in a or b, if a is true, b is skipped. In fact, and and or return one of their operands rather than strictly True or False, which gives a neat way to supply a default:

name = ""
display_name = name or "Guest"
print(display_name)

count = 0
print(count != 0 and 10 / count > 1)   # safe: the division never runs
Guest False

Identity operators: is and is not

== asks "do these have the same value?", while is asks "are these the very same object in memory?". Use is mainly to check for None.

phone = None
print(phone is None)
print(phone is not None)

a = [1, 2]       # a list: an ordered collection
b = [1, 2]
print(a == b)    # same contents
print(a is b)    # but two separate lists
True False True False

Membership operators: in and not in

in checks whether a value appears inside a string or collection.

print("a" in "Kampala")
print("z" not in "Kampala")
print("beans" in ["maize", "beans", "rice"])
True True True

Operator precedence

When an expression mixes operators, Python applies them in this order, from highest to lowest priority:

  1. Brackets ( )
  2. Power **
  3. Unary minus -x
  4. *, /, //, %
  5. +, -
  6. Comparisons, in, is
  7. not
  8. and
  9. or
print(True or False and False)    # and runs first
print(not 5 > 3)                  # comparison runs before not
print(10 - 4 > 2 * 3)             # 6 > 6
True False False

Python also has bitwise operators (&, |, ^, ~, <<, >>) that work on the binary digits of integers. You'll see &, | and ^ again with sets. When a long expression is hard to read, add brackets even if they aren't strictly needed.

Key points

  • Arithmetic operators do maths; compound assignments like += update a variable in place.
  • Comparisons return True/False and can be chained, as in 0 < x < 10.
  • and, or and not combine conditions and short-circuit.
  • Use is for None checks and == for comparing values; in tests membership.
  • Follow precedence rules, and use brackets to make complex expressions clear.
Up next · New chapter: Control FlowIf…Else StatementsMake decisions in your code.
→

Tip: use the ← and → keys to move between pages.