Articles

Python program to find difference of two numbers

Python program to find difference of two numbers


In this python programming example guide, we will learn to write:

  • python program to find difference of two numbers

Program to find the difference of two numbers in Python

Here we are going to accept two int value from user and then perform subtraction. 

 

Please check our video tutorial on python program to find difference of two numbers:

 

 

Given below is the python program to find difference of two numbers:

# Owner : TutorialsInHand Author : Devjeet Roy

n1 = int(input("Enter first number: "))
n2 = int(input("Enter second number: "))

if n1 > n2:
    diff = n1 - n2
else:
    diff = n2 - n1

print("The difference is:",diff)

The output of the program to find the difference of two numbers in python is as follows:

 

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter first number: 34
Enter second number: 20
The difference is: 14

Few important points about this program

1. To find the positive difference between two numbers we have subtracted the smaller number from the larger one. So, we have used an if clause to find which number is bigger.

2. Alternatively, we can use the abs() function to find the mod value of the difference of the two numbers such that, the difference is always positive.

3. Even without using the "diff" variable, we can directly find the difference inside the print function by specifying abs(n1 - n2) in place of "diff".

 

Given below is screenshot for program to find the difference of two numbers in python.

difference of two numbers in python

You can find entire python programs for beginners list here

Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :    Published Date : Oct 31,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!