Subtraction Operator in Python (-)

posted on 03 May 2026 , updated on 03 May 2026
operators

The Subtraction Operator (-) is a binary operator used to subtract the second operand from the first. In Python, it is versatile enough to handle simple integers, decimal numbers, and even complex mathematical values.

1. Basic Numeric Subtraction

Python performs subtraction between integers and floats with ease. When you subtract a float from an integer, the result is automatically converted to a float.


# Subtracting Integers
print(20 - 5)      # Output: 15

# Subtracting Floats
print(10.5 - 4.5)  # Output: 6.0

# Mixed types
print(10 - 2.5)    # Output: 7.5

2. Complex Number Subtraction

Just like addition, you can subtract complex numbers. Python subtracts the real parts and the imaginary parts separately.


c1 = 5 + 8j
c2 = 2 + 3j

print(c1 - c2)    # Output: (3+5j)
Tip: In Python, you must use j for the imaginary part. Using i (as commonly used in math) will result in a NameError.

3. Set Difference

A unique feature of the - operator in Python is its use with **Sets**. It returns a new set containing elements that are in the first set but NOT in the second set.


set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}

# Elements in A but not in B
print(set_a - set_b)  # Output: {1, 2}

4. Strings

With string operand, the subtraction operation throws a TypeError (unsupported operand type). 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’

5. Common Errors To Avoid

TypeError: Unlike addition, you cannot use the subtraction operator with Strings, Lists, or Tuples.
# "Hello" - "H"    # Raises TypeError
# [1, 2] - [1]     # Raises TypeError

💡 Interesting Facts

  • Unary Minus: The - symbol can also be used as a unary operator to negate a number (e.g., x = -5).
  • Boolean Subtraction: Since True is 1 and False is 0, True - False equals 1.
  • Precision: Sometimes, subtracting floats can lead to tiny precision errors (e.g., 1.1 - 1.0 might result in 0.10000000000000009).

Related Concepts

...

An article to understand the floor division operator in Python

...

An article to understand the division operator in Python

...

An article to understand the multiplication operator in Python

...

An article to understand the subtraction operator in Python

...

An article to understand the addition operator in Python

Search
Download PYTHON
Download Python on your system from python.org downloads section