Concepts / Type Errors and Debugging

Type Errors and Debugging

Multiplication depends on the types of both operands.

  • Programming

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?

  • Subtraction from an unseen value

  • Unary negation of the number 5

  • Multiplication by negative 5

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.

Worked example

Separating subtraction from negation

Interpret 10 - 3 and -5.

  1. 1
    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.

  2. 2
    Identify the operation

    With two operands, the minus sign performs subtraction. With one operand, the minus sign performs unary negation.

  3. 3
    Read the result

    10 - 3 removes 3 from 10. -5 represents the negated number 5.

Result

The position and number of operands determine whether the minus sign means subtraction or unary negation.

10 - 3two operandsSubtractionremove the right operand-5one operandUnary negationnegate the numbertwo operandsone operand
What changes when the minus sign has a value on its left compared with when the left operand is missing?

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.

4 * 3two numbers12numeric product'la' * 3string and integer'lalala'repeated stringnumeric multiplicationstring repetition
What changes when the multiplication operator is applied to two numbers compared with a string and an integer?
Expression patternOperand typesBehavior
number * numberTwo numbersProduces a numeric product
string * integerA string and an integerRepeats the string
integer * stringAn integer and a stringRepeats the string
string * stringTwo stringsCauses an error
string * floatA string and a floatCauses an error

The operand types determine whether multiplication calculates a product or repeats a string.

python
Output
12
lalala
lalala

Reading 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.

Worked example

Tracing subtraction and multiplication

Follow the values in this sequence: assign a number, subtract from it, multiply a number, and repeat a string.

  1. 1
    Start with a value

    A variable is assigned a number, so the trace records that variable's numeric state.

  2. 2
    Apply subtraction

    The right operand is removed from the left operand. The trace records the updated numeric value.

  3. 3
    Apply numeric multiplication

    Two numeric operands produce a numeric product, which is recorded as the next result.

  4. 4
    Apply string multiplication

    A string multiplied by an integer becomes a repeated string, which appears in the output.

Result

The trace lets you verify each intermediate value instead of checking only the final output.

Write expressionRun PythonCheck operand typesRead execution traceRun againCorrect expressionexecuteinspect resultcompatibleincompatibleconfirm predictiontest correction
How does Python expose an incompatible operand type, and how can the expression be corrected?

Mistakes to Correct

  • Common mistake

    Treating -5 as subtraction from a missing value.

    What it looks like
    -5
    Why it fails

    There is no left operand. The single minus sign performs unary negation.

    Do this instead

    Read -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 fails

    String multiplication requires an integer multiplier, and multiplying two strings causes an error.

    Do this instead

    Use 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.0
    Why it fails

    String multiplication requires an integer multiplier. A float multiplier causes an error.

    Do this instead

    Use an integer multiplier when repeating a string.

  • Common mistake

    Checking only the final output while debugging.

    What it looks like

    A program produces an unexpected result, but its intermediate assignments are not inspected.

    Why it fails

    The final output does not show the exact step where the state diverged from the prediction.

    Do this instead

    Read the execution trace line by line and compare each variable state and printed result with your prediction.

Practice the Trace

MEDIUM

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

  1. 1
    Binary subtraction removes the right operand from the left, while unary negation uses a minus sign with no left operand.
  2. 2
    Multiplying two numbers produces a numeric product.
  3. 3
    Multiplying a string by an integer repeats the string, and the string and integer can appear in either order.
  4. 4
    Multiplying two strings or using a float as the string multiplier causes an error.
  5. 5
    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.

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.

Related concepts

More concepts starting with T