Module 4 · Operators — topic 3 of 16
Subtraction
The - operator for numbers, unary negation, differences and changes, absolute differences with abs(), and how subtraction works with sets.
Learning objectives
- Subtract numbers and interpret negative results
- Use unary minus and abs()
- Use - for set difference
Definition
The - operator for numbers, unary negation, differences and changes, absolute differences with abs(), and how subtraction works with sets.
Introduction
Subtraction with - finds the difference between two numbers. The same symbol written before a single value negates it, and between two sets it removes elements.
Why it matters
Remaining balances, change due, time elapsed, score differences and profit or loss are all subtractions. Understanding negative results and absolute differences lets you report them correctly.
Detailed explanation
a - b subtracts b from a. If b is larger, the result is negative: 3 - 8 is -5.
Type rules match addition: two ints give an int; any float makes the result a float.
Unary minus negates: -x. Combined with variables it flips the sign: if x is -4, -x is 4.
abs(a - b) gives the size of a difference regardless of order, useful when you only care how far apart two values are.
Strings and lists do not support -; there is no “remove these characters” subtraction. Sets do: {1, 2, 3} - {2} is {1, 3}, the elements in the first set but not the second.
Floating-point subtraction can expose rounding error, for example 1.1 - 1.0 shows 0.10000000000000009. Round for display.
Subtraction is how programs measure change over time. Today’s reading minus yesterday’s gives the change; dividing that change by yesterday’s value gives a growth rate. Keeping the order consistent (new minus old) means a positive result always means an increase.
Syntax
- Order matters: a - b is not b - a.
- Unary minus flips the sign.
- abs() removes the sign.
- Between sets, - keeps elements only in the left set.
Code examples
Differences and negatives
380 -5 7
Absolute difference
70 is 5 away from the target 82 is 7 away from the target
More examples
Profit, change and set difference
Loss 45 Change due: 237 Absent: ['Asha', 'Chen'] 0.1
Step-by-step explanation
- price - cost is -45, so the conditional expression prints Loss and abs() shows 45.
- paid - bill gives the change to return.
- enrolled - present keeps students who are enrolled but not present; sorted() orders the names.
- round(..., 2) hides the floating-point error in 1.1 - 1.0.
Expected output
Running “Profit, change and set difference” prints exactly:
Practical applications
- Point-of-sale systems compute change due with subtraction.
- Attendance apps find absent students with a set difference.
- Monitoring tools compare readings to targets using abs().
- Stock-keeping software subtracts sold quantities from stock and raises an alert when the result drops below a reorder level.
Best practices
- Name differences clearly: change_due, profit, days_left.
- Use abs() when only the size of a difference matters.
- Round float differences for display.
Common mistakes
Subtracting in the wrong order
Avoid
Prefer
Subtraction isn’t commutative; swapping operands flips the sign.
Trying to subtract from a string
Avoid
Prefer
Strings don’t support -. Use string methods to remove characters.
Key takeaways
- - subtracts numbers; order matters.
- Unary minus negates; abs() removes the sign.
- set_a - set_b gives elements only in set_a.
Try it yourself
- 1Calculate how many days are left in a 365-day year after day 287.
- 2Given two sets of skills, print the skills the first person has that the second doesn’t.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
14 questions cover the “Operators” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?