Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →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.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →How do I create a class in Python?
Start with an initializer that stores a starting balance on each new account:
#1 Best Overall
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.
Rank #2
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.
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.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesWhy 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.
Best Value
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.
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.
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.
Quick Recap
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.




