# Python Program to Subtract Two Numbers

We will develop a python program to subtract two numbers. We will give two numbers num1 and num2. Python programs will subtract these numbers using the arithmetic operator (-). We will also develop a python program to subtract two numbers without using the (-) operator.

## Subtraction of Two Numbers in Python

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

## Python Program to Subtract Two Numbers with User Input

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 input values test-case-1:-

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

Output for the input values test-case-2:-

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

Output for the input values test-case-3:-

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

## Python Program to Subtract Two Numbers 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 for the input values test-case-1:-

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

## Python Program to Subtract Two Numbers

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 for the input values test-case-1:-

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

## Python Program to Subtract Two Numbers Without using – Operator

This python program also performs the same task but in different ways. In this program, we subtract two numbers without using (-) operator.

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

def subtract_num(a,b):   #user-defined function
if (b == 0):
return a
return subtract_num(a ^ b, (~a & b) << 1)

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

# calling function
subtract = subtract_num(num1, num2)

# print subtract of numbers
print('The subtract of numbers {0} and {1} is {2}'
.format(num1, num2, subtract))
``````

Output for the input values test-case-1:-

Enter first number: 125
Enter second number: 32
The subtract of numbers 125 and 32 is 93

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

### 2 thoughts on “Python Program to Subtract Two Numbers”

1. Enter first number: 50
Enter second number: -40
The subtraction of numbers = 90.00

I got this answer for Numpy.subtract() function