Type Errors and Debugging
Then, type python and ensure there are no errors.
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
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.
Identify 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.
Identify the operation: With two operands, the minus sign performs subtraction. With one operand, the minus sign performs unary negation.
Read 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 |
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.
Start with a value: A variable is assigned a number, so the trace records that variable's numeric state.
Apply subtraction: The right operand is removed from the left operand. The trace records the updated numeric value.
Apply numeric multiplication: Two numeric operands produce a numeric product, which is recorded as the next result.
Apply 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
Treating -5 as subtraction from a missing value.
There is no left operand. The single minus sign performs unary negation.
Fix:
Read -5 as the negated number 5. Use an expression such as 10 - 5 when you mean subtraction.Expecting multiplication to repeat two strings.
String multiplication requires an integer multiplier, and multiplying two strings causes an error.
Fix:
Use a string and an integer, such as "ha" * 2, when you want repetition.Using a float as the string multiplier.
String multiplication requires an integer multiplier. A float multiplier causes an error.
Fix:
Use an integer multiplier when repeating a string.Checking only the final output while debugging.
The final output does not show the exact step where the state diverged from the prediction.
Fix:
Read 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
- Binary subtraction removes the right operand from the left, while unary negation uses a minus sign with no left operand.
- Multiplying two numbers produces a numeric product.
- Multiplying a string by an integer repeats the string, and the string and integer can appear in either order.
- Multiplying two strings or using a float as the string multiplier causes an error.
- An execution trace shows variable state and output at each step, making it useful for finding where a prediction and a program's behavior diverge.
Key Takeaways
- 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.