Operators and Expressions
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 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.
Find the operator: The plus sign indicates the operation in the expression.
Find the operands: The values on either side of the plus sign, 2 and 3, are the operands.
Reassemble 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
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
Treating every operator as if it had the same precedence.
The source states that multiplication has higher precedence than addition.
Fix:
Identify the operators and compare their precedence before deciding which operation is evaluated first.Assuming the first operator written must be evaluated first.
The position of an operator in the written expression does not override precedence.
Fix:
Evaluate the multiplication operator first because it has higher precedence than addition.Calling the entire expression an operator.
An expression can be broken down into operators and operands; the plus sign is the operator, while 2 and 3 are operands.
Fix:
Name 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
- Most statements, or logical lines, that you write will contain expressions.
- An expression can be broken down into operators and operands.
- In 2 + 3, the plus sign is the operator and 2 and 3 are the operands.
- When operators have different precedence levels, the higher-precedence operator is evaluated first.
- In 2 + 3 * 4, multiplication has higher precedence than addition.
Key Takeaways
- 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.