Operator precedence
About this app
Python expressions step by step: the expression tree hangs right below the expression — what goes first sits at the bottom — and the 14-level precedence ladder shows why. Values fly up, the intermediate state substitutes sub-expressions, and/or short circuits stay visibly unevaluated. 17 examples from the lecture notes (a += a == 2, -2 ** 2, 3 > 2 > 1 …), your own expressions, a guess beforehand and a “misconception” reading (strictly left to right, times before divided, powers from the left …) to compare.
With explain mode: an AI tutor walks you through the app, points out what matters in the scene and answers your questions.
Operator precedence in Python
When an expression contains several operators, precedence decides what is computed first. In Python parentheses bind tightest, then ** and the sign, then * / // %, then + and −, followed by shifts, &, ^, |, comparisons, not, and, and finally or. Operators on the same level are evaluated left to right; only ** goes right to left: 2 ** 3 ** 2 is 2 ** 9 = 512. Comparisons chain, so 3 > 2 > 1 means 3 > 2 and 2 > 1.
What the app shows
The expression sits at the top with its expression tree below: the operations that compute first hang at the bottom, the root at the top computes last. Step by step a node folds into its value, and the intermediate state shows the expression with values substituted. The precedence ladder on the right highlights the level of the current operator. With and and or, a branch skipped by a short circuit stays visibly unevaluated. The reading “Misconception” builds the tree the way the expression is often misread and evaluates it.
Try it yourself
Pick one of 17 examples, such as 2 + 3 * 4, 12 / 2 * 3, -2 ** 2 or the assignments a *= 5 + 5 and a += a == 2 from the lecture notes. Type your guess first and compare. Your own expressions with numbers, True/False, the variable a and up to 12 operators are evaluated like Python 3, including error messages such as ZeroDivisionError.
Frequently asked questions
- Which operators take precedence in Python?
- From tightest to loosest: parentheses, **, signs (+x, -x, ~x), * / // %, + -, << >>, &, ^, |, comparisons, not, and, or. Whatever is higher is evaluated first. Assignments such as a += 1 are statements and come last.
- Does Python evaluate operators of equal precedence left to right?
- Yes, with one exception. 12 / 2 * 3 gives 18.0 because / and * share a level and are evaluated from the left. The power operator ** goes from the right instead: 2 ** 3 ** 2 is 512.
- Why is -2 ** 2 equal to -4 in Python?
- ** binds tighter than a minus in front of it. Python reads the expression as -(2 ** 2) = -4. If you mean (-2) ** 2 = 4, you need parentheses.
- What can I try in the app “Operator precedence”?
- Evaluate examples and your own expressions step by step, read off the expression tree and the precedence ladder what computes first, watch short circuits in and/or, type your guess first and let the app work out typical misconceptions such as “strictly left to right”.
Subject: Computer science
More from Computer science
- Ackermann function
- Elevator simulator
- Binary workshop
- Binary versus linear search
- The Collatz problem
- Inside a CPU
- Swap the inside
- One frame in 16 milliseconds — how a game engine renders an image (3D)
A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps