October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Subtract Numbers in Python

Subtract two values in Python with the minus operator. Learn how variables, parentheses, floats, and Decimal affect the result.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Python’s minus operator (-) between two numbers: 10 - 3 evaluates to 7. You can print the result directly or save it in a variable.

Subtract numbers with the minus operator

In an expression such as a - b, Python subtracts the right operand (b) from the left operand (a). The Python tutorial describes the interpreter as a simple calculator: you can enter an expression and it displays the value.

print(8 - 5)       # 3
remaining = 10 - 3
print(remaining)   # 7

Use the expression directly when you only need to display or pass along the result. Assign it to a variable when you need to use that result later.

Subtract variables and understand grouping

Variables work the same way as literal numbers: put the variable holding the amount to subtract on the right side of -.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
total = 20
spent = 7
remaining = total - spent
print(remaining)  # 13

Python follows operator precedence: multiplication is evaluated before subtraction. Parentheses change the grouping when you want another order. The tutorial’s examples show the difference:

print(50 - 5 * 6)      # 20: 5 * 6 is evaluated first
print((50 - 5) * 6)    # 270: parentheses group subtraction first

Why subtraction may return an integer or a float

When both operands are integers, subtraction produces an integer. If an expression includes a float, the result may be a float instead; for example, 4 * 3.75 - 1 evaluates to 14.0. A displayed result ending in .0 is therefore often a consequence of using a float operand, not a different subtraction operator. See the built-in numeric types documentation.

print(8 - 5)                 # 3
balance = 20.50 - 7.25
print(balance)               # 13.25

Handle decimal precision when needed

Python’s built-in float values use binary representation, so many decimal fractions cannot be represented exactly. A result can consequently contain a small difference in its final decimal places. This is a property of floating-point representation, not a flaw in subtraction. For comparisons involving approximate float results, use a suitable tolerance rather than expecting arbitrary decimal calculations to match exactly. The floating-point arithmetic tutorial explains the issue.

For decimal-oriented arithmetic—such as values entered as money—use Decimal and construct values from strings:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from decimal import Decimal

change = Decimal("1.10") - Decimal("0.20")
print(change)  # 0.90

The Decimal documentation explains that decimal inputs can be represented exactly and that subtraction can preserve fixed-point scale from the operands. Keep the operands in a consistent numeric type: combining Decimal with a float in arithmetic generally raises a TypeError.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the numeric type for the calculation

  • Integers: Use for whole-number arithmetic.
  • Floats: Use for ordinary calculations where a small binary approximation is acceptable.
  • Decimal: Use for decimal-oriented calculations when decimal representation, precision, or rounding control matters.
  • Fractions: Use Fraction when rational values need exact representation and it suits the calculation.

Python documents these numeric types and their behavior in the built-in types reference and the Decimal module documentation.

Fix common subtraction problems

  • The result ends in .0: Check whether one operand is a float; mixed arithmetic can produce a float result.
  • The last decimal digits look unexpected: The expression may use floats, which approximate many decimal fractions. Use a tolerance for approximate comparisons or consider Decimal for decimal arithmetic.
  • Decimal raises TypeError: Check that you are not mixing a Decimal and a float. Convert or construct both operands as a consistent type.
  • The answer reflects an unexpected order: Add parentheses to make the intended grouping explicit.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.