Operators & Expressions

Variables are only useful once you can do something with them. Today's about the operators that combine and compare values — the arithmetic you'd expect, plus a couple of Python-specific ones worth knowing early.

What You'll Learn

  • Arithmetic: + - * / (always gives a float), // (floor division), % (modulo — the remainder), ** (exponent)
  • Comparison: == != < > <= >= — each gives back a bool, which is exactly what feeds into next lesson's conditionals
  • Order of operations: same rules as math class — exponents, then *//////%, then +/- — use parentheses to force a different order
Watch out for: // and % are easy to mix up — // gives the whole number of times one value divides another, % gives just what's left over. Try saying out loud what real quantity each one represents if you're not sure which to use.

Today's Activity

Today's practice happens inside the notebook below — work through it top to bottom:

  1. A worked example, fully written out
  2. Two guided practices — change a few values, then fill in some blanks
  3. Two independent practices — write your own from scratch, different scenario each time
  4. An optional stretch, if you want to go further
  5. One check-your-understanding question to close

Download the notebook, save it into your 1_foundations folder in your cloned repo, and open it in VS Code. Commit once you've saved it, then commit again when you're done for the day.

Download 1.3_Operators_and_Expressions.ipynb

New to notebooks? See Using Jupyter Notebooks first.

What's Next

Coming up: 1.4 — Conditionals, where your programs finally start making decisions.