# Subtraction in Python

We have to develop a program for subtraction in python. We will give two numbers while declaring the variables and the python program will subtract these numbers using an arithmetic operator. We have also developed a program using built-in function numpy.subtract() methods.

## Python Program to Subtract Two Numbers

This Python program is the simplest and easiest way to calculate the subtraction of two numbers. We will take two numbers while declaring the variables. Then, subtract numbers and their subtraction value will be stored to the sub variable. Finally, the subtraction value will be displayed on the screen.

``````# Python program to subtract two numbers

# take inputs
num1 = 10
num2 = 7

# subtract two numbers
sub = num1 - num2

# print the subtraction result
print('The subtraction of numbers =', sub)``````

Output:-

The subtraction of numbers = 3

## How to Subtract 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 subtract two numbers

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))

# subtract two numbers
sub = num1 - num2

# print the subtraction result
# value will print in float
print('The subtraction of numbers = %0.2f' %sub)``````

Output for the different input values:-

Enter first number: 20
Enter second number: 16
The subtraction of numbers = 4.00

Enter first number: 25
Enter second number: 6.4
The subtraction of numbers = 18.60

Enter first number: 12
Enter second number: 23
The subtraction of numbers = -11.00

## Subtraction in Python using Function

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

``````# Python program to subtract two numbers

def sub_num(num1, num2):  #user-defined function
#subtract two numbers
return num1-num2

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))

# calling function and print the subtraction value
print('The subtraction of numbers = %0.2f' %sub_num(num1, num2))``````

Output:-

Enter first number: 45
Enter second number: 12
The subtraction of numbers = 33.00

## Subtract Function in Python

This python program also performs the same task but with different methods. In this program, we are using a built-in function to subtract numbers. The numpy.subtract() function is used when we want to compute the difference between two numbers or arrays. It returns the difference of numbers.

``````# Python program to subtract two numbers

#importing numpy.subtract() function
import numpy

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))

# subtract two numbers
sub = numpy.subtract(num1, num2)

# print the subtraction result
# value will print in float
print('The subtraction of numbers = %0.2f' %sub)``````

Output:-

Enter first number: 10
Enter second number: 5
The subtraction of numbers = 5.00

Also See:- Python Program to Add Two Numbers

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!