Operators

Doing things to values

Beginner 9 min

In this lesson

Operators are the symbols that combine and compare values. You’ve met several already; this lesson gathers them into one map and explains the order they run in.

Explain it like I’m 5

Operators are little verbs: add, compare, combine yes/no answers, check membership. Knowing the full set means you can express almost any simple idea in code.

The families of operators

  • Arithmetic: + - * / // % **
  • Comparison: == != < > <= >= → produce booleans
  • Logical: and or not → combine booleans
  • Assignment: =, and shortcuts like +=, -=, *=
  • Membership: in, not in → is a value inside a collection?
  • Identity: is, is not → are these the same object? (mainly for None)
Example
letters = "aeiou"
print("e" in letters)
print("z" not in letters)

result = None
print(result is None)
Output
True
True
True
Membership (in) and identity (is) operators.

Augmented assignment

count += 1 is shorthand for count = count + 1. The same pattern works for -=, *=, //=, and others. It’s less typing and makes ‘update this variable’ obvious.

Example
total = 0
total += 5
total += 5
total *= 2
print(total)
Output
20
Augmented assignment, step by step.

Precedence: the order operators run

Like maths, Python has an order of operations: ** first, then * / // %, then + -, then comparisons, then not, then and, then or. So 2 + 3 * 4 is 14. When in doubt, add parentheses — they cost nothing and make intent obvious.

Example
print(2 + 3 * 4)
print((2 + 3) * 4)
print(2 ** 3 * 2)
Output
14
20
16
Precedence, and how parentheses override it.

Predict the result, then run: combine a price calculation with a membership test. Add parentheses to make the maths clear.

price = 10
qty = 3
total = price * qty
print(total)
print(total > 25 and "discount" in "discount day")

The conditional expression (a one-line if/else)

Sometimes you want to choose a value based on a condition. Python has a compact operator for exactly that — the conditional expression (often called the ‘ternary’): value_if_true if condition else value_if_false.

It reads almost like English: status = "adult" if age >= 18 else "minor". Reach for it when the choice is short and clear; for anything with multiple steps, a full if statement is easier to read.

Example
age = 16
status = "adult" if age >= 18 else "minor"
print(status)

for n in [4, 7]:
    print(n, "is", "even" if n % 2 == 0 else "odd")
Output
minor
4 is even
7 is odd
Picking a value inline, including inside a loop.

What does 2 + 3 * 4 evaluate to?

What is the shorthand for x = x + 3?

Mini exercise (medium)

Start with points = 0. Using augmented assignment, add 10, then add 5, then double it, and print the result.

Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.

points = 0
# TODO: add 10, then add 5, then double it — using += and *=
print(points)

What to learn next

You got the full operator tour: arithmetic, comparison, logical, assignment, membership, and identity, plus how precedence works and why parentheses can make code clearer. You practised the assignment operators by growing a score with += and *=.

Your programs can compute, but they can’t yet talk to a person. Input and Output shows how to read input() (which always hands back a string), convert it to what you need, and format what you print back. When you’re ready, continue to the next lesson.