How to Subtract from a Variable in Python?

Recently, I was doing some Python programming and learned different methods to subtract from a variable in Python. In this tutorial, I will show you how to subtract from a variable in Python with different examples and complete code.

To subtract from a variable in Python, you can use the -= operator, which is a shorthand for subtracting a value directly from the variable and updating it in place. For example, if you have a = 10 and you want to subtract 3, you can simply write a -= 3, which will update a to 7.

Subtract from a Variable in Python

There are various methods to subtract from a variable in Python, let us check one by one with examples.

Basic Subtraction

In Python, you can subtract using the – operator. We use the - operator to subtract one number from another. Let’s start with a simple example:

a = 10
b = 3
result = a - b
print(result)  # Output: 7

In this example, we subtract b from a, resulting in 7. You can see the exact output in the screenshot below:

subtraction in python

Check out How to Multiply a Variable in Python?

Subtracting from a Variable

If you want to subtract a value from an existing variable in Python, you can do so directly. Here is an example.

a = 10
a -= 3
print(a)  # Output: 7

Here, the -= operator is a shorthand for a = a - 3. This is particularly useful for updating the value of a variable in place.

You can see the exact output in the screenshot below:

subtract from a variable in python

Subtract in Loops

Subtraction is often used in loops, especially when you need to decrement a counter. Consider a scenario where you are counting down days until a specific event, such as the Fourth of July:

days_until_july_4th = 10

while days_until_july_4th > 0:
    print(f"{days_until_july_4th} days until the Fourth of July!")
    days_until_july_4th -= 1

print("Happy Fourth of July!")

In this example, we start with 10 days and decrement the counter until it reaches 0, at which point we print a celebratory message.

Read How to Increment a Variable in Python?

Subtract in Lists

Sometimes, you may need to subtract elements of one list from another. Python’s NumPy library makes this easy:

import numpy as np

list1 = np.array([10, 20, 30])
list2 = np.array([1, 2, 3])
result = np.subtract(list1, list2)
print(result)  # Output: [ 9 18 27 ]

Using NumPy, we subtract corresponding elements of list2 from list1, resulting in [9, 18, 27].

Subtract Dates

In the USA, date manipulation is common, whether you are calculating deadlines or planning events. Python’s datetime module allows you to subtract dates easily:

from datetime import datetime, timedelta

today = datetime.now()
event_date = datetime(2024, 11, 28)  # Thanksgiving 2024
days_until_event = (event_date - today).days
print(f"{days_until_event} days until Thanksgiving 2024")

Here, we calculate the number of days until Thanksgiving 2024 by subtracting the current date from the event date.

Read How to Divide a Variable in Python?

Subtract with Functions

You can also create functions to handle subtraction, making your code reusable and organized. Let’s create a function to subtract two numbers in Python:

def subtract_numbers(a, b):
    return a - b

result = subtract_numbers(15, 5)
print(result)  # Output: 10

This function takes two arguments and returns their difference. Here is the output:

how to subtract from a variable in python

How to subtract 1 from a variable in Python

To subtract 1 from a variable in Python, you can use the -= operator, which is a shorthand for subtracting a value and updating the variable in place. Here’s a simple example:

counter = 10
counter -= 1
print(counter)  # Output: 9

In this example, counter starts at 10. The -= operator subtracts 1 from counter, updating its value to 9. You can also see the exact output in the screenshot below:

How to subtract 1 from a variable in Python

Practical Example: Subtract from a Variable in Python

Here, let me show you a practical example of subtracting from a variable in Python.

Let’s say you are tracking your monthly expenses in a budget app. You start with a budget and subtract expenses as they occur:

initial_budget = 2000  # Monthly budget in USD
expenses = [150, 200, 50, 300, 100]  # List of expenses

for expense in expenses:
    initial_budget -= expense

print(f"Remaining budget: ${initial_budget}")  # Output: Remaining budget: $1200

In this example, we subtract each expense from the initial budget, giving us the remaining budget at the end of the month.

Conclusion

In this tutorial, I have explained how to subtract from a variable in Python using different methods with examples. Also, we covered how to subtract 1 from a variable in Python. If you still have questions, feel free to leave a comment below.

You may also like:

Leave a Comment