Python has a variety of operators to perform various arithmetic operations like addition, subtraction, multiplication, division, modulus and exponentiation. Here we will discuss the subtraction operator in Python with some code snippets

The subtraction operator (-) in Python performs the subtraction of 2 numbers. The The numbers can be of the type integer or floating point. The first operand is called the minuend and the second operand is called the subtrahend. The result of the operation is called as the difference.

Difference = Minuend – Subtrahend

Let’s see some examples and understand the subtraction operator.

Subtraction of 2 Integer numbers

In below code the subtraction operator is used to perform subtraction of 2 integers, the difference too is a integer value.

a=20
b=5
c=a-b      
print(c)        #15
print(type(c))  #int

Subtraction of 2 Floating Point numbers

In below code the subtraction operator is used to perform subtraction of 2 floating point numbers, the difference too is a floating point value.

a=4.5
b=1.8
c=a-b      
print(c)        #2.7
print(type(c))  #float

Subtraction of an integer number and a floating point number

In below code the subtraction operator is used to perform subtraction of an integer number (minuend is int) and a floating point number (subtrahend is float), the difference is a floating point value.

a=5
b=1.8
c=a-b      
print(c)        #3.2
print(type(c))  #float

In below code the subtraction operator is used to perform subtraction of a floating point number (minuend is float) and an integer number (subtrahend is int), the difference is a floating point value.

a=3.6
b=2
c=a-b      
print(c)        #1.6
print(type(c))  #float

Subtraction of 2 complex numbers

In below code the subtraction operator is used to perform subtraction of 2 complex numbers, the difference too is a complex number.

a=5+8j
b=2+3j
c=a-b      
print(c)        #3+5j
print(type(c))  #complex

Subtraction of an integer number and a complex number

In below code the subtraction operator is used to perform subtraction of an integer number (minuend is int) and a complex number (subtrahend is complex), the difference too is a complex number.

a=3
b=2-3j
c=a-b      
print(c)        #1+3j
print(type(c))  #complex

In below code the subtraction operator is used to perform subtraction of a complex number (minuend is complex) and an integer number (subtrahend is int), the difference too is a complex number.

a=5-3j
b=3
c=a-b      
print(c)        #2-3j
print(type(c))  #complex

Subtraction of a floating point number and a complex number

In below code the subtraction operator is used to perform subtraction of a floating point number (minuend is float) and a complex number (subtrahend is complex), the difference too is a complex number.

a=3.6
b=2-3j
c=a-b      
print(c)        #1.6+3j
print(type(c))  #complex

In below code the subtraction operator is used to perform subtraction of a complex number (minuend is complex) and a floating point number (subtrahend is float), the difference too is a complex number.

a=5-3j
b=3.6
c=a-b      
print(c)        #1.4-3j
print(type(c))  #complex

Subtraction of 2 string values

With string, the subtraction operation throws a TypeError (unsupported operand type). The subtraction cannot be used with string values as operands. The following errors may be observed if any one operand is of type string.

  • TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’
  • TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’
  • TypeError: unsupported operand type(s) for -: ‘int’ and ‘str’
  • TypeError: unsupported operand type(s) for -: ‘str’ and ‘float’
  • TypeError: unsupported operand type(s) for -: ‘float’ and ‘str’
  • TypeError: unsupported operand type(s) for -: ‘str’ and ‘complex’
  • TypeError: unsupported operand type(s) for -: ‘complex’ and ‘str’

Subtraction of 2 boolean values

With boolean variables, the subtraction operator works as in integer values, treating True as integer value 1 and False as integer value 0.

a=True      #treated as integer value 1
b=False     #treated as integer value 0
c=a-b       #c will have the value 1 and type int
print(c)    #1
d=a-a       #d will have the value 0 and type int
print(d)    #0
e=b-b       #e will have the value 0 and type int
print(e)    #0

Similar Operators in Python

Last modified: April 10, 2023

Author