DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

How to Add Two Numbers in Python: Operator, Variables, and Functions

Python adds two numbers with the + operator. Assign the expression to keep its result, use a function for reuse, and choose Decimal or Fraction when exactness matters.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Python’s + operator: 2 + 3 evaluates to 5. To keep the result, assign the expression to a variable: total = a + b. A function is optional—it packages the same addition for reuse.

Add two numbers with the + operator

Place + between the two numeric values:

print(2 + 3)  # 5

Python evaluates the expression and print() displays its result. The operator is the addition method; no special function, package, calculator app, or paid service is required. The Python Software Foundation’s Python tutorial describes + as an arithmetic operator.

Store the result in a variable

When you need to use the answer later, assign the sum to a name:

a = 2
b = 3
total = a + b
print(total)  # 5

+ performs the addition; = assigns the resulting value to total. These operators have different jobs.

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

Put the addition in a reusable function

For a one-off sum, a function adds unnecessary structure. If you want to reuse the operation, define one that returns the result:

def add_two_numbers(a, b):
    return a + b

print(add_two_numbers(2, 3))  # 5

The function accepts two arguments, adds them with the same + operator, and returns the sum.

Choose a numeric type that fits the values

Python’s built-in numeric types include int, float, and complex. The standard library also provides Decimal and Fraction. For addition, the practical choice depends on what the numbers represent:

Type Use it for What to know
int Whole numbers Python documents integers as having unlimited precision. See Built-in Types.
float Most general calculations and approximate measurements Many decimal fractions are approximated in binary, though floating-point arithmetic is adequate for most tasks. See Floating-Point Arithmetic: Issues and Limitations.
Decimal Calculations where exact decimal representation matters, such as accounting-style values Construct values from decimal strings to avoid first introducing a float approximation.
Fraction Values that need exact rational representation Available in Python’s standard library; see Numeric and Mathematical Modules.

Combining an integer and a float produces floating-point arithmetic. For ordinary values, the simple expression remains a + b; choose a different representation only when the required precision calls for it.

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.

Use Decimal when decimal exactness matters

For example, create Decimal values from strings rather than converting floats:

from decimal import Decimal

subtotal = Decimal('0.10') + Decimal('0.20')
print(subtotal)  # 0.30

This keeps the decimal inputs exact. By contrast, a float stores many decimal fractions only approximately, so a calculation can display a result that differs slightly from the decimal value you expect. The Python tutorial explains the limits of binary floating-point representation; its standard-library tour shows Decimal for exact decimal arithmetic.

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

Adding many floating-point values

For a short calculation, use + as usual. When summing a collection of float values, Python documents sum() and math.fsum() as options; math.fsum() can reduce accumulated rounding error in relevant cases. It is not necessary for adding just two ordinary values. See the Python documentation on floating-point arithmetic.

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.

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

Leave a Reply

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

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
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

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.