Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Minimum of two numbers in Python
Next article icon

Subtract Two Numbers in Python

Last Updated : 14 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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) 

Output
5 

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) 

Output
5 

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) 

Output
5 

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) 

Output
5 14 

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

  • operator module
  • lambda function
  • function

Next Article
Minimum of two numbers in Python

M

mailforriya2708
Improve
Article Tags :
  • Python
  • Python Programs
  • python-basics
Practice Tags :
  • python

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 subt
    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 perfor
    5 min read
  • Number System in Python
    The arithmetic value that is used for representing the quantity and used in making calculations is defined as NUMBERS. The writing system for denoting numbers logically using digits or symbols is defined as a Number system. Number System is a system that defines numbers in different ways to represen
    6 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. [GFGTABS] Python a = 7 b = 3 print(min(a, b)) [/GFGTABS]Output3 Explanation: min() function compares the two numbe
    2 min read
  • Python | Nested Tuples Subtraction
    Sometimes, while working with records, we can have a problem in which we require to perform index-wise subtraction of tuple elements. This can get complicated with tuple elements being tuples and inner elements again being tuple. Let’s discuss certain ways in which this problem can be solved.  Metho
    7 min read
  • Python | Subtraction of dictionaries
    Sometimes, while working with dictionaries, we might have a utility problem in which we need to perform elementary operations among the common keys of dictionaries in Python. This can be extended to any operation to be performed. Let's discuss the subtraction of like key values and ways to solve it
    6 min read
  • Python program to add two binary numbers
    Given two binary numbers, write a Python program to compute their sum. Examples: Input: a = "11", b = "1" Output: "100" Input: a = "1101", b = "100" Output: 10001Approach: Naive Approach: The idea is to start from the last characters of two strings and compute digit sum one by one. If the sum become
    4 min read
  • Python Program to Find LCM of Two Numbers
    We are given two numbers and our task is to find the LCM of two numbers in Python. In this article, we'll discuss different approaches to finding the LCM of two numbers in Python. Example: Input: a = 12, b = 15Output: 60Explanation: LCM of 12 and 15 is 60Python Program to Find LCM of Two NumbersBelo
    3 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences