Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to subtract in Python

Getting Started with Subtraction in Python

Python is a versatile and widely-used programming language, making it a great choice for beginners. Today, we're going to delve deep into one of the fundamental operations in Python: subtraction. But fear not, with a few examples and some fun analogies, we'll make this journey together.

The Basic Syntax

Subtraction in Python is as simple as it is in basic arithmetic. To subtract two numbers, we use the '-' operator.

# Subtracting two numbers
result = 5 - 2
print(result)

When you run this code, Python will output 3. This is because we've instructed Python to subtract 2 from 5, and store the result in a variable we've named result.

Subtracting Variables

Subtraction isn't limited to directly stated numbers. We can also subtract variables. Let's imagine we're pirates, and we've got two chests of gold. The first chest, chest_A, has 50 gold coins. The second chest, chest_B, has 30 gold coins.

# Variables and subtraction
chest_A = 50
chest_B = 30
difference = chest_A - chest_B
print(difference)

Running the above code will give us 20. This is because we've instructed Python to calculate the difference between the gold coins in the two chests.

Subtraction with More Than Two Numbers

Like a master chef mixing various ingredients, Python allows us to subtract more than two numbers at the same time. Let's say you started the day with $100. You then spent $30 on groceries, $20 on lunch, and $15 on a book. How much money do you have left?

# Subtracting multiple numbers
total_money = 100
groceries = 30
lunch = 20
book = 15
remaining_money = total_money - groceries - lunch - book
print(remaining_money)

Running this code will output 35. Python subtracts each of the amounts spent from the total money to calculate the remaining amount.

Subtraction with Negative Numbers

Remember that subtraction can also lead to negative numbers. Dealing with negative numbers is just like having a debt. Let's say you owe a friend $10 and you borrow another $15, then you owe him a total of $25. In Python, we would represent this as:

# Subtracting to get negative numbers
debt = 0
borrowed_money1 = 10
borrowed_money2 = 15
total_debt = debt - borrowed_money1 - borrowed_money2
print(total_debt)

Running the code will output -25. This represents that you are $25 in debt.

Subtraction with Floating-Point Numbers

Python can also subtract floating-point numbers, which are numbers that have decimal points. Imagine you're a baker and you've baked 8.5 cakes (maybe half got eaten during taste testing!). After a busy day at your bakery, you've sold 4.2 cakes. How many cakes do you have left?

# Subtracting floating-point numbers
total_cakes = 8.5
cakes_sold = 4.2
cakes_left = total_cakes - cakes_sold
print(cakes_left)

Executing this code will give you 4.3. So, you have 4.3 cakes left in your bakery.

Conclusion

And there you have it! We've embarked on a grand adventure through the world of subtraction in Python. From the basics to the complexities of negative numbers and floating-point numbers, we've seen it all. We've navigated the treacherous seas of debt, counted our remaining pirate's treasure, and even baked (and half-eaten) a few cakes along the way.

Python's flexibility and simplicity make it a powerful tool in any programmer's arsenal. With a solid understanding of subtraction in Python, you're well on your way to becoming a Python whiz. So, keep exploring, keep questioning, and most importantly, keep subtracting!