Arithmetic Operators in Python
Operators are symbols or keywords that perform actions; operands are the data that operators work with.
Reading an Expression
Most logical lines of Python code contain expressions. An expression combines values and operations to produce a result. The two fundamental parts of an expression are operators and operands: the operator performs an action, and the operands provide the data that the operator acts on.
What do you think happens?
In the expression 2 + 3, which part tells Python what action to perform, and which parts provide the data?
Reveal answer
Answer: The plus sign is the operator. The values 2 and 3 are the operands.
The operator performs the action of addition, while the operands are the two values used by that action.
Operator and Operand Roles
An operator is a symbol or keyword that performs an action. An operand is the data that an operator works with. An operator takes one or more operands as input and produces a result.
In 2 + 3, the plus sign is the operator, while 2 and 3 are the operands. The operator sits between the two operands, which is the most common pattern for binary operators. A binary operator requires exactly two operands. The plus sign therefore uses 2 and 3 as its inputs and produces 5 as the result.
Tracing a Combined Expression
Evaluating 2 + 3 * 4
Identify the operators and operands, then follow how the expression produces its result.
Identify the parts: The expression contains two operators, + and *. Its three operands are 2, 3, and 4.
Apply multiplication: Python follows standard mathematical precedence, so multiplication happens before addition. The * operator uses 3 and 4 as its operands and produces 12.
Use the intermediate result: The + operator then uses 2 and the result 12 as its operands.
Produce the final result: The addition operation produces 14.
The expression 2 + 3 * 4 produces 14.
This example shows that an operand does not have to be an original literal value at every stage. The result of one expression can become an operand for another operator. Here, 12 is produced by multiplication and then becomes one of the operands for addition.
Different Kinds of Operands
Operands can be literal values, variables, or the results of other expressions. An operand can also be a string of text or the result of a function call. What matters is that it provides data for the operator to work with.
If a variable x contains the value 10, then x is an operand in x + 5. The plus sign remains the operator and still requires two operands, but one operand is now a variable rather than a literal number.
| Expression part | Role | Source example |
|---|---|---|
| 2 or 3 | Literal value used as an operand | 2 + 3 |
| x | Variable used as an operand | x + 5 |
| 12 | Result of another expression used as an operand | 2 + 12 after 3 * 4 |
| + or * | Operator that performs an action | 2 + 3 or 3 * 4 |
Mistakes with Expression Parts
Treating the operator as if it were one of the values
The plus sign does not provide data. It specifies the action to perform on the two operands.
Fix:
Classify 2 and 3 as operands and + as the operator.Looking only at literal numbers when identifying operands
A variable can provide data for an operator, so it can function as an operand.
Fix:
Identify every value source used by the operator, including variables.Ignoring the order in a combined expression
Python follows standard mathematical rules of precedence, so multiplication happens before addition in this expression.
Fix:
Identify the nested operation that is evaluated first, then use its result as an operand for the remaining operation.Assuming an operator can work without operands
Operators require operands to function and cannot produce a result without data.
Fix:
Check that the operator has the required operand or operands.
Expression Identification Practice
For the expression 2 + 3 * 4, list every operator and every operand. Then state which operation is applied first and identify the value that becomes an operand for the remaining operation.
Hints
- There are two operators and three original operands.
- Use standard mathematical precedence to decide whether addition or multiplication happens first.
- The first operation produces an intermediate result.
Practice solution
Identify the parts and evaluation flow in 2 + 3 * 4.
Operators: The operators are + and *.
Operands: The original operands are 2, 3, and 4.
First operation: Multiplication is applied first to 3 and 4, producing 12.
Remaining operation: Addition then uses 2 and 12 as its operands.
The final result is 14, and the intermediate result 12 becomes an operand for addition.
Key Takeaways
- Operators are symbols or keywords that perform actions.
- Operands are the data that operators work with.
- Expressions combine operators and operands to produce results.
- Operators require operands to function; binary operators require exactly two.
- An operand can be a literal value, a variable, or the result of another expression.
Key Takeaways
- An operator specifies an action, while an operand supplies data for that action.
- In 2 + 3, the plus sign is the operator and 2 and 3 are the operands.
- Expressions can contain multiple operators, and precedence determines the order in which they are applied.
- The result of one operation can become an operand for another operation.
- An operator cannot produce a result without the operand or operands it requires.