Subtraction - Python Examples
Python Subtraction - Arithmetic Operator
Python Subtraction Operator takes two operands, first one on left and second one on right, and returns difference of the the second operand from the first operand.
The symbol used for Python Subtraction operator is -
.
Syntax
Following is the syntax of Python Subtraction Arithmetic Operator with two operands.
result = operand_1 - operand_2
where operand_1
and operand_2
are numbers and the result is the difference of operand_2
from operand_1
.
Example 1: Subtraction of Numbers
In this example, we shall take two integers, subtract them, and print the result.
Python Program
a = 20
b = 12
result = a - b
print(result)
Output
8
Chaining of Subtraction Operator
You can subtract more than one number from a number in a single statement. This is because, Subtraction Operator supports chaining.
Python Program
a = 63
b = 12
c = 5
d = 10
result = a - b - c - d
print(result)
Output
36
Example 2: Subtraction of Floating Point Numbers
Float is one of the numeric datatypes. You can compute the difference of floating point numbers using Python Subtraction operator.
Python Program
a = 12.5
b = 10.2
result = a - b
print(result)
Output
2.3000000000000007
Example 3: Subtraction of Complex Numbers
You can find the subtraction of complex numbers using -
. In the following example program, we shall take two complex numbers and find their difference.
Python Program
a = 1 + 8j
b = 3 + 5j
result = a - b
print(result)
Output
(-2+3j)
Summary
In this tutorial of Python Examples, we learned what Python Subtraction Arithmetic Operator is, how to use it to find the difference of two or more numbers.