Articles

# 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

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 code is as follows:

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

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. 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 