Subtract Two Numbers in Python
Last Updated :
14 May, 2025
Subtracting two numbers in Python is a basic operation where we take two numeric values and subtract one from the other. For example, given the numbers a = 10 and b = 5, the result of a - b would be 5. Let's explore different methods to do this efficiently.
Using Minus Operator (-)
This is the most straightforward and common way to subtract two numbers. Just use the minus sign (-) between the numbers.
Python
a = 10
b = 5
res = a - b
print(res)
Explanation: a - b subtracts 5 from 10 and the result is stored in the variable res.
Using operator.sub()
Python's operator module provides a function called sub() for subtraction. It's like telling Python "subtract a from b" explicitly through a function call.
Python
import operator
a = 10
b = 5
res = operator.sub(a, b)
print(res)
Explanation: operator.sub(a, b) expression uses the sub() subtract b from a and the result is stored in the variable res.
Using lambda function
lambda function is an anonymous (unnamed) function. You can use it to subtract two numbers without having to define a full function.
Python
x = lambda a, b: a - b
res = x(10, 5)
print(res)
Explanation: lambda a, b: a - b defines an anonymous function that subtracts b from a. When x(10, 5) is called, it subtracts 5 from 10 and the result is stored in the variable res.
Using function
Defining a function allows you to reuse the subtraction logic in multiple places, making your code more organized. It’s a bit more work but useful if you need to subtract multiple pairs of numbers.
Python
def fun(a, b):
return a - b
r1 = fun(10, 5)
r2 = fun(20,6)
print(r1)
print(r2)
Explanation: fun(a, b) takes two arguments and returns their difference (a - b). When fun(10, 5) is called, it subtracts 5 from 10, storing the result in r1. Similarly, fun(20, 6) subtracts 6 from 20, storing the result in r2.
Related articles
Similar Reads
Python Program to Subtract Two Binary Numbers We are given two binary numbers, num1 and num2 and we have to subtract these two binary numbers and return the result. In this article, we will see how we can subtract two binary numbers in Python.Examples:Input: a = "1110" , b = "0110"Output: "1000"Explanation: We can see that when "1110" is subtra
3 min read
How to Add Two Numbers in Python The task of adding two numbers in Python involves taking two input values and computing their sum using various techniques . For example, if a = 5 and b = 7 then after addition, the result will be 12.Using the "+" Operator+ operator is the simplest and most direct way to add two numbers . It perform
4 min read
Minimum of two numbers in Python In this article, we will explore various methods to find minimum of two numbers in Python. The simplest way to find minimum of two numbers in Python is by using built-in min() function. Pythona = 7 b = 3 print(min(a, b))Output3 Explanation:min() function compares the two numbers a and b.So, it retur
2 min read
Python Program to Find the Gcd of Two Numbers The task of finding the GCD (Greatest Common Divisor) of two numbers in Python involves determining the largest number that divides both input values without leaving a remainder. For example, if a = 60 and b = 48, the GCD is 12, as 12 is the largest number that divides both 60 and 48 evenly. Using e
2 min read
Working with Negative Numbers in Python Negative numbers play a crucial role in various mathematical and programming scenarios. In Python, handling negative numbers is a fundamental skill for developers. In this article, we will explore some commonly used methods for working with negative numbers in Python, along with examples to illustra
3 min read
Python | Mutual tuple subtraction in list Sometimes, while working with data, we can have a problem in which we need to perform tuple subtraction among all the tuples in list. This can have application in many domains. Let's discuss certain ways in which this task can be performed. Method #1 : Using combinations() + list comprehension This
3 min read