Python program to create bank account class
Hello friends! In this tutorial, we will learn how to write a Python program using OOP concept to create a bank account class using the deposit, withdraw and display function. It can perform some simple bank operations such as deposit and withdrawal and display of current balance.
Creating bank account class with deposit and withdraw function.
Step 1: Create a class Bank_Acccount
. Then define a function using __init__
with default argument self
. This keyword is used in Python to initialize attributes of the class when an object of that class is created. This step is followed by initializing the balance as 0.
class Bank_Account: def __init__(self): self.balance=0 print("Welcome to Deposit & Withdrawal Machine!")
Step 2: Now we have to create a function deposit
such that the amount of money is taken by input using float and is then added to the balance. Then the amount deposited will be displayed using the print statement in the next line as shown in the code below:
def deposit(self): amount=float(input("Enter amount to be deposited: ")) self.balance += amount print("Amount Deposited: ",amount)
Step 3: Just like step 2, we are going to create another function withdraw
in which we are going to take float input for the amount to get withdraw. We are using an if condition here just to check if we have sufficient
balance available to perform a withdrawal of any amount from the account. If the balance is not sufficient then our class will show “Insufficient balance”.
def withdraw(self): amount = float(input("Enter amount to withdraw: ")) if self.balance>=amount: self.balance-=amount print("You withdraw: ",amount) else: print("Insufficient balance ")
Step 4: Now we are going to create our final function which is display
function. It will display the final balance of the account after withdrawal and deposit.
def display(self): print("Net Available Balance=",self.balance)
Step 5: As a final step we are going to create an object of our class so that we can call all the functions with that class to execute our code.
#creating an object of class s = Bank_Account() #calling functions with that class s.deposit() s.withdraw() s.display()
Here is the complete code:
class Bank_Account: def __init__(self): self.balance=0 print("Welcome to Deposit & Withdrawal Machine!") def deposit(self): amount=float(input("Enter amount to be deposited: ")) self.balance += amount print("Amount Deposited: ",amount) def withdraw(self): amount = float(input("Enter amount to withdraw: ")) if self.balance>=amount: self.balance-=amount print("You withdraw: ",amount) else: print("Insufficient balance ") def display(self): print("Net Available Balance=",self.balance) #creating an object of class s = Bank_Account() #calling functions with that class s.deposit() s.withdraw() s.display()
Here is the output:
Welcome to Deposit & Withdrawal Machine! Enter amount to be deposited: 100.0 Amount Deposited: 100.0 Enter amount to withdraw: 55.0 You withdraw: 55.0 Net Available Balance= 45.0
I hope you like it. If you have any doubts then please comment below.
How to check minimum balance I need that program also
I was hoping to see a function to handle all the transaction history. This can be used to keep track of account activities. I have tried to do that I couldn’t think of a way