The Wayback Machine - https://web.archive.org/web/20250824152327/https://www.geeksforgeeks.org/python/python-program-to-create-bankaccount-class-with-deposit-withdraw-function/
Open In App

Create Bankaccount class with deposit and withdraw function in Python

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Let's write a simple Python program using the Object-Oriented Programming (OOP) concept to simulate basic bank operations such as depositing and withdrawing money. The key focus will be on creating a BankAccount class that allows users to:

  • Deposit money into their account.
  • Withdraw money from their account (only if sufficient balance is available).
  • Display the current balance.

We need to follow these steps to create the BankAccount class with deposit and withdraw functions.

Step 1: We start by defining the class BankAccount. Inside it, we will define the __init__ method that initializes an instance of the class. This method runs automatically when we create a new object and initializes the account balance to zero.

Python
class BankAccount:
    def __init__(self):
        self.balance = 0  # Initialize balance to 0
        print("Welcome to the Machine")

Step 2: This method allows users to deposit money. The amount is taken as input, converted to a float, added to the current balance, and a confirmation message is printed.

Python
    def deposit(self):
        amount = float(input("Enter amount to be Deposited: "))
        self.balance += amount
        print("\nAmount Deposited:", amount)

Step 3: The withdraw method lets users withdraw money, but only if the account has sufficient funds. If the balance is enough, the amount is deducted, and a success message is shown; otherwise, an "Insufficient balance" message is displayed.

Python
    def withdraw(self):
        amount = float(input("Enter amount to be Withdrawn: "))
        if self.balance >= amount:
            self.balance -= amount
            print("\nYou Withdrew:", amount)
        else:
            print("\nInsufficient balance")

Step 4: This method simply displays the current balance available in the account.

Python
    def display(self):
        print("\nNet Available Balance =", self.balance)

Completed code

Python
class BankAccount:
    def __init__(self):
        self.balance = 0
        print("Welcome to the Machine")

    def deposit(self):
        amount = float(input("Enter amount to be Deposited: "))
        self.balance += amount
        print("\nAmount Deposited:", amount)

    def withdraw(self):
        amount = float(input("Enter amount to be Withdrawn: "))
        if self.balance >= amount:
            self.balance -= amount
            print("\nYou Withdrew:", amount)
        else:
            print("\nInsufficient balance")

    def display(self):
        print("\nNet Available Balance =", self.balance)


# Driver code
if __name__ == "__main__":
    s = BankAccount()  # Create an object of BankAccount

    s.deposit()        # Deposit money
    s.withdraw()       # Withdraw money
    s.display()        # Display balance

Output

Welcome to the Machine
Enter amount to be Deposited: 1000

Amount Deposited: 1000.0
Enter amount to be Withdrawn: 500

You Withdrew: 500.0

Net Available Balance = 500.0

Python program to create Bankaccount class with deposit, withdraw function

Similar Reads