Master Python Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Classes & Objects in Python

Learn how to create classes, objects, attributes and methods in Python using practical real-world examples.

What are Classes and Objects?

Class and Object are fundamental concepts of Object-Oriented Programming.

A class is a blueprint or template that defines the properties and behaviors of an entity.

An object is a real instance created from that class.

For example, if Student is a class, then Rahul, Priya and Amit can be individual student objects.

Real-World Analogy

Think of a house blueprint.

  • Blueprint → Class
  • Actual house → Object

One blueprint can be used to construct multiple houses. Similarly, one Python class can be used to create multiple objects.

Class & Object Diagram

This diagram shows how a class works as a blueprint for creating multiple objects.

Student Class
Blueprint
↓
Object 1

Samiksha
.NET Full Stack

Object 2

Pradnya
Python Full Stack

Object 3

Samadhan
Java Full Stack

Creating a Class

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

class Student:
    pass

The pass statement is used when we want to create an empty class temporarily.

Creating an Object

After defining a class, we can create an object by calling the class like a function.

class Student:
    pass


student1 = Student()
student2 = Student()

print(student1)
print(student2)

Here, student1 and student2 are two different objects of the Student class.

Class Attributes and Object Attributes

Attributes are variables that store information about an object.

class Student:
    pass


student1 = Student()

student1.name = "Rahul"
student1.course = ".NET Full Stack"
student1.fees = 35000

print(student1.name)
print(student1.course)
print(student1.fees)

Output:

Rahul
.NET Full Stack
35000

Methods in a Class

A method is a function defined inside a class. It represents behavior or an action of an object.

class Student:

    def display(self):
        print("Student Details")


student = Student()

student.display()

The display() method belongs to the Student class.

Understanding self

self refers to the current object. It allows a method to access the attributes and other methods belonging to that object.

class Student:

    def display(self):
        print("Student object")


student1 = Student()

student1.display()

When student1.display() is called, Python passes student1 as the current object represented by self.

Using __init__()

The __init__() method is commonly used to initialize object attributes when an object is created.

class Student:

    def __init__(self, name, course):
        self.name = name
        self.course = course


student1 = Student("Rahul", ".NET Full Stack")

print(student1.name)
print(student1.course)

Output:

Rahul
.NET Full Stack

The detailed concept of constructors will be covered in the next topic.

Creating Multiple Objects

A single class can be used to create many independent objects.

class Student:

    def __init__(self, name, course):
        self.name = name
        self.course = course


student1 = Student("Rahul", ".NET")
student2 = Student("Priya", "Python")
student3 = Student("Amit", "Java")

print(student1.name, student1.course)
print(student2.name, student2.course)
print(student3.name, student3.course)

Each object stores its own values while using the same class structure.

Complete Student Example

class Student:

    def __init__(self, name, age, course):
        self.name = name
        self.age = age
        self.course = course

    def display_details(self):
        print("Name:", self.name)
        print("Age:", self.age)
        print("Course:", self.course)


student1 = Student(
    "Sneha",
    22,
    "Python Full Stack"
)

student1.display_details()

Output:

Name: Sneha
Age: 22
Course: Python Full Stack

Class vs Object

Class Object
Blueprint or template Instance of a class
Defines structure Contains actual data
Does not represent a specific entity Represents a specific entity
Example: Student Example: student1

Objects Have Independent Data

Different objects created from the same class can contain different values.

class Student:

    def __init__(self, name):
        self.name = name


student1 = Student("Rahul")
student2 = Student("Priya")

student1.name = "Amit"

print(student1.name)
print(student2.name)

Output:

Amit
Priya

Changing student1 does not change the value stored in student2.

CIIT Training Example 📩🌍

Suppose CIIT needs to maintain information about different training courses.

class Course:

    def __init__(self, name, duration, fees):
        self.name = name
        self.duration = duration
        self.fees = fees

    def display(self):
        print("Course:", self.name)
        print("Duration:", self.duration)
        print("Fees:", self.fees)


course1 = Course(
    "Python Full Stack",
    "6 Months",
    35000
)

course2 = Course(
    ".NET Full Stack",
    "6 Months",
    35000
)

course1.display()
course2.display()

Here, Course is the class and course1 and course2 are objects containing different course information.

Output
Course: Python Full Stack
Duration: 6 Months
Fees: 35000
Course: .NET Full Stack
Duration: 6 Months
Fees: 35000

Important Rules

  • Use the class keyword to define a class.
  • Class names are conventionally written using PascalCase.
  • Create an object by calling the class.
  • Use self to refer to the current object.
  • Use __init__() to initialize object data.
  • Methods are functions defined inside a class.

Common Beginner Mistakes

  • Forgetting indentation inside the class.
  • Forgetting the colon after the class declaration.
  • Forgetting self in instance methods.
  • Confusing class names with object names.
  • Calling an instance method without an object.

Interview Questions

  1. What is a class in Python?
  2. What is an object?
  3. What is the difference between class and object?
  4. How do you create an object in Python?
  5. What is self?
  6. What is the purpose of __init__()?
  7. Can multiple objects be created from one class?

CIIT Learning Point

Think of a class as a blueprint and an object as the actual entity created from that blueprint. Once you understand this relationship, Python OOPs becomes much easier to understand and apply in real-world projects.

CIIT Practice Tasks

  1. Create a Student class with name, age and course.
  2. Create three Student objects with different values.
  3. Create an Employee class with name, department and salary.
  4. Create a Course class with course name, duration and fees.
  5. Add a method to each class that displays the object's information.

Summary :

A class is a blueprint used to define the structure and behavior of objects. An object is an instance of a class. Python classes can contain attributes that store data and methods that define behavior. The self keyword refers to the current object, while __init__() is commonly used to initialize object attributes. One class can be used to create multiple objects, and each object can maintain its own independent data.