Operators and Expressions
An operator indicates the operation being used, while operands are the values that the operator acts on.
Reading a Line of Code
Most statements, or logical lines, that you write will contain expressions. An expression is a part of a statement that can be understood by breaking it into operators and operands. The simple expression 2 + 3 gives us a clear starting point: the numbers are the operands, and the plus sign is the operator.
An operator indicates the operation being used, while operands are the values that the operator acts on.
Expression Parts
In 2 + 3, the plus sign is the operator. The values 2 and 3 are the operands. Together, the operator and its operands form the expression 2 + 3. This way of reading an expression helps you separate the values involved from the operation connecting them.
Decomposing a Simple Expression
Identify the operator and operands in 2 + 3.
- 1Find the operator
The plus sign indicates the operation in the expression.
- 2Find the operands
The values on either side of the plus sign, 2 and 3, are the operands.
- 3Reassemble the expression
The operator and operands together make the complete expression 2 + 3.
Operator: +. Operands: 2 and 3. Complete expression: 2 + 3.
Choosing the First Operation
An expression can contain more than one operator. For example, 2 + 3 * 4 contains both addition and multiplication. The operations are not treated as if their symbols appeared in an arbitrary order. Multiplication has higher precedence than addition, so Python evaluates the multiplication before the addition.
What do you think happens?
In 2 + 3 * 4, which operator has higher precedence?
Reveal answer
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 source describes Python's precedence table as running from the lowest precedence, which is the least binding, to the highest precedence, which is the most binding. In a given expression, Python evaluates operators and expressions according to their precedence. For 2 + 3 * 4, multiplication is higher in precedence than addition, so multiplication comes first.
Mistakes with Expression Order
Common mistake Treating every operator as if it had the same precedence.
What it looks likeReading 2 + 3 * 4 as though addition and multiplication must be handled by the same rule.
Why it failsThe source states that multiplication has higher precedence than addition.
Do this insteadIdentify the operators and compare their precedence before deciding which operation is evaluated first.
Common mistake Assuming the first operator written must be evaluated first.
What it looks likeChoosing the plus sign before considering the multiplication sign in 2 + 3 * 4.
Why it failsThe position of an operator in the written expression does not override precedence.
Do this insteadEvaluate the multiplication operator first because it has higher precedence than addition.
Common mistake Calling the entire expression an operator.
What it looks likeDescribing 2 + 3 as the operator instead of separating its parts.
Why it failsAn expression can be broken down into operators and operands; the plus sign is the operator, while 2 and 3 are operands.
Do this insteadName the complete expression separately from its operator and operands.
Practice Check
For the expression 2 + 3 * 4, identify both operators. Then state which operator Python evaluates first and explain why.
Hints
- Separate the expression into its symbols and values.
- Compare the precedence of addition and multiplication.
- The source example gives the precedence relationship you need.
For the expression 2 + 3, label each part as an operator, an operand, or the complete expression.
Hints
- There is one operator in this expression.
- The two numbers are the operands.
- The full sequence is the expression.
Key Takeaways
- 1Most statements, or logical lines, that you write will contain expressions.
- 2An expression can be broken down into operators and operands.
- 3In 2 + 3, the plus sign is the operator and 2 and 3 are the operands.
- 4When operators have different precedence levels, the higher-precedence operator is evaluated first.
- 5In 2 + 3 * 4, multiplication has higher precedence than addition.
At a glance
- Expressions are parts of statements that can be analyzed as operators and operands.
- In 2 + 3, 2 and 3 are operands, and + is the operator.
- Operator precedence determines which operation Python evaluates first.
- Multiplication has higher precedence than addition, so it is evaluated first in 2 + 3 * 4.