Python Classes for Beginners: Learn Objects, Attributes & Methods Step by Step
Feb 27, 2026
Want To Stay Updated?
Subscribe to get the latest articles delivered directly to your inbox.
Feb 27, 2026
Subscribe to get the latest articles delivered directly to your inbox.
Classes are the blue prints for making objects just like cookie cutter is the blueprint for making cookies. They are helpful in organizing code and reusability. But most people often get confused and overwhelmed when learning classes. In this practical and beginner-friendly guide you will learn what a class is in Python, how it works and how to use it step by step.
Classes are the blueprints for creating objects. They define what properties and behaviours an object will have.
You probably have seen cookie cutters. Think of a cookie cutter as a class and cookie as an object. The cookie cutter has a structure and shape that defines how the cookie would look like. You can also make as many cookies as you want from a single cookie cutter.
The same analogy works with the classes. They also have structure, property and behaviour that decide how the object would look like and behave. You can create as many objects as you want from a single class.
It is difficult to understand classes without working with them practically. In this practical workflow you will learn how to create classes in Python and how to create objects from them.
In python you can define a class with a class keyword followed by the name of the class.
class Calculator:
passWe have defined a class above with the name Calculator. This class will be used to make objects, later in this tutorial.
Note: The pass keyword is a placeholder that tells Python the block is intentionally left empty, so it does not raise a syntax error. This is not only specific to classes, it can be used anywhere in Python where a block is required.
In Python, we define variables to store any data. However, when a variable is defined in a Python class, it is called an attribute of an object of that class.
For example, our Calculator class can also have attributes like name, type and model. These attributes can either have a default value, or set by the user. Let's define these attributes.
class Calculator:
def __init__(self, name, type, model):
self.name=name
self.type=type
self.model=modelLet's understand what just happend. In Python class, the attributes are usually defined inside the __init__() method. This method is automatically called when a new object of the class is created.
The self keyword refers to the current object, allowing us to access its attributes and other methods.
Let's create the object and provide the attributes values.
calculator_1=Calculator("Casio", "Scientific Calculator", "C3832")To access the attributes of an object call the specific attribute by using dot (.) followed by the attribute name.
print(calculator_1.name)
print(calculator_1.type)
print(calculator_1.model)
Run the program to see the value of each attribute on the terminal. The attributes are specific to the object. You can check this by creating another calculator object and providing different attributes this time.
In Python, we define functions for reusable logic. However, when functions are defined inside a class they are called methods of that specific class.
For example, a calculator can add, subtract, multiply and divide two numbers. Let's create these methods inside our Calculator class.
class Calculator:
def __init__(self, name, type, model):
self.name=name
self.type=type
self.model=model
def add(self,a,b):
return
Let's create an object to use these methods.
my_calculator=Calculator("Casio", "Simple", "C3832")Call any of the above methods and provide the parameters.
add_result=my_calculator.add(2,3)
print(add_result)This is the most basic example of how classes work in Python. However, there are some other important concepts in classes such as
These concepts are very helpful when working with real world applications. Let's learn these concepts with practical examples step by step.
Inheritance allows a child class to use the methods and attributes of a parent class.
Suppose we are building an advanced calculator with AdvancedCalculator class, that can perform advanced calculations such as square root and average of two numbers.
class AdvancedCalculator:
def sqrt(self,x):
return x*x
def average(self,x,y):
return (x+y)/2However this class must also be capable of performing simple calculations such as add, subtract, multiply and divide. But these simple methods are already defined in the Calculator class. So defining these methods again is repetitive and time consuming. In that case, we want to use the methods already defined in the Calculator class.
To use methods of the parent class Calculator, we just need to inherit them in the child class AdvanceCalculator. But what if we try to call one of the methods such as add() of the Calculator class in this one.
my_advance_cal=AdvancedCalculator()
add=my_advance_cal.add(1,2)If you run the program, you will see the following error on the terminal
AttributeError: 'AdvancedCalculator' object has no attribute 'add'It simply means we can't use the methods of other classes until we inherit them. Let's inherit the methods and attributes of the parent class Calculator into the AdvanceCalculator class.
class AdvancedCalculator(Calculator):
def sqrt(self,x):
return x**0.5
def average(self,x,y):
return (x+y)/2Notice how we inherit the Calculator class by passing it inside AdvancedCalculator. Now we can use the attributes and methods of the parent class.
my_advance_cal=AdvancedCalculator("Casio","Advance Calculator", "C1402")As you can see we have provided the attributes this time because the parent class has these attributes defined in the __init__() method. Otherwise the class initialization would result in error. Let's try to use the add() method again.
add=my_advance_cal.add(1,2)
print(add)Run the program. This time you will get the actual result instead of the error. This is possible due to the inheritance property of classes that allow us to use the methods and attributes of parent class without defining them manually.
Polymorphism is the ability of different objects to respond to the same method name in different ways.
To understand how polymorphism works we need to dive practically. Imagine a dog and cat. Both are animals and both can generate a sound. Let's implement this analogy by using two Python classes Dog and Cat.
class Dog():
def sound(self):
return "Bark"
class Cat():
def sound(self):
return "Meow"As you can see both have the same method name but return different responses. Let's create objects and call them one by one to see what actually happens.
my_dog=Dog()
my_cat=Cat()
dog_sound=my_dog.sound()
cat_sound=my_cat.sound()
print(dog_sound) #Bark
print(cat_sound) #MeowAs you can see both methods work differently for their objects due to the polymorphism.
Encapsulation allows classes to protect its attributes and allows access only through controlled methods.
There are some cases where we want to protect the data such as account balance of users. Let's create a BankAccount class to understand how encapsulation works.
class BankAccount:
def __init__(self):
self.balance = 0Anyone can directly alter the balance attribute just as I did in the following example.
user_account = BankAccount()
user_account.balance = 5000
print(user_account.balance)But we can protect the balance attribute with encapsulation. To do so we need to define the attribute with a prefix __. Any attribute defined with this prefix cannot be accessed directly.
class BankAccount:
def __init__(self):
self.__balance = 0 # private variableLet's try to access the balance now.
user_account=BankAccount()
print(user_account.__balance)By doing so, you will get the following error.
AttributeError: 'BankAccount' object has no attribute '__balance'.To access it we can define methods that can change and access these attributes. Let's define these methods.
class BankAccount:
def __init__(self):
self.__balance = 0 # private variable
def deposit(self, amount):
if amount > 0:
self.__balance += amount
def withdraw(self, amount
With the above methods we can deposit and withdraw amounts, and can also check the balance. Let's try to check the balance this time with the get_balance method.
user_account=BankAccount()
user_balance=user_account.get_balance()
print(user_balance) # it will print 0Notice how we can protect the specific attributes in classes through encapsulation.
Classes are the blueprints for creating objects. They provide key benefits such as reusability, code organization, polymorphism, encapsulation and inheritance. These benefits cannot be gained through a procedural programming approach. If you want to dive further into Classes in Python, I recommend you read this article on Object Oriented Programming In Python