Open workspace
Python 1.6Code walkthrough10 parts

Operators

Compute and compare with Python's operators, and know their precedence.

Loading your progress…

In this lesson10 parts

  1. 01Operators, operands and expressions
  2. 02The seven arithmetic operators, and the type each result has
  3. 03Floor division and modulo with negative numbers
  4. 04Precedence: which operator runs first, and the double star exception
  5. 05Comparison operators, chained comparisons, and assignment against comparison
  6. 06Logical operators: and, or, not, short circuit evaluation and returning an operand
  7. 07Augmented assignment: minus equals, plus equals, star equals and the rest
  8. 08Identity with is, and membership with in
  9. 09Why floats are not exact: isclose, storing cents, and rounding half to even
  10. 10Recap and your challenge

Key terms

The words this lesson introduces, each in one line. The module’s glossary collects them all.

operator · operand · expression
+ · the values it works on · code that evaluates to a value
precedence
the order operators run in: **, unary -, * / // %, + -, comparisons, not, and, or

Quiz 5 questions

Answer each one in your head, or on paper, before you check it. Getting one wrong here is how the lesson sticks.

  1. 01

    What is 10 / 5?

  2. 02

    What is -7 // 2?

  3. 03

    What is 70 / 1.75 ** 2?

  4. 04

    What is "" or "Guest"?

  5. 05

    What is round(2.5)?

Practice

Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.

The self-checking notebook for this lesson is Notebook 3 · Input, Output and Operators.

Common mistakes

What you will see when it goes wrong, why it happens, and the fix.

Where it’s used

Where this lesson’s ideas turn up in real work.