Understanding Expressions and Operators
Most statements (logical lines) that you write will contain expressions . A simple example of an expression is 2 + 3 . An expression can be broken down into operators and operands.
Try it: Order of Operations
How Python decides which operator in an expression runs first — precedence, left-to-right order and right-to-left **, plus what /, // and % really return.
How it works
- Parentheses first; then **, which groups right to left and binds tighter than a minus sign on its left.
- Then *, /, // and %, left to right; then + and −, left to right.
- / always gives a float; // rounds down; % takes the sign of the divisor.
- Each step replaces one operation with its value until one number is left.
Default run (7 steps): Expression: 2 + 3 * (7 - 4) ** 2 // 5. Python evaluates left to right, but higher-precedence operators bind first. … Result: 7 (an int).
Simplified: Numbers (at most 6 digits) and arithmetic operators only; parsed and evaluated by the lab's own evaluator (no eval), checked against real Python results.
Loading the simulation…
Reading a Simple Expression
Most statements, also called logical lines, that you write contain expressions. An expression is a part of a statement that can be examined as a combination of operators and operands. The simple expression 2 + 3 gives us a clear starting point.
In 2 + 3, the values 2 and 3 are the operands, and the plus sign is the operator. Breaking the expression into these parts helps you read more complicated expressions instead of treating the entire line as one undifferentiated piece.
Tracing Evaluation Order
An expression can contain more than one operator. Consider 2 + 3 * 4. The important question is whether Python performs the addition first or the multiplication first.
What do you think happens?
In 2 + 3 * 4, which operation is evaluated first?
Reveal answer
The multiplication
Multiplication has higher precedence than addition, so Python evaluates the multiplication before the addition.
Precedence describes how strongly an operator binds in an expression. The multiplication operator has higher precedence than the addition operator. Therefore, in 2 + 3 * 4, the multiplication is handled before the addition.
Using the Precedence Table
Examining 2 + 3 * 4
Decide whether addition or multiplication is evaluated first in the expression 2 + 3 * 4.
- 1Separate the parts
The expression contains the operands 2, 3, and 4, together with the addition operator and the multiplication operator.
- 2Compare precedence
Multiplication has higher precedence than addition.
- 3Trace the order
The multiplication part is evaluated before the addition part.
Multiplication is evaluated before addition.
Python's precedence table is ordered from the lowest precedence, meaning least binding, to the highest precedence, meaning most binding. For a given expression, operators and expressions lower in the table are evaluated before those listed higher in the table. When reading a particular expression, use the relevant precedence relationship to determine its order.
Mistakes Beginners Make
Common mistake Treating an entire expression as a single unnamed part.
What it looks likeReading 2 + 3 without identifying the values and the plus sign.
Why it failsAn expression can be broken down into operators and operands.
Do this insteadIdentify 2 and 3 as operands and + as the operator.
Common mistake Assuming that an expression always performs operations from left to right.
What it looks likeAssuming that addition happens before multiplication in 2 + 3 * 4.
Why it failsMultiplication has higher precedence than addition.
Do this insteadCompare the operators' precedence before deciding the evaluation order.
Common mistake Confusing an expression with the complete statement that contains it.
What it looks likeLooking at an expression without recognizing that most statements, or logical lines, contain expressions.
Why it failsThe source describes expressions as components that commonly appear within statements.
Do this insteadRead the expression as a part of the surrounding logical line.
Practice Check
For the expression 2 + 3 * 4, identify all operands and operators. Then state which operator is evaluated first and explain why.
Hints
- List the values separately from the symbols that perform operations.
- Compare the precedence of addition and multiplication.
Describe the relationship between an expression and a statement, using the idea that most statements, or logical lines, contain expressions.
Hints
- Think of the expression as a part of the complete logical line.
- Use 2 + 3 as your simple example.
Key Takeaways
- 1Most statements, or logical lines, contain expressions.
- 2An expression can be broken down into operators and operands.
- 3In 2 + 3, 2 and 3 are operands and + is the operator.
- 4Operator precedence determines which operators are evaluated first.
- 5Multiplication has higher precedence than addition, so multiplication is evaluated first in 2 + 3 * 4.
At a glance
- Expressions are common parts of statements, or logical lines.
- Operators and operands are the parts used to analyze an expression.
- The expression 2 + 3 contains two operands and one operator.
- Precedence determines evaluation order when an expression contains multiple operators.
- Multiplication has higher precedence than addition.