Create objects under if __name__ == “__main__”

Python Practice › OOP Programs › Program 8

Create objects under if __name__ == "__main__"

EasyOOP Programs · 8 of 48

Write a Python class and create its object inside the if __name__ == "__main__": block.

Output
Hello, SQA Tools

Approach

  1. Code in that block runs only when the file is executed directly, not when imported.

Solution

Python
class Greeter:    def __init__(self, name):        self.name = name     def greet(self):        return f"Hello, {self.name}"  if __name__ == "__main__":    g = Greeter("SQA Tools")    print(g.greet())
Output
Hello, SQA Tools

Line by line

Lines 9-11

Another file can import Greeter without creating this demo object.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Class and module name

Python Practice › OOP Programs › Program 7

Class and module name

EasyOOP Programs · 7 of 48

Write a Python program to get an object’s class name and module name.

Output
Product
__main__

Approach

  1. type(obj).__name__ and __module__.

Solution

Python
class Product:    pass p = Product()print(type(p).__name__)print(Product.__module__)
Output
Product
__main__

Line by line

Line 6

The module is __main__ because the class is defined in the script being run.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Class method

Python Practice › OOP Programs › Program 6

Class method

EasyOOP Programs · 6 of 48

Write a Python class with a class method used as an alternative constructor.

Output
2 11 2026

Approach

  1. @classmethod receives the class as cls.

Solution

Python
class Date:    def __init__(self, day, month, year):        self.day, self.month, self.year = day, month, year     @classmethod    def from_string(cls, text):        day, month, year = map(int, text.split("-"))        return cls(day, month, year) d = Date.from_string("02-11-2026")print(d.day, d.month, d.year)
Output
2 11 2026

Line by line

Lines 5-8

from_string parses text and calls cls(...), so subclasses get objects of their own type.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Static method

Python Practice › OOP Programs › Program 5

Static method

EasyOOP Programs · 5 of 48

Write a Python class with a static method.

Output
True False

Approach

  1. @staticmethod needs no self; it is a helper grouped with the class.

Solution

Python
class MathUtils:    @staticmethod    def is_even(n):        return n % 2 == 0 print(MathUtils.is_even(10), MathUtils.is_even(7))
Output
True False

Line by line

Lines 2-4

The method uses only its arguments, so it does not need an object.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Class variables

Python Practice › OOP Programs › Program 4

Class variables

EasyOOP Programs · 4 of 48

Write a Python class with a class variable shared by all objects.

Output
SQA Tools SQA Tools 2

Approach

  1. Define the variable in the class body; update it through the class name.

Solution

Python
class Employee:    company = "SQA Tools"    count = 0     def __init__(self, name):        self.name = name        Employee.count += 1 e1 = Employee("Asha")e2 = Employee("Ravi")print(e1.company, e2.company, Employee.count)
Output
SQA Tools SQA Tools 2

Line by line

Lines 2-3

Class variables are shared by every object.

Line 7

Updating through Employee.count changes the shared value; self.count += 1 would create a separate copy on that object instead.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Instance method

Python Practice › OOP Programs › Program 3

Instance method

EasyOOP Programs · 3 of 48

Write a Python class with an instance method that uses the object’s data.

Output
28.27

Approach

  1. An instance method takes self and reads the attributes.

Solution

Python
class Circle:    def __init__(self, radius):        self.radius = radius     def area(self):        return round(3.14159 * self.radius ** 2, 2) c = Circle(3)print(c.area())
Output
28.27

Line by line

Lines 5-6

self.radius is the radius of the circle the method was called on.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Instance variables

Python Practice › OOP Programs › Program 2

Instance variables

EasyOOP Programs · 2 of 48

Write a Python class with instance variables and show that each object has its own values.

Output
Tata red
Honda black
{'brand': 'Tata', 'colour': 'red'}

Approach

  1. Set the variables on self in __init__.

Solution

Python
class Car:    def __init__(self, brand, colour):        self.brand = brand        self.colour = colour a = Car("Tata", "red")b = Car("Honda", "white")b.colour = "black"print(a.brand, a.colour)print(b.brand, b.colour)print(vars(a))
Output
Tata red
Honda black
{'brand': 'Tata', 'colour': 'red'}

Line by line

Line 8

Changing b.colour does not affect a; instance variables belong to one object.

Line 11

vars() shows an object’s instance variables as a dictionary.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Class with a constructor

Python Practice › OOP Programs › Program 1

Class with a constructor

EasyOOP Programs · 1 of 48

Write a Python class with a constructor (__init__) that sets the object’s attributes.

Output
Created student Asha
Asha 7

Approach

  1. __init__ runs automatically when an object is created.

Solution

Python
class Student:    def __init__(self, name, roll_no):        self.name = name        self.roll_no = roll_no        print(f"Created student {name}") s = Student("Asha", 7)print(s.name, s.roll_no)
Output
Created student Asha
Asha 7

Line by line

Line 2

self is the new object; name and roll_no come from the call.

Lines 3-4

Assigning to self.<name> stores the values on that object.

Line 7

Calling the class creates the object and runs __init__ with these arguments.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Python OOP Programs with Solutions

Python Practice › OOP Programs

Python OOP Programs with Solutions

48 programs44 easy4 medium0 hard

48 object-oriented programming exercises: constructors, instance and class variables, class and static methods, every kind of inheritance, overriding, abstract classes, encapsulation, properties and small real-world systems such as bank accounts, carts and courses.

Before you start

Read the Classes & Objects and Inheritance and Polymorphism & Encapsulation lesson first if the topic is new to you.

All programs