Python program to create Bankaccount class with deposit, withdraw function

23 Aug, 2021
Prerequisite: Object Oriented Programming in PythonLet’s write a simple Python program using OOP concept to perform some simple bank operations like deposit and withdrawal of money.First of all, define class Bankacccount. This step is followed by defining a function using __init__. It is run as soon as an object of a class is instantiated. This __init__ method is useful to do any initialization you want to do with object, then we have the default argument self. 
Python3
# BankAccount classclass Bankaccount:    def __init__(self):
This step is followed by declaring that balance is 0 using self argument then we simply print a statement welcoming to Machine. In function deposit and withdraw , amount is taken as input(in float) and is then added/subtracted to the balance. Thus resultant balance is printed in next line. 
Python3
# Function to deposite amountdef deposit(self):        amount = float(input("Enter amount to be deposited: "))        self.balance += amount        print("\n Amount Deposited:", amount)
Use an if condition to check whether there is a sufficient amount of money available in the account to process a fund withdrawal. 
Python3
# Function to withdraw the amountdef withdraw(self):        amount = float(input("Enter amount to be withdrawn: "))        if self.balance >= amount:            self.balance -= amount            print("\n You Withdrew:", amount)        else:            print("\n Insufficient balance  ")
Next, we use a display function to display the remaining balance in the account. Then we create a object and call it to get the program executed.  
Python3
# Function to display the amountdef display(self):        print("\n Net Available Balance =", self.balance)
Below is the implementation:  
Python3
# Python program to create Bankaccount class# with both a deposit() and a withdraw() functionclass Bank_Account:    def __init__(self):        self.balance=0        print("Hello!!! Welcome to the Deposit & Withdrawal Machine")     def deposit(self):        amount=float(input("Enter amount to be Deposited: "))        self.balance += amount        print("\n Amount Deposited:",amount)     def withdraw(self):        amount = float(input("Enter amount to be Withdrawn: "))        if self.balance>=amount:            self.balance-=amount            print("\n You Withdrew:", amount)        else:            print("\n Insufficient balance  ")     def display(self):        print("\n Net Available Balance=",self.balance) # Driver code  # creating an object of classs = Bank_Account()  # Calling functions with that class objects.deposit()s.withdraw()s.display()
Output:  
Hello !!! Welcome to Deposit&Withdrawal Machine
Enter amount to be deposited: 
 Amount Deposited: 1000.0
Enter amount to be withdrawn: 
 You Withdrew: 500.0

 Net Available Balance = 500.0
 
lennyatomz
Akanksha_Rai
rajeev0719singh
gulshankumarar231
Python-OOP
python-oop-concepts
Technical Scripter 2018
Python Programs
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Program to check Armstrong Number
Python program to interchange first and last elements in a list
Differences and Applications of List, Tuple, Set and Dictionary in Python
Appending to list in Python dictionary
Python Program for simple interest
Python Program for Merge Sort
Python Program for n-th Fibonacci number
Python | Find most frequent element in a list
Python | Remove spaces from a string
Python Program to find largest element in an array