Concepts / Addition and Division Operators

Addition and Division Operators

Subtraction removes the right operand from the left; unary minus (a single minus with no left operand) negates a number.

  • Programming

Start with the Operand Roles

Arithmetic operators transform data. Subtraction shrinks a quantity by removing the right operand from the left operand. Multiplication can produce a numeric product or repeat a string, depending on the values being multiplied. The most reliable way to understand these operations is to inspect each operand, predict the result, and then trace how the expression is evaluated.

What do you think happens?

What is the result of 10 - 3, and what is the result if the operands are swapped?

  • 10 - 3 gives 7, and 3 - 10 gives -7
  • Both expressions give 7
  • 10 - 3 gives -7, and 3 - 10 gives 7
Reveal answer

Answer: 10 - 3 gives 7, and 3 - 10 gives -7

Subtraction removes the right operand from the left operand, so changing their positions changes the result.

Swapping Subtraction Operands

Subtraction is directional. In the expression 10 - 3, the left operand is 10 and the right operand is 3. The operation removes 3 from 10, producing 7. If the operands are swapped, the expression becomes 3 - 10, so 10 is removed from 3 and the result is -7. The minus symbol is the same, but the roles of the operands have changed.

subtractfrom left operandsubtractfrom left operand10left operand3left operand3right operand10right operand710 - 3-73 - 10
How does the result change when the left and right operands are swapped?

Reading a Subtraction Expression

Determine the result of 18 - 25.

Identify the operands: The left operand is 18 and the right operand is 25.

Apply the direction: Remove 25 from 18. Because the right operand is larger, the result is negative.

Compare the swapped expression: The swapped expression 25 - 18 removes 18 from 25 and produces 7 instead.

18 - 25 produces -7, while 25 - 18 produces 7.

Unary Minus and Negation

A minus sign does not always mean subtraction. In 10 - 3, the minus sign is a binary operator because it has two operands: 10 and 3. In -5, there is no left operand. This is unary minus, and it negates the number that follows it. The expression -5 represents the negative form of 5.

subtractfrom left operandnegate10left operand-unary minus-subtraction5one operand3right operand-5negated value7binary result
What happens when a minus sign appears with two operands compared with no left operand?

Numbers and Repeated Strings

Multiplication changes its behavior according to the operand types. 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, multiplying the string 'la' by 3 produces 'lalala'. In Python, 3 multiplied by 'la' also produces the same repeated string.

multiplymultiplyrepeattimes4number'la'string3number3integer multiplier12numeric product'lalala'repeated string
How does multiplication behave differently when its operands are numbers versus when a number and a string are involved?
Expression kindOperandsResult behavior
Numeric multiplicationTwo numbersProduces a numeric product
String multiplicationA string and an integerRepeats the string
Unsupported string multiplicationTwo strings or a string and a float multiplierCauses an error

Multiplication depends on the types of its operands.

Tracing a Mixed Expression

A trace records the value produced at each stage of an expression. For an expression containing multiplication and subtraction, evaluate the multiplication first and then use that result in the subtraction. This lets you verify both the intermediate value and the final output instead of trying to guess the whole expression at once.

evaluate multiplicationreplace with 12subtract20 - 4 * 34 * 31220 - 128
In what order are multiplication and subtraction evaluated, and what result does each step produce?

Step-by-Step Evaluation

Predict the result of 20 - 4 * 3.

Find the multiplication: The multiplication part is 4 * 3, which produces 12.

Rewrite the expression: Replace 4 * 3 with 12. The expression becomes 20 - 12.

Complete the subtraction: Subtract the right operand 12 from the left operand 20.

The expression produces 8.

What do you think happens?

What does the expression 'go' * 2 - 1 do?

Reveal answer

Answer: It is not a valid example of subtraction and string multiplication together because string repetition produces a string, while subtraction removes one operand from another. The subtraction operation is not defined for the resulting string and the number.

String multiplication repeats a string, but subtraction is described as removing the right operand from the left in the arithmetic context. Do not assume that every operator can be chained across incompatible result types.

Mistakes with Minus and Multiplication

  • Treating subtraction as if operand order does not matter.

    Subtraction removes the right operand from the left, so swapping the operands changes the result.

    Fix: Name the left and right operands before calculating.

  • Confusing unary minus with binary subtraction.

    Unary minus has no left operand and negates the number that follows it.

    Fix: Count the operands attached to the minus sign.

  • Using a non-integer multiplier for a string.

    String multiplication requires an integer multiplier.

    Fix: Use an integer when repeating a string.

  • Multiplying two strings.

    Multiplying two strings is not defined and causes an error.

    Fix: Use a string with an integer multiplier when the goal is repetition.

  • Skipping the intermediate step in a mixed expression.

    The multiplication step must be evaluated before using its result in the subtraction.

    Fix: Write the intermediate result before completing the remaining operation.

Practice the Trace

MEDIUM

For each expression, identify the operands, predict the result, and write any intermediate step needed: 14 - 9, -12, 3 * 5, 'ha' * 4, and 30 - 2 * 6.

Hints
  • For subtraction, identify which value is on the left and which is on the right.
  • For a minus sign with no left operand, apply unary negation.
  • For string multiplication, check that the multiplier is an integer.
  • For the mixed expression, evaluate the multiplication before the subtraction.

Practice Check

Evaluate 30 - 2 * 6.

Evaluate multiplication: The multiplication part is 2 * 6, which produces 12.

Substitute the intermediate result: The expression becomes 30 - 12.

Evaluate subtraction: Remove the right operand 12 from the left operand 30.

The result is 18.

Key Takeaways

  1. Subtraction is directional: it removes the right operand from the left operand.
  2. Unary minus has one operand and negates the value that follows it.
  3. Multiplying two numbers produces a numeric product.
  4. Multiplying a string by an integer repeats the string; multiplying two strings or using a float multiplier causes an error.
  5. Tracing intermediate values helps you predict results and locate mistakes.

Key Takeaways

  • Subtraction depends on operand direction.
  • Unary minus and binary subtraction use the same symbol for different purposes.
  • Multiplication produces numeric products or repeated strings depending on operand types.
  • String repetition requires an integer multiplier.
  • Execution traces make intermediate states visible for prediction and debugging.