Skip to main content
CodeFlow
control flow~12 minRoute 01 4 of 12

Operators

The verbs — add, compare, combine, decide.

Prerequisites:VariablesData Types

Story — the cook on the kitchen counter

52+*==>and

A cook stands at the counter with two ingredients in front of them and a row of tools above the cutting board. A knife (+), a measuring cup (==), a sieve (>), a blender (and). Same ingredients, different tools — and you get a different dish each time.

That is what an operator is. Take one or two values, apply a small operation, get a new value back. Some operators give numbers; some give a true-or-false answer (a boolean). The cook keeps the operands; the result is something new.

Cook says

Same fish, same lemon. With the knife I get slices. With the measuring cup I get an answer to a question — yes, this is enough.

See it — apply each tool

We hold a = 5 and b = 2 on the counter. Press Step to walk through the carousel of operators and watch the result chip change colour — green for numbers, blue for booleans.

a
5
+
b
2
?
Carousel of tools
+-*==!=>andor
Pick the next operator: +.
Text-only trace
  1. a = 5, b = 2

Try it — order the recipe

Put the four lines in an order that runs.

Drag the pieces — or tab to one and use the / buttons — to put the program in order.

  1. print(a + b)
  2. b = 2
  3. a = 5
  4. print(a > b)
Hint: Both ingredients have to exist before you can apply a tool to them.

Code it — five languages, one idea

The same six expressions in five popular languages. Notice how comparisons always come back as a boolean — the only thing that changes is whether your language prints them as true or 1.

a = 5
b = 2
print(a + b)              # 7
print(a > b)              # True
print(a == b)             # False
print((a > 0) and (b > 0)) # True

Walk through it line by line

Each click of Next applies one operator. Notice which lines produce a number and which produce a True/False.

Step 1 / 6python
1a = 5
2b = 2
3print(a + b)
4print(a > b)
5print(a == b)
6print((a > 0) and (b > 0))

Quiz it — make it stick

  1. Question 1

    What kind of value does `5 > 2` produce?

  2. Predict — Question 2

    What does this print?

    x = 4
    y = 4
    print(x == y, x != y)
  3. True or false — Question 3

    In Python, `True and False` is True.

No Dumb Questions

Real questions other learners asked on this page.

  • Why does C++ print `1` and `0` for booleans instead of `true` and `false`?
    C++ historically stored booleans as integers, with 1 meaning true and 0 meaning false. Many languages still treat them that way under the hood. You can ask C++ to print "true"/"false" by setting `std::boolalpha`, but the default is the old numeric form.
  • What is the difference between `=`, `==`, and `===`?
    `=` assigns. `==` compares values. `===` (in JavaScript) compares values *and* types — `5 == "5"` is true, but `5 === "5"` is false. Other languages do not need `===` because they would not compare a number to a string in the first place.
  • When do I use `and` vs `&`?
    `and` (or `&&`) is the logical operator — it works on booleans and returns a boolean. `&` is bitwise — it works on the 1s and 0s inside a number. You almost always want the logical one when writing conditions.