Skip to main content
Updated Feb 24, 2026

Reading Expressions and Predicting Output

In Lesson 2, you learned to read Python's four primitive types -- str, int, float, bool -- and interpret type annotations as labels that tell you what a variable holds. You can look at price: float = 9.99 and know that price is a decimal number. Now you move from reading individual values to reading what Python does with them.

Emma writes five lines on a whiteboard:

Loading Python environment...

"Before I run this," she says, "predict the value of every variable at the end." James traces through them. x starts at 5, y is 8, x becomes 10... he pauses at z. "Is that 1.25 or 1?" He guesses 1. Emma runs the code. z is 1.25 -- a float. "Division always returns a float in Python," she says. James got two values wrong. His brain skipped steps and took shortcuts.

"That is why we trace," Emma says. "Your brain takes shortcuts. The trace table does not."

Arithmetic Operators

Python has seven arithmetic operators. You already know most of them from math, but three behave in ways that surprise beginners.

OperatorNameExampleResult
+Addition3 + 47
-Subtraction10 - 37
*Multiplication5 * 420
/Division6 / 32.0
//Floor division7 // 23
%Modulo (remainder)7 % 31
**Exponentiation2 ** 38

The three that surprise you are /, //, and **.

Division always returns a float. Even when the result is a whole number, / gives you a float:

Loading Python environment...

Output:

2.0

Floor division rounds toward negative infinity. The // operator divides and drops the decimal part -- but it floors toward negative infinity, not toward zero:

Loading Python environment...

Output:

3
-4

For positive numbers, 7 // 2 gives 3 (drops the .5). For negative numbers, -7 // 2 gives -4, not -3. The floor goes down to the next lower integer, and -4 is lower than -3.5.

Exponentiation is right-to-left. When you chain **, Python evaluates from right to left:

Loading Python environment...

Output:

512

Python reads this as 2 ** (3 ** 2) = 2 ** 9 = 512. Not (2 ** 3) ** 2 = 8 ** 2 = 64. The exponent on the right evaluates first.

One more trap with ** and negation:

Loading Python environment...

Output:

-9

This is -(3 ** 2), not (-3) ** 2. Exponentiation binds tighter than the negation sign. If you want negative three squared, write (-3) ** 2 to get 9.

Operator Precedence

When an expression has multiple operators, Python follows a precedence table to decide which operations happen first. You do not need to memorize every level. You need to know four rules:

PriorityOperatorsWhat to Remember
1 (highest)()Parentheses override everything
2**Exponentiation, right-to-left
3+x, -x (unary)Positive/negative sign
4*, /, //, %Multiplication family, left-to-right
5+, -Addition family, left-to-right
6<, <=, >, >=, ==, !=Comparisons
7notBoolean NOT
8andBoolean AND
9 (lowest)orBoolean OR

The key insight: multiplication happens before addition, just like in math class.

Loading Python environment...

Output:

14

Python evaluates 3 * 4 first (giving 12), then adds 2. The answer is 14, not 20. If you want left-to-right order, use parentheses: (2 + 3) * 4 gives 20.

Read and Predict: What does this expression evaluate to?

Loading Python environment...

Work through it step by step: 2 ** 2 = 4 (exponentiation first), 8 / 4 = 2.0 (division next), 10 - 4 + 2.0 = 8.0 (left-to-right). The result is 8.0 -- a float, because division produced a float.

String Operations

Strings have their own set of operators and methods. Here are the ones you will encounter most when reading Python code.

Concatenation joins two strings with +:

Loading Python environment...

Output:

Hello World

Repetition repeats a string with *:

Loading Python environment...

Output:

--------------------

Indexing retrieves a single character. Indices start at 0. Negative indices count from the end:

Loading Python environment...

Output:

P
n

Slicing extracts a range of characters. The syntax is s[start:stop] where start is included and stop is excluded:

Loading Python environment...

Output:

yth

F-strings embed expressions inside strings:

Loading Python environment...

Output:

Zia is 25 years old

Common string methods you will see in code:

MethodWhat It DoesExampleResult
.upper()All uppercase"hello".upper()"HELLO"
.lower()All lowercase"Hello".lower()"hello"
.strip()Remove whitespace from both ends" hi ".strip()"hi"
.split(",")Split into a list"a,b,c".split(",")["a", "b", "c"]
"-".join(["a","b"])Join a list into a string"-".join(["a","b"])"a-b"

Boolean Logic

Python's boolean operators are and, or, and not. They look straightforward, but they have a behavior that surprises every beginner: and and or return actual values, not just True or False.

and returns the first falsy value, or the last value if all are truthy:

Loading Python environment...

Output:

0
3

or returns the first truthy value, or the last value if all are falsy:

Loading Python environment...

Output:

default
hello

This is short-circuit evaluation: Python stops as soon as it knows the answer. With and, if the first value is falsy, the result is decided -- no need to check the rest. With or, if the first value is truthy, the result is decided.

Comparison chaining is a Python feature most languages do not have:

Loading Python environment...

Output:

True

Python reads 0 < x < 10 as "is x greater than 0 AND less than 10?"

Trace Tables

A trace table tracks the value of every variable after each line of code executes. It is the most reliable way to predict output -- your brain takes shortcuts, but a trace table forces you to evaluate each line in order.

Loading Python environment...

Build the trace table line by line:

LineCodexyresult
1x: int = 55----
2y: int = x + 358--
3x = x * 2108--
4result: int = x + y10818

Line 1: x gets 5. No other variables exist yet. Line 2: y gets x + 3 = 5 + 3 = 8. The value of x at this moment is 5. Line 3: x gets x * 2 = 5 * 2 = 10. This overwrites the old value of x. Line 4: result gets x + y = 10 + 8 = 18. Notice that x is now 10, not 5.

The common mistake is on Line 4: using x = 5 instead of x = 10. Line 3 changed x. If you skip that step -- if your brain takes a shortcut -- you get 13 instead of 18.

Common Prediction Traps

These expressions surprise beginners because Python's behavior does not match intuition. Predict each result before reading the answer.

ExpressionYour PredictionActual ResultWhy It Surprises
round(2.5)3?2Banker's rounding: Python rounds to the nearest even number when exactly halfway
round(3.5)4?4Rounds to 4 (nearest even), which happens to match intuition this time
0.1 + 0.2 == 0.3True?False0.1 + 0.2 equals 0.30000000000000004 due to how computers store decimals in binary (IEEE 754)
bool("0")False?True"0" is a non-empty string. Only "" (empty string) is falsy
bool("")False?FalseEmpty string is falsy
int(-3.9)-4?-3int() truncates toward zero, not toward negative infinity (unlike //)
-3 ** 29?-9-(3 ** 2), not (-3) ** 2. Exponentiation binds tighter than negation

Python's round() uses round half to even: when a number is exactly halfway, it rounds to the nearest even number. This reduces cumulative rounding bias in large datasets. And 0.1 + 0.2 is not a Python bug -- it is how all languages store decimals in binary (IEEE 754). The number 0.1 has no exact binary representation, so tiny approximation errors accumulate.

Exercises

Exercise 1: Single Operators

Predict the result of each expression, then check it in Python.

Loading Python environment...

Exercise 2: Combined Arithmetic

Predict the result. Apply precedence rules.

Loading Python environment...

Exercise 3: Strings and Booleans

Predict each result:

Loading Python environment...

Exercise 4: Mixed Expressions

Predict each result. Watch for type surprises.

Loading Python environment...

Exercise 5: Trace Table

Build a trace table for this code block:

Loading Python environment...

Linewidthheightareahalfperimeter
1?--------
2??------
3???----
4????--
5????--
6?????

Try With AI

Prompt 1: Expression Quiz

Give me 5 Python expressions and I will predict the result before you
tell me. Rules:
- Use only arithmetic, string, or boolean operators
- Include at least one expression with operator precedence
- Include at least one expression with a common trap (like 0.1 + 0.2 or round)
- After I give my predictions, tell me which ones I got right and explain
the ones I got wrong

What you're learning: Predicting expression results is the core reading skill. You are training your brain to trace what Python actually does instead of what you assume it does. Getting predictions wrong is valuable -- each mistake reveals a gap in your mental model.

Prompt 2: Floating Point Explanation

Explain why 0.1 + 0.2 does not equal 0.3 in Python. I am a beginner,
so explain it step by step:
1. What is IEEE 754?
2. Why can't 0.1 be stored exactly in binary?
3. What does Python actually store when I type 0.1?
4. How do the small errors add up?
5. What should I use instead of == to compare floats?
Give a concrete example using Python code for each step.

What you're learning: The floating-point issue is not a Python bug -- it exists in every programming language that uses IEEE 754 binary arithmetic. Understanding why it happens helps you spot it in real code and know the safe alternative (comparing with a small tolerance instead of exact equality).

Prompt 3: Trace Table Practice

Show me a Python code block with 5-6 lines that uses variables,
arithmetic, and at least one reassignment (like x = x + 1). Do NOT
show me the output yet. Let me build a trace table first. Then check
my trace table row by row and tell me where I went wrong (if anywhere).

What you're learning: Building trace tables is the method that separates guessing from predicting. When you trace code line by line, you build a mental model of how Python actually executes -- which variable changes when, which old value gets overwritten, and what the state is at any given line.

Key Takeaways

  1. Division always returns a float. 6 / 3 gives 2.0, not 2. Use // for integer division, but remember that // floors toward negative infinity (-7 // 2 is -4, not -3).

  2. Operator precedence follows math rules with extensions. Exponentiation before multiplication, multiplication before addition. Parentheses override everything. When in doubt, add parentheses to make your intent explicit.

  3. Boolean operators return actual values, not just True/False. and returns the first falsy value (or the last value), or returns the first truthy value (or the last value). Short-circuit evaluation means Python stops checking as soon as it knows the answer.

  4. Trace tables catch what your brain misses. When a variable gets reassigned (like x = x * 2), your brain may skip the update. A trace table forces you to record the new value on every line.

  5. Common traps test your mental model. round(2.5) is 2 (banker's rounding), 0.1 + 0.2 != 0.3 (floating-point representation), bool("0") is True (non-empty string). Knowing these traps means you will catch them when reading AI-generated code.

Looking Ahead

You can now predict what expressions evaluate to and trace variable state through a code block. But real Python code is not a list of expressions -- it is organized into functions. In Lesson 4, you will learn to read function signatures as contracts: what a function accepts, what it returns, and what it promises. A function signature tells you everything you need to know before reading a single line of the function body.