In this tutorial, you will learn about the python program for bank account using class.
We will explore how to create a Python program for a bank account using the concept of classes.
We will walk through the steps of designing a class to represent a bank account, defining methods to perform common banking operations such as deposit, withdrawal, and checking the account balance.
By the end of this tutorial, you will have a solid understanding of how to implement a bank account system using classes in Python.
Section 1
What is a Class in Python?
A class is a blueprint for creating objects that have similar attributes and behaviors.
It defines a structure for the objects and the methods they can perform.
In our case, we will create a class called BankAccount that will serve as a template for creating bank account objects.
Creating the BankAccount Class
To start, let’s define the BankAccount class in Python.
Open your preferred code editor and create a new Python file.
We recommend using our online Python compiler.
Python Program For Bank Account Using Class
class BankAccount:
pass
In the above code, we have defined an empty class named BankAccount.
We will add the necessary methods and attributes to this class to create a functional bank account.
Section 2
Initializing the Bank Account
Every bank account needs some initial details, such as the account holder’s name and an account number.
Let’s modify the BankAccount class to include an initialization method that accepts the account holder’s name and sets it as an attribute of the class:
Python Program For Bank Account Using Class
class BankAccount:
def __init__(self, account_holder_name):
self.account_holder_name = account_holder_name
In the above code, we have defined an __init__ method that takes account_holder_name as a parameter and assigns it to the self.account_holder_name attribute.
The self parameter refers to the instance of the class and allows us to access the attributes and methods within the class.
Section 3
Depositing Funds
To deposit funds into a bank account, we need to implement a method that increases the account balance by a certain amount.
Let’s add a deposit method to the BankAccount class.
Python Program For Bank Account Using Class
class BankAccount:
def __init__(self, account_holder_name):
self.account_holder_name = account_holder_name
self.balance = 0
def deposit(self, amount):
self.balance += amount
In the code above, we have added a deposit method that takes amount as a parameter and increments the self.balance attribute by that amount.
Section 4
Withdrawing Funds
Similarly, we need to implement a method to withdraw funds from the account.
Let’s add a withdraw method to the BankAccount class.
Python Program For Bank Account Using Class
class BankAccount:
def __init__(self, account_holder_name):
self.account_holder_name = account_holder_name
self.balance = 0
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
print("Insufficient balance.")
In the code above, we have added a withdraw method that checks if the amount is less than or equal to the current balance.
If so, it deducts the amount from the balance.
Otherwise, it displays an “Insufficient balance” message.
Section 5
Checking Account Balance
To check the account balance, we can simply return the value of the self.balance attribute.
Let’s add a get_balance method to the BankAccount class.
Python Program For Bank Account Using Class
class BankAccount:
def __init__(self, account_holder_name):
self.account_holder_name = account_holder_name
self.balance = 0
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
print("Insufficient balance.")
def get_balance(self):
return self.balance
The get_balance method returns the current balance of the account.
Section 6
Handling Insufficient Balance
When a withdrawal is attempted with an amount greater than the available balance, we display an “Insufficient balance” message.
However, it would be more convenient if this information is raised as an exception instead.
Let’s modify the withdraw method to raise a ValueError when there is insufficient balance.
Python Program For Bank Account Using Class
class BankAccount:
def __init__(self, account_holder_name):
self.account_holder_name = account_holder_name
self.balance = 0
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
raise ValueError("Insufficient balance.")
def get_balance(self):
return self.balance
By raising a ValueError exception, we can handle the insufficient balance situation more effectively.
Section 7
Modifying Account Holder Name
In some cases, the account holder may need to modify their name on the bank account.
Let’s add a method to the BankAccount class that allows changing the account holder’s name.
Python Program For Bank Account Using Class
class BankAccount:
def __init__(self, account_holder_name):
self.account_holder_name = account_holder_name
self.balance = 0
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
raise ValueError("Insufficient balance.")
def get_balance(self):
return self.balance
def change_account_holder_name(self, new_name):
self.account_holder_name = new_name
The change_account_holder_name method accepts new_name as a parameter and updates the self.account_holder_name attribute with the new name.
Section 8
Viewing Account Details
Lastly, let’s add a method to the BankAccount class that displays the account holder’s name and current balance.
Python Program For Bank Account Using Class
class BankAccount:
def __init__(self, account_holder_name):
self.account_holder_name = account_holder_name
self.balance = 0
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
raise ValueError("Insufficient balance.")
def get_balance(self):
return self.balance
def change_account_holder_name(self, new_name):
self.account_holder_name = new_name
def display_account_details(self):
print("Account Holder Name:", self.account_holder_name)
print("Account Balance:", self.balance)
The display_account_details method simply prints the account holder’s name and current balance.
Testing
Testing Python Program For Bank Account Using Class
Let’s see our program in action.
Python Program For Bank Account Using Class
# Define the BankAccount class
class BankAccount:
def __init__(self, account_holder_name):
self.account_holder_name = account_holder_name
self.balance = 0
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
raise ValueError("Insufficient balance.")
def get_balance(self):
return self.balance
def change_account_holder_name(self, new_name):
self.account_holder_name = new_name
def display_account_details(self):
print("Account Holder Name:", self.account_holder_name)
print("Account Balance:", self.balance)
# Create a new bank account
account = BankAccount("John Doe")
# Display the initial account details
account.display_account_details()
# Deposit funds into the account
print("Depositing funds...")
account.deposit(1000)
# Display the updated account details
account.display_account_details()
# Withdraw funds from the account
print("Withdrawing Funds...")
account.withdraw(500)
# Display the updated account details
account.display_account_details()
Output
Account Holder Name: John Doe
Account Balance: 0
Depositing funds…
Account Holder Name: John Doe
Account Balance: 1000
Withdrawing Funds…
Account Holder Name: John Doe
Account Balance: 500
Section 9
What are the Benefits of Using a Class?
Using a class to represent a bank account offers several advantages:
- Code Organization: Classes provide a structured way to organize related attributes and methods, making the code more readable and maintainable.
- Code Reusability: Once a class is defined, it can be used to create multiple instances of bank accounts, each with its own set of attributes and behavior.
- Encapsulation: By encapsulating the account details and operations within a class, we can control access to the data and ensure the integrity of the account operations.
- Modularity: Classes promote modular design, allowing developers to separate concerns and focus on individual components of the bank account system.
FAQs
FAQs About Python Program For Bank Account Using Class
How can I create multiple bank accounts using the same class?
To create multiple bank accounts using the same class, you can simply instantiate the BankAccount class multiple times with different account holder names.
Here’s an example.
account1 = BankAccount("John Doe")
account2 = BankAccount("Jane Smith")
In the above code, we have created two bank accounts named “John Doe” and “Jane Smith” using the BankAccount class.
Can I modify the account number for a bank account?
In the current implementation, we have not included an account number attribute in the BankAccount class. However, you can easily add an account number attribute and modify it as needed.
Is it possible to add interest to the account balance?
Yes, it is possible to add interest to the account balance.
You can add a method to the BankAccount class that calculates and adds interest based on a specific interest rate.
Can I have a negative balance in a bank account?
By default, the BankAccount class allows withdrawals even if the account balance goes below zero.
However, you can modify the withdraw method to check for a minimum balance and prevent a negative balance if desired.
What happens if I try to withdraw more than the available balance?
If you try to withdraw an amount greater than the available balance, the current implementation raises a ValueError with the message “Insufficient balance.”
You can catch this exception and handle it accordingly in your code.
How can I delete a bank account created using the class?
In Python, objects are automatically garbage collected when they are no longer referenced.
Once you no longer have any references to a bank account object, it will be eligible for garbage collection.
Wrapping Up
Conclusions: Python Program For Bank Account Using Class
In this tutorial, we have learned how to create a Python program for a bank account using the concept of classes.
We designed a BankAccount class that allows performing common banking operations such as depositing funds, withdrawing funds, checking the account balance, modifying account details, and viewing account details.
By utilizing classes, we achieved code organization, reusability, encapsulation, and modularity.
Feel free to expand upon this program and add additional features to enhance its functionality.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.