Classes and Objects in Python

Subject: Data Structures and Algorithms · Language: Python · Level: beginner · 7 min read

Understand Python classes, objects, attributes, methods, constructors, self, and representing real-world entities in code.

A class is a blueprint that describes what data and behavior an object should have. An object is a real instance created from that class. This idea helps organize code around real-world or logical entities.

For example, a `Student` class can store a student's name, roll number, and marks. Each student object can have different values, but all objects follow the same structure defined by the class.

The `__init__` method is used to initialize an object. It runs automatically when the object is created. The word `self` refers to the current object, so `self.name` means the name stored inside that particular object.

Methods are functions inside a class. They define actions related to the object. For example, a student object may have a method to display details or check whether the student passed. This keeps data and behavior together.

Classes are useful in DSA when one item has multiple connected values. A linked list node stores data and a reference to the next node. A tree node stores data and references to left and right children. These are natural class-based structures.

Students should understand that OOP is not only for big software projects. Even a small `Node` class in DSA shows why classes are useful: they group related values into one meaningful unit.

Python supports object-oriented programming. That means Python programs can be organized using classes and objects.

A class is a blueprint, and an object is a real item created from that blueprint. For example, `Student` can be a class, and Rahul, Priya, or Amit can be objects of that class.

This idea helps us represent real-world things in code. A student has data such as name and marks, and a student can also perform actions such as displaying details or checking pass status.

What Is a Class?

A class defines the structure and behavior of an object. Structure means what data the object will store. Behavior means what actions the object can perform.

In Python, a class is created using the `class` keyword.

```python

class Student:

pass

```

Here, `Student` is a class. The `pass` keyword is used when the class body is empty for now.

What Is an Object?

An object is created from a class. Once an object is created, it can store its own data.

```python

class Student:

pass

s1 = Student()

```

Here, `s1` is an object of the `Student` class.

You can create many objects from the same class. Each object can store different values.

Attributes

Attributes are variables that belong to an object. They store object data.

```python

s1.name = "Rahul"

s1.marks = 85

```

Here, `name` and `marks` are attributes of object `s1`.

Methods

Methods are functions written inside a class. They define actions related to the object.

```python

class Student:

def display(self):

print("Student details")

```

Output note

```text

No output is shown until an object calls display().

```

A method is called using the object name.

```python

s1.display()

```

Constructor Using __init__

The `__init__` method is a special method in Python. It runs automatically when an object is created.

It is commonly called a constructor because it initializes object data.

```python

class Student:

def __init__(self, name, marks):

self.name = name

self.marks = marks

```

When we create `Student("Rahul", 85)`, Python sends the values to `__init__`, and the object stores them.

self Keyword

`self` refers to the current object. It is used to access attributes and methods inside the class.

```python

self.name = name

self.marks = marks

```

Here, `self.name` is the object's attribute, and `name` is the parameter received by the constructor.

For beginners, remember this simple idea: `self` means this current object.

Creating Multiple Objects

A class can create many objects. Each object has its own data.

```python

s1 = Student("Rahul", 85)

s2 = Student("Priya", 92)

```

Now `s1` and `s2` are two different student objects.

Accessing Object Data

Use the dot operator to access object attributes and methods.

```python

print(s1.name)

print(s1.marks)

```

Output

```text

Rahul

85

```

The dot operator connects the object with its data or behavior.

Example Program

```python

class Student:

def __init__(self, name, marks):

self.name = name

self.marks = marks

student = Student("Rahul", 85)

print("Name =", student.name)

print("Marks =", student.marks)

```

Output

```text

Name = Rahul

Marks = 85

```

This program creates a `Student` class with a constructor. Then it creates one object and prints its details.

Classes and Objects in DSA

In beginner DSA, many problems use variables and lists. But classes become useful when one item has multiple related values.

For example, a `Student` may have name, roll number, and marks. A `Node` in a linked list has data and a reference to the next node. A `Pair` can store two values together.

Linked lists, trees, graphs, heaps, and custom data structures often use classes in Python.

A class is a blueprint, and an object is a real value created from that blueprint. This idea helps us group related data and behavior together. For example, a `Student` class can store name and marks, and it can also have methods to display details.

Each object has its own data. If two student objects are created, changing the marks of one student does not automatically change the marks of the other student. This is because each object keeps its own instance variables.

Methods are functions written inside a class. They describe actions that an object can perform. A method normally receives `self` so it can access the object's own data. This keeps related logic close to related data.

Classes are useful in DSA when one item has multiple pieces of information. A linked list node has data and next reference. A tree node has data, left child, and right child. A pair object can store two related values together.

Students should not think OOP is only for large projects. Even in DSA, small classes make custom structures easier to understand. The key is to use a class when data naturally belongs together.

Practice Question

Create a `Book` class with title, author, and price. Add a constructor to initialize these values, create one object, and print its details.

More Python Lessons

Browse all PrepCampus study materials