Editor

Python Program for a Bank Account Using a Class

Intermediate · Classes and objects

This program models a bank account as a class with methods to deposit money, withdraw it and print a statement. It refuses to withdraw more than the account holds, and keeps a list of every transaction for the mini statement.

Program

bank_account_class.py
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
self.history = []
def deposit(self, amount):
if amount <= 0:
print("Deposit amount must be positive")
return
self.balance += amount
self.history.append(f"Deposited Rs. {amount}")
def withdraw(self, amount):
if amount > self.balance:
print(f"Cannot withdraw Rs. {amount}: the balance is only Rs. {self.balance}")
return
self.balance -= amount
self.history.append(f"Withdrew Rs. {amount}")
def statement(self):
print(f"Statement for {self.owner}:")
for entry in self.history:
print(" -", entry)
print(f"Balance: Rs. {self.balance}")
account = BankAccount("Riya", 1000)
account.deposit(500) # or: account.deposit(int(input("Amount to deposit: ")))
account.withdraw(300)
account.withdraw(5000) # more than the balance, so it is refused
account.statement()
Output
Cannot withdraw Rs. 5000: the balance is only Rs. 1200
Statement for Riya:
 - Deposited Rs. 500
 - Withdrew Rs. 300
Balance: Rs. 1200

How it works

  1. Each account object keeps its own owner, balance and history, set up in __init__; balance=0 lets you open an account with no starting amount.
  2. deposit() adds to the balance and withdraw() takes money out, and both record what happened in history.
  3. withdraw() checks the balance first and refuses any amount larger than it, so the balance can never go below zero.
  4. statement() prints every transaction followed by the current balance, like a mini statement from an ATM.
  5. Changing the balance only through methods, instead of writing account.balance = ... directly, is called encapsulation: the class makes sure its rules are always followed.

Other ways to write it

Transferring money between two accounts

Transferring money between two accounts
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def transfer(self, other, amount):
if amount > self.balance:
print(f"{self.owner} cannot send Rs. {amount}")
return
self.balance -= amount
other.balance += amount
print(f"{self.owner} sent Rs. {amount} to {other.owner}")
riya = BankAccount("Riya", 2000)
aman = BankAccount("Aman", 500)
riya.transfer(aman, 750)
aman.transfer(riya, 5000)
print("Riya has Rs.", riya.balance, "and Aman has Rs.", aman.balance)
Output
Riya sent Rs. 750 to Aman
Aman cannot send Rs. 5000
Riya has Rs. 1250 and Aman has Rs. 1250

A method can take another object as an argument, so transfer() changes two accounts in one call.

Raising an error instead of printing

Raising an error instead of printing
class BankAccount:
def __init__(self, balance):
self.balance = balance
def withdraw(self, amount):
if amount > self.balance:
raise ValueError(f"the balance is only Rs. {self.balance}")
self.balance -= amount
account = BankAccount(1000)
for amount in [400, 900]:
try:
account.withdraw(amount)
print("Withdrew Rs.", amount, "- balance now Rs.", account.balance)
except ValueError as error:
print("Could not withdraw Rs.", amount, "-", error)
Output
Withdrew Rs. 400 - balance now Rs. 600
Could not withdraw Rs. 900 - the balance is only Rs. 600

Raising an error lets the code that called withdraw() decide what to do about it, which is how real programs report problems, instead of the method printing a message itself.

Try it yourself

Add a minimum balance rule: refuse any withdrawal that would leave less than Rs. 500 in the account.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises