An expression is any piece of code that produces a value. Operators are the symbols that combine values into expressions: +, -, ==, and, and many more.

This section walks through every kind of operator Python offers.

What’s in this section

  1. Arithmetic operators — add, subtract, multiply, divide, modulo, exponent
  2. Comparison operators — equal, less than, greater than
  3. Logical operatorsand, or, not, and short-circuit behaviour
  4. Assignment operators=, +=, -=, and friends
  5. Membership and identityin, not in, is, is not
  6. The ternary operator — a one-line if/else

Most of these will feel familiar from maths class. The ones that won’t — is vs ==, short-circuiting, the ternary — are the ones to slow down on.

Toggle theme (T)