Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Examples
  3. Bank Account Class

OOP · Python example

Bank Account Class

Model a bank account with a class, methods, and simple validation.

Open the compilerAll examples
intermediateclassesobjectsmethods

Classes combine data and behaviour. This small account object stores an owner and balance, and exposes methods for safe changes.

example.py
class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        if amount > self.balance:
            return False
        self.balance -= amount
        return True

    def __str__(self):
        return f"{self.owner}: ${self.balance:.2f}"


account = BankAccount("Ada", 120)
print(account)

print("Withdrawal succeeded:", account.withdraw(50))
print(account)

print("Overdraft rejected:", not account.withdraw(1000))
print(account)

How the code works

  • The constructor creates an account for Ada with a balance of 120.
  • A withdrawal of 50 succeeds and lowers the balance to 70.
  • A withdrawal of 1000 is rejected, so the balance stays 70.

Key ideas

  • __init__ initializes each new object.
  • self refers to the current instance.
  • withdraw() returns a boolean so callers can react to a rejected transaction.
  • __str__ controls what print() shows for the object.

Try changing it

  • Raise a ValueError instead of returning False for invalid withdrawals.
  • Add a transaction history list.
  • Create a SavingsAccount subclass with interest.

Related examples

beginner

A Function with a Default Argument

Define a reusable greeting function with a default parameter value.

intermediate

Write and Read a Text File

Create a file in Python's virtual filesystem, then read it back.

Previous exampleWrite and Read a Text File

Expected output

Ada: $120.00
Withdrawal succeeded: True
Ada: $70.00
Overdraft rejected: True
Ada: $70.00

Output is compared after trimming trailing whitespace on each line. Run the code to see it produced by CPython in your browser.

Practice next

Apply the same idea with automatic tests or browse a guided project.

ExercisesProjects