How to Subtract Two Numbers in Python

To subtract two numbers in Python, put the minus operator between them: a - b. It works the same way for integers and floats, and the result is negative when the second number is bigger:

difference = 1299.99 - 150.00   # 1149.99

Every output below comes from Python 3.12.5 in a real Command Prompt, using US prices in dollars and 2026 dates.

In this tutorial, I’ll cover user input, -=, operator.sub, float cents, negative results and functions.

Subtract two numbers with the minus operator

Save this as subtract_minus.py and run python subtract_minus.py in Command Prompt:

price = 1299.99      # laptop list price in USD
discount = 150.00
print("Sale price:", price - discount)

print(100 - 40)      # int - int   -> int
print(40 - 100)      # order matters: negative result
print(10 - 2.5)      # int - float -> float
print(type(10 - 4), type(10 - 4.0))

Output:

Sale price: 1149.99
60
-60
7.5
<class 'int'> <class 'float'>
Command Prompt output of Python subtracting two numbers with the minus operator, including a negative result and an int minus a float
Swapping the numbers flips the sign, and an int minus a float gives a float.

Python follows the same type rules when you’re adding two numbers in Python: two ints give an int, and any float in the mix gives a float.

How do you subtract two numbers entered by the user?

input() always returns text, so convert both values before subtracting. I use float() for money; the Python input() function guide covers prompts in more detail:

balance = float(input("Gift card balance (USD): "))
spent = float(input("Amount spent (USD): "))
left = balance - spent
print(f"Remaining balance: ${left:.2f}")

Output:

Gift card balance (USD): 250
Amount spent (USD): 87.45
Remaining balance: $162.55
Command Prompt running a Python script that reads two dollar amounts with input() and float() and prints the remaining balance
The user types 250 and 87.45, and the script prints the remaining balance with two decimals.

Forget the conversion and Python refuses to subtract two strings:

a, b = "250", "87"       # what input() gives you: text
try:
    print(a - b)
except TypeError as err:
    print("TypeError:", err)
print(int(a) - int(b))

Output:

TypeError: unsupported operand type(s) for -: 'str' and 'str'
163

Use int() for whole numbers. If the text has commas or dollar signs, clean it first, as shown in converting a string to a float in Python.

Subtract in place with -= and operator.sub

budget -= 1200.50 is short for budget = budget - 1200.50. See increment and decrement operators in Python for why there’s no --:

import operator
from functools import reduce

budget = 5000.00                 # Q1 2026 marketing budget
budget -= 1200.50                # January ads
budget -= 845.25                 # February trade show
print(f"Left after February: ${budget:,.2f}")

print(operator.sub(5000, 1200))  # same as 5000 - 1200
print(reduce(operator.sub, [5000, 1200, 845, 300]))   # 5000-1200-845-300

Output:

Left after February: $2,954.25
3800
2655

operator.sub(a, b) is the function version of a - b. It’s handy when another function needs a callable, like reduce() in Python, which subtracts a whole list from left to right.

Why does float subtraction give 0.19999999999999998?

Floats are binary, so most decimal fractions aren’t stored exactly and price math can drift:

from decimal import Decimal

print(0.3 - 0.1)
print(19.99 - 9.99)
print(round(19.99 - 9.99, 2))
print(f"${19.99 - 9.99:.2f}")

total = Decimal("19.99") - Decimal("9.99")
print(total, Decimal("100.00") - Decimal("0.01"))
print(Decimal(19.99) - Decimal(9.99))    # built from floats: the error comes along

Output:

0.19999999999999998
9.999999999999998
10.0
$10.00
10.00 99.99
9.999999999999998223643160600
Command Prompt output of Python float subtraction showing 0.19999999999999998, then round(), an f-string and Decimal giving exact USD amounts
Plain float subtraction shows the binary rounding error; round(), :.2f and Decimal give clean dollar amounts.

For display, round(x, 2) or an f-string is enough; see printing 2 decimal places in Python. For money you store or total up, use Decimal built from strings. Built from floats, it keeps the error.

Subtract two numbers in a function and handle negative results

A function makes subtraction reusable. Add abs() when only the gap matters:

def subtract(a, b):
    """Return a minus b."""
    return a - b

def difference(a, b):
    """Return the gap between a and b, never negative."""
    return abs(a - b)

print(subtract(72, 58))        # Austin high vs low, Oct 19, 2026
print(subtract(58, 72))
print(difference(58, 72))
print(subtract(2026, 1776), "years since 1776")

Output:

14
-14
14
250 years since 1776
Command Prompt output of a Python subtract function returning positive and negative results and an abs() difference function
subtract(58, 72) returns -14, while difference() returns 14 either way.

Your function must return the result rather than print it, or the caller gets None; the Python return statement shows why. More on abs() is in absolute value in Python.

Ready for the other arithmetic basics? Pick one:

Frequently asked questions

How do you subtract two numbers in Python?

Use the minus operator: result = a - b. For example, 100 - 40 gives 60.

How do I subtract numbers a user types in?

Convert the input first: float(input("Amount: ")). input() returns a string, and subtracting strings raises a TypeError.

What does -= mean in Python?

x -= 5 subtracts 5 from x and stores the result back in x.

Why is 0.3 – 0.1 not 0.2 in Python?

Floats are binary, so the result is 0.19999999999999998. Use round() for display or Decimal for exact money math.

How do I always get a positive difference?

Wrap the subtraction in abs(): abs(58 - 72) returns 14.

Where is the Decimal type documented?

See the decimal module in the Python docs.