Best afterOperator precedence: × before +
Powers: ** not ^
Lots of languages and spreadsheets use ^ for 'to the power of'. Python doesn't — ^ is bitwise XOR, and it produces a number with no error to warn you. Watch 2 ^ 3 come out as something surprising.
Raise base (2) to the power of 3 (we want 8).
^ is not 'to the power of' execution-derived · CPython
variables
state after line 1 runs
| variable | value |
|---|---|
base | 2 |
step 1 / 2
Click a line, drag the slider, or use the ← → keys.
What you are looking at
Watch power — the operator is doing something other than what it looks like.
- Buggy —
power = base ^ 3gives1.^is XOR (bitwise), and2 XOR 3is1. The code runs fine and is silently wrong — the most dangerous kind of bug. - Procedural —
power = base ** 3gives8. The double-star**is Python's power operator.
Every state you see came from running the program under CPython's tracer at build time — see how GlassBox stays honest.