Python
Python

 

Introduction

Welcome to this tutorial where we will create a basic Bank Account Simulator using Python programming. The objective of this program is to simulate the functionality of a simple bank account that allows a user to perform basic banking operations such as depositing money, withdrawing money, and checking the current account balance. This tutorial is ideal for beginners looking to practice basic Python concepts like classes, functions, and user input.

Objective

The goal of this project is to develop a Python program that:

  • Allows the user to deposit money into their bank account.
  • Lets the user withdraw money from their account, if sufficient funds are available.
  • Enables the user to check their current account balance.

Python Code

class BankAccount:
    def __init__(self, account_holder, balance=0):
        """Initialize the bank account with an account holder's name and an optional initial balance."""
        self.account_holder = account_holder
        self.balance = balance

    def deposit(self, amount):
        """Deposit a certain amount into the account."""
        if amount > 0:
            self.balance += amount
            print(f"Deposited ${amount}. Current balance: ${self.balance}")
        else:
            print("Deposit amount must be positive.")

    def withdraw(self, amount):
        """Withdraw a certain amount from the account if sufficient funds exist."""
        if amount > 0:
            if self.balance >= amount:
                self.balance -= amount
                print(f"Withdrew ${amount}. Current balance: ${self.balance}")
            else:
                print("Insufficient balance for the withdrawal.")
        else:
            print("Withdrawal amount must be positive.")

    def check_balance(self):
        """Check and display the current balance of the account."""
        print(f"Current balance: ${self.balance}")

# Example usage
def main():
    # Create an instance of BankAccount for a user
    account = BankAccount(account_holder="John Doe")
    
    # Perform some operations
    account.deposit(500)      # Deposit money
    account.withdraw(200)     # Withdraw money
    account.check_balance()   # Check balance

if __name__ == "__main__":
    main()

Program Structure and Explanation

The program consists of a class named BankAccount, which encapsulates the basic functionality of a bank account:

  • __init__: Initializes the account with the account holder’s name and an optional balance (default is 0).
  • deposit(): Allows the user to deposit money into their account. It ensures that the deposit amount is positive.
  • withdraw(): Allows the user to withdraw money from the account, but checks whether the account has sufficient funds.
  • check_balance(): Displays the current balance of the account.

The main() function demonstrates the use of the BankAccount class. It creates an account, performs a deposit, a withdrawal, and checks the balance at each step.

How to Run the Program

To run the program:

  1. Copy the provided code into a Python file, for example bank_account_simulator.py.
  2. Open a terminal or command prompt, and navigate to the directory where the file is saved.
  3. Run the Python file by typing python bank_account_simulator.py (or python3 bank_account_simulator.py on some systems).
  4. The program will execute and show you the results of the banking operations on the console.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)