Last modified: Dec 01, 2026
Python Class Basics for Beginners
Classes are the foundation of object-oriented programming in Python. They let you group data and behavior together. This guide covers the core ideas in a simple way.
You will see small code examples with output. Each example builds on the last one. By the end, you will know how classes really work.
What Is a Class?
A class is a blueprint. It describes what data an object holds and what it can do. An object is a real instance built from that blueprint.
Think of a class as a cookie cutter. The cookies are the objects. Each cookie has the same shape but can have its own toppings.
class Dog:
pass # an empty class for now
my_dog = Dog() # create an object
print(type(my_dog))
The class Dog is empty, but Python still lets you create an object from it. That object is a real thing in memory.
Attributes: Storing Data
Attributes are variables that belong to an object or a class. They store the data your object needs.
You can set attributes directly on an object. This is quick but not always clean.
class Dog:
pass
my_dog = Dog()
my_dog.name = "Rex" # instance attribute
my_dog.age = 3
print(my_dog.name, my_dog.age)
Rex 3
Setting attributes after creation works, but it is easy to forget one. A better way is to use a constructor. To see how that works, read about a Python class with arguments.
The Constructor: __init__
The method __init__ runs when you create an object. It sets up the initial state. This is where you assign attributes safely.
class Dog:
def __init__(self, name, age):
self.name = name # instance attribute
self.age = age
my_dog = Dog("Rex", 3)
print(my_dog.name, my_dog.age)
Rex 3
Now every Dog object must have a name and an age. That makes your code more predictable.
What Is self?
self refers to the current object. It is the first parameter of almost every method. Python passes it for you.
You do not type self when you call a method. Python fills it in automatically. It is how a method knows which object it belongs to.
class Dog:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} says woof!"
my_dog = Dog("Rex")
print(my_dog.speak())
Rex says woof!
Notice that speak takes only self. The object already knows its own name.
Methods: Adding Behavior
Methods are functions defined inside a class. They describe what an object can do. They can read and change attributes.
Good classes keep related data and behavior together. That is the main benefit of classes. For a deeper look, see these Python class methods and attributes explained step by step.
class Counter:
def __init__(self):
self.count = 0
def add(self, amount):
self.count += amount # update attribute
counter = Counter()
counter.add(5)
counter.add(3)
print(counter.count)
8
This class holds a number and knows how to grow it. The logic lives in one place. That makes it easy to reuse.
Class Attributes vs Instance Attributes
An instance attribute belongs to one object. A class attribute is shared by all objects of that class.
Use class attributes for values that are the same for every object. Use instance attributes for values that differ.
class Dog:
species = "Canis familiaris" # class attribute
def __init__(self, name):
self.name = name # instance attribute
a = Dog("Rex")
b = Dog("Max")
print(a.species, b.species)
print(a.name, b.name)
Canis familiaris Canis familiaris
Rex Max
Both dogs share the species. Each has its own name. Mixing these up is a common beginner mistake.
Real Method Examples
Methods can return values, modify state, or both. Small, focused methods are easier to test and read.
Here is a class with a few practical methods:
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount > self.balance:
return "Insufficient funds"
self.balance -= amount
return self.balance
account = BankAccount("Ana", 100)
print(account.deposit(50))
print(account.withdraw(30))
print(account.withdraw(500))
150
120
Insufficient funds
Each method does one job. The object keeps its own state. This pattern scales well. You can find more patterns in these Python class methods examples.
Properties: Controlled Access
Sometimes you want to control how an attribute is read or set. A property lets you run code when an attribute is accessed.
Properties look like attributes but act like methods. They are great for validation and computed values.
class Temperature:
def __init__(self, celsius):
self._celsius = celsius
@property
def fahrenheit(self):
return self._celsius * 9 / 5 + 32
temp = Temperature(25)
print(temp.fahrenheit)
77.0
You read fahrenheit like a normal attribute. No parentheses needed. That keeps the interface clean.
Properties and plain attributes are not the same thing. If you are unsure when to use each, read this Python property vs attribute clear guide.
Common Beginner Mistakes
Most early errors come from a few small habits. Fixing them early saves a lot of time.
Forgetting self. Every instance method needs self as the first parameter. Without it, Python raises a TypeError.
Using a class attribute for changing data. If you modify a mutable class attribute, every object sees the change. Use instance attributes instead.
Mixing up __init__ and __new__. Beginners only need __init__. It sets up the object after it is created.
class BadDog:
def speak(): # missing self
return "woof"
try:
BadDog().speak()
except TypeError as e:
print("Error:", e)
Error: speak() takes 0 positional arguments but 1 was given
The fix is simple. Add self to the method signature. Then the call works.
When to Use Classes
Classes are not always needed. Simple scripts can use plain functions and dictionaries.
Reach for a class when you have data and behavior that belong together. Reach for a class when you need many similar objects. Reach for a class when you want clear, reusable code.
Do not overuse classes. A class with one method and no state is often just a function. Keep your design simple.
Conclusion
Python classes are easier than they look. A class is a blueprint. An object is an instance. Attributes store data. Methods add behavior. self connects them.
Start small. Write a class with __init__ and one method. Add more as you go. Practice is the fastest way to learn.
With these basics, you can read most Python code. You can also start writing cleaner, more organized programs today.