We have to develop a Python program to the difference between two numbers. We will give two numbers while declaring the variables. Then, the Python program will find the absolute difference between these two numbers using an arithmetic operator. We have also developed a program using built-in function abs() methods.

## Python Absolute Difference Between two numbers

We will take two numbers while declaring the variables num1 and num2. Then, find the difference between numbers using the **if-else statement** and its value will be stored to diff variable. Finally, the difference value will be displayed on the screen.

Program description:-** write a program to find the difference of two numbers in python.**

```
# Python program to find difference between two numbers
# first number
num1 = 25
# second number
num2 = 13
# num1 is greater than num2
if num1 > num2:
diff = num1 - num2
# num1 is less than num2
else:
diff = num2 - num1
# print difference value
print('The difference between numbers =', diff)
```

Output:-

The difference between numbers = 12

## How to Find the Difference between Two Numbers in Python

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user. Inputs are scanned using the **input() function** and stored in variables num1 and num2. Since input() returns a string, we convert the string to a number using the **float() function**. The values will be printed in the float.

```
# Python program to find difference between two numbers
# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
# num1 is greater than num2
if num1 > num2:
diff = num1 - num2
# num1 is less than num2
else:
diff = num2 - num1
# print difference value
print('The difference between numbers = %0.2f' %diff)
```

Output for the different input values:-

Enter first number: 45

Enter second number: 23

The difference between numbers = 22.0

Enter first number: 12

Enter second number: 18

The difference between numbers = 6.0

Enter first number: 25.6

Enter second number: 16

The difference between numbers = 9.60

## Write a Program to Print the Absolute Difference between the Two Given Numbers in Python

We can also take the help of a **user-defined function** to find the difference between two numbers in python. A function is a block of code that performs a specific task.

Program description:-** write a program to print the absolute difference between the two given numbers. (absolute difference is the difference without the negative sign)**

```
# Python program to find difference between two numbers
def difference(a, b):
# a is greater than b
if a > b:
diff = a - b
# a is less than b
else:
diff = b - a
# return value
return diff
# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
# calling function and print difference value
print('The difference between numbers =', difference(num1, num2))
```

Output:-

Enter first number: 42.8

Enter second number: 36.2

The difference between numbers = 6.60

## Difference of Two Numbers in Python using abs() Function

This python program also performs the same task but with different methods. In this program, we are using a built-in function to find the difference between numbers. **The abs() method returns the absolute value of the given number**. The argument can be an integer, a floating-point number, or a complex number. If the argument is an integer or floating-point number, abs() returns the absolute value in integer or float.

```
# Python program to find difference between two numbers
# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
# find difference between numbers
diff = abs(num1 - num2)
# print difference value
print('The difference between numbers =', diff)
```

Output:-

Enter first number: 10

Enter second number: 15

The difference between numbers = 5.0

**Q1) Difference between 43 and 17 in Python.**

```
num1 = 43
num2 = 17
diff = abs(num1 - num2)
print('The difference between 43 and 17 =', diff)
```

or

```
num1 = 17
num2 = 43
diff = abs(num1 - num2)
print('The difference between 43 and 17 =', diff)
```

Output:- The difference between 43 and 17 = 26

Also See:- Find Average of 3 Numbers in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!