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

Cartesian product of two sets

Python Practice › Set Programs › Program 22

Cartesian product of two sets

EasySet Programs · 22 of 48

Write a Python program to find the cartesian product of two sets.

Input
{1, 2}, {"a", "b"}
Output
[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]

Approach

  1. itertools.product; sort for a stable display.

Solution

Python
from itertools import productprint(sorted(product({1, 2}, {"a", "b"})))
Output
[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]

Learn the concept

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

More set programs

← All 48 Set Programs

Longest word in a set

Python Practice › Set Programs › Program 48

Longest word in a set

EasySet Programs · 48 of 48

Write a Python program to find the longest word in a set.

Input
{"cat", "elephant", "dog", "hippopotamus"}
Output
hippopotamus

Approach

  1. max(s, key=len).

Solution

Python
print(max({"cat", "elephant", "dog", "hippopotamus"}, key=len))
Output
hippopotamus
Note

If two words share the longest length, the result depends on set order; use max(sorted(s), key=len) to make it predictable.

Learn the concept

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

More set programs

← All 48 Set Programs

Books two readers both own

Python Practice › Set Programs › Program 47

Books two readers both own

EasySet Programs · 47 of 48

Write a Python program to create two sets of books and find the books in both.

Output
['Atomic Habits', 'Wings of Fire']

Approach

  1. &.

Solution

Python
asha = {"Wings of Fire", "The Alchemist", "Atomic Habits"}ravi = {"Atomic Habits", "Sapiens", "Wings of Fire"}print(sorted(asha & ravi))
Output
['Atomic Habits', 'Wings of Fire']

Learn the concept

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

More set programs

← All 48 Set Programs

Set of favourite movies

Python Practice › Set Programs › Program 46

Set of favourite movies

EasySet Programs · 46 of 48

Write a Python program to create a set of favourite movies and add one more.

Output
['Dangal', 'Inception', 'Interstellar', 'Lagaan']

Approach

  1. add() then print sorted.

Solution

Python
movies = {"Inception", "Dangal", "Interstellar"}movies.add("Lagaan")print(sorted(movies))
Output
['Dangal', 'Inception', 'Interstellar', 'Lagaan']

Learn the concept

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

More set programs

← All 48 Set Programs

Set of favourite actors

Python Practice › Set Programs › Program 45

Set of favourite actors

EasySet Programs · 45 of 48

Write a Python program to create a set of your favourite actors.

Output
4 ['Akshay Kumar', 'Robert Pattinson', 'Shah Rukh Khan', 'Tom Hardy']

Approach

  1. A set of strings; print it sorted.

Solution

Python
actors = {"Shah Rukh Khan", "Akshay Kumar", "Tom Hardy", "Robert Pattinson", "Akshay Kumar"}print(len(actors), sorted(actors))
Output
4 ['Akshay Kumar', 'Robert Pattinson', 'Shah Rukh Khan', 'Tom Hardy']

Line by line

Line 1

“Akshay Kumar” appears twice but is stored once.

Learn the concept

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

More set programs

← All 48 Set Programs