**Python Programs**

Python Flow Control

➤ Even Number in Python

➤ Odd Number in Python

➤ Even Odd Program

➤ Largest of 3 Numbers

➤ Leap Year Program

➤ Multiplication Table

➤ Factors of a Number

➤ Prime Factors in Python

➤ Factorial of a Number

➤ Factorial using Function

➤ Math.factorial() in Python

➤ Factorial using Recursion

➤ LCM of Two Numbers

➤ HCF/GCD of 2 Numbers

➤ Solve Quadratic Equation

➤ Sum of Digits of a Number

➤ Sum of N Natural Numbers

➤ Fibonacci Series in Python

➤ Fibonacci Series – Recursion

➤ Simple Calculator in Python

➤ Perfect Square in Python

➤ Absolute Value in Python

Conversion Programs

➤ Celsius to Fahrenheit

➤ Fahrenheit to Celsius

➤ Decimal to Binary

➤ Binary to Decimal

➤ Decimal to Octal

➤ Octal to Decimal

➤ Decimal to Hexadecimal

➤ Hexadecimal to Decimal

Array Programs

➤ Linear Search in Python

➤ Binary Search in Python

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

## Python Program to Find 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.

```
# 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

## Difference between Two Values 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

## How to Find the Difference between Two 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.

```
# 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

## Python Difference 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

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 you find anything incorrect? Let us know in the comments. Thank you!