Subtract Two Numbers in Python

(Last Updated On: 31/01/2023)

Python is a powerful and versatile programming language that is widely used in many industries, including finance, healthcare, and technology. One of the most basic and fundamental operations in programming is performing mathematical calculations, such as subtracting two numbers, and Python makes it easy to do so with its built-in mathematical functions and operators.

Python Program to Subtract Two Numbers Using Integer

In this article, we will be discussing how to write a Python program to subtract two numbers using integers. Integers are whole numbers, and they are one of the most common data types used in programming.

First, let’s start by creating a new Python file and naming it “subtraction.py“. We will then begin by defining the two numbers that we want to subtract, using the assignment operator (=) to assign values to variables. For example, we can define two variables, num1, and num2, and assign them the values 92 and 63, respectively.

Next, we will use the subtraction operator (-) to subtract the value of num2 from num1 and store the result in a new variable, called “result”.

# Subtract Two Numbers in Python Using Integer

# we are taking a number from user as input
# entered value will be converted to int from string
num1 = int(input("Enter the First Number: "))
num2 = int(input("Enter the Second Number: "))

output = num1 - num2

#display the subtraction
print("Subtract Two Number using integer",output)

To display the result on the screen, we can use the built-in “print” function in Python.

Output:

The final program, to subtract two numbers using integers, should look like this:

Enter the First Number: 92
Enter the Second Number: 63
Subtract Two Number using integer 29

When you run this program, you will see the output “The result of subtraction is: 29” on the screen, which is the expected result.

Python Program to Subtract Two Numbers Using Float

Floats are numbers with decimal points and they are also a commonly used data type in programming. One of the common use of them is when you have to subtract two numbers. First, let’s start by creating a new Python file and naming it “subtraction_float.py“.

We will then begin by defining the two numbers that we want to subtract, using the assignment operator (=) to assign values to variables. For example, we can define two variables, num1 and num2, and assign them the values 5.6 and 1.1, respectively.

Next, we will use the subtraction operator (-) to subtract the value of num2 from num1 and store the result in a new variable, called “result”.

# Subtract Two Numbers in Python Using Float

# we are taking a number from user as input
# entered value will be converted to float from string
num1 = input("Enter the First Number: ")
num2 = input("Enter the Second Number: ")

output = float(num1) - float(num2)

#display the subtraction
print("Subtract Two Number using float variable",output)

To display the result on the screen, we can use the built-in “print” function in Python.

Output:

The final program, to subtract two numbers using a float, should look like this:

Enter the First Number: 5.6
Enter the Second Number: 1.1
Subtract Two Number using float variable 4.5

When you run this program, you will see the output “The result of subtraction is: 4.5” on the screen, which is the expected result.

It’s important to note that in python the type of the numbers is determined by the value, not by the variable, that’s why we don’t need to specify that num1 and num2 are float numbers, python infer that from the values.

In conclusion, this simple program demonstrates how easy it is to perform mathematical calculations in Python using floats. This is just the beginning of what you can do with Python, and there are many more advanced concepts and functions that you can learn to perform more complex operations. With online learning platforms like this website, you can learn Python and other programming languages at your own pace and take your skills to the next level.

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions

About The Author

Leave a Reply