Sunday, 15 March 2026

Day 3: Subtract two numbers in Python

 

๐Ÿš€ Day 3/150 – Subtract Two Numbers in Python

1️⃣ Basic Subtraction (Direct Method)

The simplest way to subtract two numbers is by using the - operator.

a = 10 b = 5 result = a - b print(result)



Output

5


This method directly subtracts b from a and stores the result in a variable.

2️⃣ Taking User Input

In real programs, numbers often come from user input rather than being predefined.


a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) print("Difference:", a - b)










Hegers.




Here we use input() to take values from the user and int() to convert them into integers.

3️⃣ Using a Function

Functions help make code reusable and organized.

def subtract(x, y): return x - y print(subtract(10, 5))



The function subtract() takes two parameters and returns their difference.


4️⃣ Using a Lambda Function (One-Line Function)

A lambda function is a small anonymous function written in a single line.

subtract = lambda x, y: x - y print(subtract(10, 5))


Lambda functions are useful when you need a short, temporary function.

5️⃣ Using the operator Module

Python also provides built-in modules that perform mathematical operations.

import operator print(operator.sub(10, 5))

The operator.sub() function performs the same subtraction operation.


6️⃣ Using List and reduce()

Another approach is to store numbers in a list and apply a reduction operation.

from functools import reduce numbers = [10, 5] result = reduce(lambda x, y: x - y, numbers) print(result)






reduce() applies the function cumulatively to the items in the list.



๐ŸŽฏ Conclusion

There are many ways to subtract numbers in Python. The most common method is using the - operator, but functions, lambda expressions, and built-in modules provide more flexibility in larger programs.

In this series, we explore multiple approaches so you can understand Python more deeply and write better code.

๐Ÿ“Œ Next in the series: Multiply Two Numbers in Python



















































0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (221) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (9) BI (10) Books (262) Bootcamp (1) C (78) C# (12) C++ (83) Course (86) Coursera (300) Cybersecurity (29) data (5) Data Analysis (27) Data Analytics (20) data management (15) Data Science (322) Data Strucures (16) Deep Learning (134) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (66) Git (10) Google (50) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (263) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (13) PHP (20) Projects (32) Python (1265) Python Coding Challenge (1074) Python Mistakes (50) Python Quiz (443) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (46) Udemy (17) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)