October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Learn Python Classes and Objects by Building a Bank Account System

Build a beginner-friendly Python Account class and learn how instances, self, methods, and per-account balances work.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Build a small Account class to see how Python classes, objects, instance data, and methods fit together. Each account will keep its own balance, while all accounts use the same deposit and withdrawal operations. The example uses Decimal for money-like values, but it is a teaching model—not a production banking system.

What are classes and objects in Python?

A class bundles data and functionality into a new type. An object created from that class is called an instance. Think of Account as the shared plan for an account, and each account you create as a separate object with its own balance.

As an Amazon Associate I earn from qualifying purchases.

Python’s official classes tutorial explains that calling a class creates an instance. When the class defines __init__(), Python invokes it to initialize that new instance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do I create a class in Python?

Start with an initializer that stores a starting balance on each new account:

from decimal import Decimal

class Account:
    def __init__(self, owner, opening_balance):
        self.owner = owner
        self.balance = Decimal(opening_balance)

account_a = Account("Ava", "100.00")
account_b = Account("Noah", "25.00")

print(account_a.balance)  # 100.00
print(account_b.balance)  # 25.00

The class definition describes what an account can contain. Each call to Account(...) creates a distinct instance, and __init__ assigns that instance an owner and balance.

Why balance is instance data

self.balance is an instance variable: its value belongs to one particular account. Changing account_a.balance does not change account_b.balance.

A class variable, by contrast, is shared through the class by its instances. It can suit a shared class-wide setting or label, but it is the wrong place for individual account balances. If a balance were a class variable, accounts could unexpectedly share that state.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What does self mean in a Python class?

When Python calls an instance method through an object, it passes that object as the method’s first argument. The parameter is conventionally named self, but that name is not a Python keyword. The tutorial expresses the relationship as x.f() being equivalent to MyClass.f(x).

So, when you call account_a.deposit(...), self inside deposit refers to account_a. The method can read or update that account’s state.

How do I add deposits and withdrawals?

Add methods to the class so each operation changes the receiving account’s balance. This example rejects zero or negative transactions and withdrawals larger than the available balance:

from decimal import Decimal

class Account:
    def __init__(self, owner, opening_balance):
        self.owner = owner
        self.balance = Decimal(opening_balance)

    def deposit(self, amount):
        amount = Decimal(amount)
        if amount <= 0:
            raise ValueError("Deposit must be greater than zero")
        self.balance += amount

    def withdraw(self, amount):
        amount = Decimal(amount)
        if amount <= 0:
            raise ValueError("Withdrawal must be greater than zero")
        if amount > self.balance:
            raise ValueError("Insufficient funds")
        self.balance -= amount

account_a = Account("Ava", "100.00")
account_b = Account("Noah", "25.00")

account_a.deposit("12.50")
account_b.withdraw("5.00")

print(account_a.balance)  # 112.50
print(account_b.balance)  # 20.00

The checks are deliberate choices for this lesson, not rules imposed by Python or a complete set of banking policies. The arithmetic and validation operate on the instance that receives each method call.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why use Decimal for money-like values?

Python’s built-in float uses binary floating-point representation, so many decimal fractions cannot be represented exactly. For example, a float may not hold the decimal value you intend. The official decimal documentation shows that constructing a Decimal from a string, such as Decimal("3.14"), represents that decimal input exactly.

Prefer Decimal("10.00") to Decimal(10.00). The latter first creates a binary float; converting it to Decimal preserves that float’s exact value, which can include digits beyond the decimal amount you expected.

Decimal does not decide an application’s currency rules. Real systems must specify such things as accepted precision and how to round. The module provides quantize() and rounding modes for setting a decimal exponent and applying a chosen rounding policy. This example does not add a rounding policy or represent a bank ledger.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What does an underscore mean for account data?

Python’s ordinary class model does not enforce private instance variables. A leading underscore—such as self._balance—signals that an attribute is intended for internal use, but it does not prevent other code from accessing it. The Python tutorial describes data hiding in this model as convention-based.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a beginner exercise, self.balance keeps the example direct. Renaming it to self._balance communicates intent, but does not add access control or financial safeguards.

What this account example leaves out

This small class demonstrates the relationship between a class and its instances; it is not suitable for handling real deposits or customer funds. It has no persistent storage, authentication, transaction history, concurrency handling, auditing, or jurisdiction-specific rules. Its validation choices are illustrative only. Real financial software needs substantially more design and review than a class with a balance attribute.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.