Python

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

script.py
Loading PYTHON engine...
Downloading Python… (one-time, ~10 MB)

Downloading Python… (one-time, ~10 MB)

This runs entirely in your browser and is cached for next time.