AppDividend
Latest Code Tutorials

Python subtract: How to Subtract Two Numbers In Python

Arithmetic operations like sum, subtract, multiplication, and division are an integral part of any programming language. These operations generally perform on Integers or Floating values, and Python is no exception. In this example, we will see how to subtract numbers in Python and see its various use cases.

Python subtract

To subtract two numbers in Python, use the subtraction(-) operator. The subtraction operator (-) takes two operands, the first operand on the left and the second operand on the right, and returns the difference of the second operand from the first operand.

To print the subtraction output, we will use the print() function.

Syntax

output = first_operand - second_operand

Arguments

The first_operand and second_operand are numbers.

Return value

The output is the difference of second_operand from first_operand.

Example

first_operand = 19
second_operand = 21

output = second_operand - first_operand

print(output)

Output

2

This is a simple example of how to do a subtraction between two numbers in Python.

Subtract two numbers using user input in Python

To take the inputs from a user, use the input() function in Python.

first_operand = int(input("Enter the first_operand: "))
second_operand = int(input("Enter the second_operand: "))

output = second_operand - first_operand

print(output)

Output

Enter the first_operand: 18
Enter the second_operand: 19

1

Subtract multiple operands using chaining

Till now, we have used two operands and applied subtraction. Next, we will use more than two operands and subtract more than one number from a number in a single statement using chaining.

To operate subtract operator in more than two operands, use the chaining of subtracting operator.

first_operand = int(input("Enter the first_operand: "))
second_operand = int(input("Enter the second_operand: "))
third_operand = int(input("Enter the third_operand: "))

output = third_operand - second_operand - first_operand

print(output)

Output

Enter the first_operand: 11
Enter the second_operand: 19
Enter the third_operand: 46

16

How to subtract two float numbers in Python

To subtract two floating numbers in Python, use the subtract operator(-). Float is one of the most used numeric data types in Python.

first_operand = float(input("Enter the first_operand: "))
second_operand = float(input("Enter the second_operand: "))
third_operand = float(input("Enter the third_operand: "))

output = third_operand - second_operand - first_operand

print(output)

Output

Enter the first_operand: 11.9
Enter the second_operand: 22.9
Enter the third_operand: 44.9

10.1

How to subtract the complex numbers in Python

To subtract complex numbers in Python, use the subtract(-) operator.

first_operand = 11 + 2j
second_operand = 22 + 3j
third_operand = 44 + 4j

output = third_operand - second_operand - first_operand

print(output)

Output

(11 - 1j)

That’s it for this tutorial.

See also

Python sum

Calculate percentage in Python

Python cube root

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.