Skip to content

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.

Beginner 10 min read·Lesson 46 of 306 in Python Programming

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

Python
difference = a - b
negated = -value
gap = abs(a - b)
only_in_first = set_a - set_b
  • 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

Python
print(500 - 120)
print(3 - 8)
print(-(-7))
Output
380
-5
7

Absolute difference

Python
target = 75
for score in [70, 82]:
print(score, "is", abs(score - target), "away from the target")
Output
70 is 5 away from the target
82 is 7 away from the target

More examples

Profit, change and set difference

Python
cost, price = 640, 595
result = price - cost
print("Loss" if result < 0 else "Profit", abs(result))
paid, bill = 1000, 763
print("Change due:", paid - bill)
enrolled = {"Asha", "Ben", "Chen", "Dev"}
present = {"Ben", "Dev"}
print("Absent:", sorted(enrolled - present))
print(round(1.1 - 1.0, 2))
Output
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:

TextOutput
Loss 45
Change due: 237
Absent: ['Asha', 'Chen']
0.1

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

change = bill - paid

Prefer

change = paid - bill

Subtraction isn’t commutative; swapping operands flips the sign.

Trying to subtract from a string

Avoid

name - "a"

Prefer

name.replace("a", "")

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

  1. 1
    Calculate how many days are left in a 365-day year after day 287.
  2. 2
    Given 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?
Practise this module