Subtraction

 

What Is Subtraction in Python?

  • In Python, the subtraction operator - is used to find the difference between two numbers.

How to Use It:

  • The basic way to subtract two numbers is:

    python
    result = number1 - number2
    
    • number1 and number2 are the numbers you want to subtract.
    • result will store the difference.

Examples and Exercises:

  1. Subtracting Two Numbers:

    python
    a = 20
    b = 12
    result = a - b
    print(result) 
    • Output: 8
  2. Chaining Subtractions:

    • You can subtract multiple numbers in one line.
    python
    a = 63
    b = 12
    c = 5
    d = 10
    result = a - b - c - d
    print(result)  
    • Output: 36
  3. Subtracting Floating Point Numbers:

    • You can also subtract numbers with decimals.
    python
    a = 12.5
    b = 10.2
    result = a - b
    print(result)  
    • Output: 2.3

Summary:

  • The subtraction operator - in Python is used to find the difference between numbers. It can be used with integers, floating point numbers, and even complex numbers. Try out the examples to get a feel for how it works!