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:
pythonresult = number1 - number2number1andnumber2are the numbers you want to subtract.resultwill store the difference.
Examples and Exercises:
Subtracting Two Numbers:
pythona = 20 b = 12 result = a - b print(result)- Output:
8
- Output:
Chaining Subtractions:
- You can subtract multiple numbers in one line.
pythona = 63 b = 12 c = 5 d = 10 result = a - b - c - d print(result)- Output:
36
Subtracting Floating Point Numbers:
- You can also subtract numbers with decimals.
pythona = 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!