0
0
Pythonprogramming~20 mins

Modifying object state in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Modifying Object State in Python
📖 Scenario: You are creating a simple program to manage a bank account. The account has a balance that can change when you deposit or withdraw money.
🎯 Goal: Build a Python class called BankAccount that can store a balance and update it when deposits or withdrawals happen.
📋 What You'll Learn
Create a class named BankAccount with an attribute balance.
Add a method deposit that increases the balance by a given amount.
Add a method withdraw that decreases the balance by a given amount.
Print the final balance after some deposits and withdrawals.
💡 Why This Matters
🌍 Real World
Managing bank accounts or any system where you track and update values over time.
💼 Career
Understanding how to modify object state is key for software development, especially in building interactive applications and managing data.
Progress0 / 4 steps
1
Create the BankAccount class with initial balance
Create a class called BankAccount with an __init__ method that sets the balance attribute to 0.
Python
Need a hint?

Use self.balance = 0 inside the __init__ method to set the starting balance.

2
Add a deposit method to increase balance
Add a method called deposit to the BankAccount class that takes a parameter amount and adds it to self.balance.
Python
Need a hint?

Use self.balance += amount inside the deposit method to add money.

3
Add a withdraw method to decrease balance
Add a method called withdraw to the BankAccount class that takes a parameter amount and subtracts it from self.balance.
Python
Need a hint?

Use self.balance -= amount inside the withdraw method to remove money.

4
Create an account, deposit, withdraw, and print balance
Create an instance of BankAccount called account. Use the deposit method to add 100, then use the withdraw method to remove 30. Finally, print account.balance.
Python
Need a hint?

Remember to create the object, call deposit(100), then withdraw(30), and print the balance.