Type Errors and Debugging
Multiplication depends on the types of both operands.
A Prediction Before Execution
Arithmetic operators are the foundation of computation. Subtraction can shrink a quantity, multiplication can scale a number, and string multiplication can repeat a pattern. The most reliable way to learn their behavior is to predict an expression first, then compare that prediction with the result produced during execution.
What do you think happens?
What kind of operation does the expression -5 represent?
Reveal answer
Unary negation of the number 5
A minus sign with no left operand negates a number. This differs from binary subtraction, which has a value on both sides, such as 10 - 3.
Following Values Through Expressions
Read an arithmetic expression by identifying its operands and then asking what type of data each operand contains. In 10 - 3, both operands are numbers, so the right operand is removed from the left. In -5, there is no left operand; the single minus sign negates 5 instead. The same symbol appears in both expressions, but the number of operands changes its meaning.
Separating subtraction from negation
Interpret 10 - 3 and -5.
- 1Identify the operands
The expression 10 - 3 has two operands: 10 on the left and 3 on the right. The expression -5 has only one operand: 5.
- 2Identify the operation
With two operands, the minus sign performs subtraction. With one operand, the minus sign performs unary negation.
- 3Read the result
10 - 3 removes 3 from 10. -5 represents the negated number 5.
The position and number of operands determine whether the minus sign means subtraction or unary negation.
Multiplication Across Data Types
Multiplication depends on the types of both operands. When both operands are numbers, multiplication produces a numeric product. When the left operand is a string and the right operand is an integer, multiplication repeats the string. For example, 'la' * 3 produces 'lalala'. Python also allows the operands in the opposite order: 3 * 'la' produces the same repeated string.
| Expression pattern | Operand types | Behavior |
|---|---|---|
| number * number | Two numbers | Produces a numeric product |
| string * integer | A string and an integer | Repeats the string |
| integer * string | An integer and a string | Repeats the string |
| string * string | Two strings | Causes an error |
| string * float | A string and a float | Causes an error |
The operand types determine whether multiplication calculates a product or repeats a string.
12
lalala
lalalaReading an Execution Trace
An execution trace records the state of variables and the output at each step. Each assignment changes or establishes a variable value, while each print statement produces output. Comparing the trace with your prediction helps you locate the exact step where the program's state diverged from what you expected.
Tracing subtraction and multiplication
Follow the values in this sequence: assign a number, subtract from it, multiply a number, and repeat a string.
- 1Start with a value
A variable is assigned a number, so the trace records that variable's numeric state.
- 2Apply subtraction
The right operand is removed from the left operand. The trace records the updated numeric value.
- 3Apply numeric multiplication
Two numeric operands produce a numeric product, which is recorded as the next result.
- 4Apply string multiplication
A string multiplied by an integer becomes a repeated string, which appears in the output.
The trace lets you verify each intermediate value instead of checking only the final output.
Mistakes to Correct
Common mistake Treating -5 as subtraction from a missing value.
What it looks like-5Why it failsThere is no left operand. The single minus sign performs unary negation.
Do this insteadRead -5 as the negated number 5. Use an expression such as 10 - 5 when you mean subtraction.
Common mistake Expecting multiplication to repeat two strings.
What it looks like"ha" * "ha"Why it failsString multiplication requires an integer multiplier, and multiplying two strings causes an error.
Do this insteadUse a string and an integer, such as "ha" * 2, when you want repetition.
Common mistake Using a float as the string multiplier.
What it looks like"ha" * 2.0Why it failsString multiplication requires an integer multiplier. A float multiplier causes an error.
Do this insteadUse an integer multiplier when repeating a string.
Common mistake Checking only the final output while debugging.
What it looks likeA program produces an unexpected result, but its intermediate assignments are not inspected.
Why it failsThe final output does not show the exact step where the state diverged from the prediction.
Do this insteadRead the execution trace line by line and compare each variable state and printed result with your prediction.
Practice the Trace
For each expression, predict whether it produces a numeric product, a repeated string, or an error. Then explain whether the minus sign in -8 represents subtraction or unary negation.
Hints
- Check whether both multiplication operands are numbers.
- For string multiplication, check that the other operand is an integer.
- Count the operands around the minus sign.
- 6 * 4
- "go" * 3
- 3 * "go"
- "go" * "go"
- "go" * 1.5
- -8
- 10 - 8
Key Takeaways
- 1Binary subtraction removes the right operand from the left, while unary negation uses a minus sign with no left operand.
- 2Multiplying two numbers produces a numeric product.
- 3Multiplying a string by an integer repeats the string, and the string and integer can appear in either order.
- 4Multiplying two strings or using a float as the string multiplier causes an error.
- 5An execution trace shows variable state and output at each step, making it useful for finding where a prediction and a program's behavior diverge.
At a glance
- A minus sign with two operands performs subtraction; a minus sign with one operand performs unary negation.
- Numeric multiplication calculates a product, while string multiplication with an integer repeats a string.
- String multiplication does not support two strings or a float multiplier.
- Tracing variable state and output step by step helps identify and correct arithmetic mistakes.