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.
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
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.
Separate the parts: The expression contains the operands 2, 3, and 4, together with the addition operator and the multiplication operator.
Compare precedence: Multiplication has higher precedence than addition.
Trace 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
Treating an entire expression as a single unnamed part.
An expression can be broken down into operators and operands.
Fix:
Identify 2 and 3 as operands and + as the operator.Assuming that an expression always performs operations from left to right.
Multiplication has higher precedence than addition.
Fix:
Compare the operators' precedence before deciding the evaluation order.Confusing an expression with the complete statement that contains it.
The source describes expressions as components that commonly appear within statements.
Fix:
Read 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
- Most statements, or logical lines, contain expressions.
- An expression can be broken down into operators and operands.
- In 2 + 3, 2 and 3 are operands and + is the operator.
- Operator precedence determines which operators are evaluated first.
- Multiplication has higher precedence than addition, so multiplication is evaluated first in 2 + 3 * 4.
Key Takeaways
- 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.