Adding Methods

Python

Instance Methods

Instance Methods

Methods are functions defined inside a class. They always take self as the first parameter.

class BankAccount:
    def __init__(self, balance):
        self.balance = balance
    
    def deposit(self, amount):
        self.balance += amount
    
    def get_balance(self):
        return self.balance

account = BankAccount(100) account.deposit(50) print(account.get_balance()) # 150

Your Task

Add a method apply_tax to the Expense class that:

  • Takes a tax_rate parameter (decimal, e.g., 0.08)
  • Returns the amount with tax applied
  • Code Editor
    Loading PYTHON engine...
    Loading...
    Loading Python engine...

    Run your code to see output