0
0
Pythonprogramming~30 mins

Instance methods in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Instance Methods in a Bank Account
📖 Scenario: You are creating a simple bank account system. Each account can store the owner's name and balance. You will add methods to deposit and withdraw money.
🎯 Goal: Build a BankAccount class with instance methods to deposit and withdraw money, and then display the current balance.
📋 What You'll Learn
Create a class called BankAccount with an __init__ method
Add an instance method called deposit that adds money to the balance
Add an instance method called withdraw that subtracts money from the balance
Create an instance of BankAccount with owner name and initial balance
Use the instance methods to change the balance
Print the final balance
💡 Why This Matters
🌍 Real World
Bank accounts in real life keep track of money for each person. Instance methods let us add or remove money safely.
💼 Career
Understanding instance methods is key for programming with objects, which is common in software development jobs.
Progress0 / 4 steps
1
Create the BankAccount class with initial data
Create a class called BankAccount with an __init__ method that takes self, owner, and balance. Inside __init__, set self.owner to owner and self.balance to balance. Then create an instance called account with owner 'Alice' and balance 1000.
Python
Need a hint?

Remember, __init__ sets up the object with the owner's name and starting balance.

2
Add a deposit method
Add an instance method called deposit to the BankAccount class. It should take self and amount as parameters and add amount to self.balance.
Python
Need a hint?

The deposit method changes the balance by adding the amount.

3
Add a withdraw method
Add an instance method called withdraw to the BankAccount class. It should take self and amount as parameters and subtract amount from self.balance.
Python
Need a hint?

The withdraw method reduces the balance by the amount.

4
Use methods and print the balance
Use the deposit method on account to add 500. Then use the withdraw method on account to subtract 200. Finally, print account.balance to show the current balance.
Python
Need a hint?

Use the methods on the account object and then print the balance.