How to Subtract Numbers in Python

When I first started working with Python, one of the simplest yet most essential operations I learned was subtraction. Whether it was subtracting two numbers, calculating differences between lists, or subtracting user inputs, subtraction has always been a core part of Python programming.

In this tutorial, I’ll walk you through how to create a Python subtraction program step by step. I’ll also share multiple methods that I personally use, depending on the scenario, from basic arithmetic subtraction to using Python functions, loops, and even NumPy arrays.

What Is Subtraction in Python?

Subtraction in Python is a simple arithmetic operation where one number is deducted from another using the minus (-) operator. For example, 10 – 3 gives 7. It works on integers, floats, lists (with some logic), and even arrays.

As a Python developer with over a decade of experience, I often use subtraction in automation scripts, financial calculations, and data analysis tasks.

Let’s start with the most basic method.

Method 1 – Subtract Two Numbers in Python

This is the simplest and most common way to perform subtraction in Python. You can directly use the – operator between two numbers.

Here’s a simple example:

# Define two numbers
num1 = 15
num2 = 8

# Perform subtraction
result = num1 - num2

# Display the result
print("The result of subtraction is:", result)

When you run this program, you’ll see the output:

The result of subtraction is: 7

I executed the above example code and added the screenshot below.

subtraction of two numbers in python

This is the most basic use case, and it’s perfect for beginners learning Python arithmetic.

Method 2 – Subtract Numbers Entered by the User

In practical applications, you often need to take inputs from users. Let’s create a subtraction program in Python that accepts user input using the input() function.

Here’s how you can do it:

# Take input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Perform subtraction
difference = num1 - num2

# Display the result
print("The difference between the two numbers is:", difference)

I executed the above example code and added the screenshot below.

how to subtract in python

When you run this code, Python will ask for two numbers. It will then calculate and display their difference, a widespread use case in real-world Python applications.

Method 3 – Subtract Multiple Numbers Using a Loop

Sometimes, you may need to subtract multiple numbers dynamically, for example, when processing a list of expenses or scores. In such cases, a Python loop works perfectly.

Here’s how I usually do it:

# List of numbers
numbers = [100, 20, 15, 5]

# Start with the first number
result = numbers[0]

# Subtract the remaining numbers
for num in numbers[1:]:
    result -= num

print("The result after subtracting all numbers is:", result)

Output:

The result after subtracting all numbers is: 60

I executed the above example code and added the screenshot below.

subtraction in python

This approach is useful when you have a dynamic dataset, for instance, calculating the remaining budget after multiple expenses.

Method 4 – Subtract Lists Element-Wise in Python

In data analysis or automation scripts, you might need to subtract corresponding elements from two lists. Python doesn’t support direct list subtraction, but you can achieve it using a loop or list comprehension.

Here’s an example:

list1 = [10, 20, 30, 40]
list2 = [1, 5, 10, 15]

# Using list comprehension
result = [a - b for a, b in zip(list1, list2)]

print("Result after subtracting lists:", result)

Output:

Result after subtracting lists: [9, 15, 20, 25]

I executed the above example code and added the screenshot below.

python subtract

This technique is beneficial when working with datasets or numerical lists in Python.

Method 5 – Subtract Using a Python Function

If you need to reuse subtraction logic multiple times, it’s best to wrap it inside a function.
This makes your Python code modular and easier to maintain.

Here’s how you can define a subtraction function:

def subtract_numbers(a, b):
    """Return the result of subtracting b from a"""
    return a - b

# Example usage
num1 = 50
num2 = 17

print("Subtraction result:", subtract_numbers(num1, num2))

Output:

Subtraction result: 33

I executed the above example code and added the screenshot below.

subtract in python

Functions are a best practice in Python programming; they make your code cleaner and reusable.

Method 6 – Subtract Arrays Using NumPy (For Data Analysis)

If you’re working with large datasets or arrays, NumPy is the most efficient way to perform subtraction. It allows you to subtract entire arrays element-wise with a single operation.

Here’s an example:

import numpy as np

# Create two NumPy arrays
array1 = np.array([100, 200, 300])
array2 = np.array([50, 70, 120])

# Subtract arrays
result = array1 - array2

print("Result after subtracting arrays:", result)

Output:

Result after subtracting arrays: [ 50 130 180]

NumPy is highly optimized for numerical operations and is widely used in Python for machine learning and data science tasks.

Method 7 – Subtract Datetime Objects in Python

In real-world applications, especially in the USA, where I often work with timestamped data, you might need to find the difference between two dates or times. Python’s datetime module makes this easy.

Here’s an example:

from datetime import datetime

# Define two dates
date1 = datetime(2025, 10, 6, 14, 30)
date2 = datetime(2025, 10, 1, 10, 15)

# Subtract dates
difference = date1 - date2

print("Difference between the two dates:", difference)
print("Days difference:", difference.days)

Output:

Difference between the two dates: 5 days, 4:15:00
Days difference: 5

This is extremely useful when calculating durations, such as the number of days between two events or timestamps.

Method 8 – Subtract Using Lambda and Map Functions

For quick, one-line operations, I often use lambda and map() in Python. This is a more Pythonic way to perform list-based subtraction.

Here’s how it works:

list1 = [25, 50, 75]
list2 = [5, 20, 30]

# Using lambda and map
result = list(map(lambda x, y: x - y, list1, list2))

print("Result after subtraction using lambda:", result)

Output:

Result after subtraction using lambda: [20, 30, 45]

This method is concise and ideal for quick data transformations in Python scripts.

Bonus Tip – Subtracting Negative Numbers in Python

Python handles negative numbers naturally. If you subtract a negative number, it’s equivalent to addition.

Example:

a = 10
b = -5

print("Result:", a - b)

Output:

Result: 15

This can be handy when dealing with financial transactions or temperature differences.

Subtraction in Python is simple but incredibly powerful. Whether you’re working on basic arithmetic, data analysis, or automation scripts, understanding different subtraction methods helps you write cleaner and more efficient Python code.

We explored everything from simple number subtraction to list operations, functions, NumPy arrays, and even date differences. Each method has its own use case, and as a Python developer, you’ll often find yourself using several of them in real projects.

If you’re just starting, begin with the basics - operator and gradually explore the advanced techniques as you grow more comfortable with Python.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.