Single inheritance

Python Practice › OOP Programs › Program 9

Single inheritance

EasyOOP Programs · 9 of 48

Write a Python program that demonstrates single inheritance.

Output
eating and barking

Approach

  1. The child class lists one parent class in brackets.

Solution

Python
class Animal:    def eat(self):        return "eating" class Dog(Animal):    def bark(self):        return "barking" d = Dog()print(d.eat(), "and", d.bark())
Output
eating and barking

Line by line

Line 5

Dog(Animal) gives Dog everything Animal has.

Learn the concept

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

More oop programs

← All 48 OOP Programs

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

1. Python Variable Tutorial: A Comprehensive Guide

Python Variable:

Introduction:
Variables are an important concept in any programming language, including Python. In simple language, a variable is a named location in memory that stores a value. In Python, you can use variables to store any type of data, including numbers, strings, and even complex data structures like lists and dictionaries.

Creating Variables in Python:

To create a variable in Python, you need to give it a name and assign a value to it. Here’s an example:
Name = “ Omkar “
Age = 25
Profession = “Software Engineer “
In the example above, we created three variables: Name, Age, and Profession.
The name variable is a string.
The age variable is an integer.
The Profession variable is also a string.

 

Python Variable Naming Rules:

When creating variables in Python, there are a few rules that you need to follow:
1. Variable names must start with a letter or underscore (_), followed by any combination of letters, digits, and underscores.
2. Variable names are case-sensitive, which means name and Name are two different variables.
3. A variable name cannot start with a number.
4. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).

Python Variable Types:

Python Variable types:
1. Numbers – Consists of integers, floating-point numbers, and complex numbers.
2. Strings – Consists of characters in quotes.
3. Booleans – Consists of True or False values.
4. Lists – Consists of ordered sequences of elements.
5. Tuples – Consists of ordered, immutable sequences of elements.
6. Sets – Consists of unordered collections of unique elements.
7. Dictionaries – Consists of unordered collections of key-value pairs.

Checking types of the variable:
First, we will create some Python variable.
Name = “ Omkar “
Age = 25
Height = 5.8 ft

To check the type of the variable use the type() function.
print(type(Name)) #Output: <class ‘str’>
print(type(Age)) #Output: <class ‘int’>
print(type(Height)) #Output: <class ‘float’>

Python variable assignment types:

Value Assignment:

To assign the value to a Python variable use equal sign ( = ).
Example:
A = 10
We can also assign the same value to multiple Python variables.
A = B = C = 10

Multiple value assignment:

We can assign different values to different Python variables at the same time. We separate the variables and their values by ‘ , ‘.
Example:
a, b, c = 40, 50, 60
Here,
Value of variable a is 40 , b is 50 and c is 60.

Python Variable scope:

Scope of Variable:

In Python, the scope of a variable determines where in the code the variable can be accessed and used. The scope of a variable is defined by where the variable is created and assigned a value.
There are two types of Python variable scopes: global scope and local scope.

1. Global Scope: Variables created outside of any function or class have a global scope. This means that they can be accessed and modified from anywhere in the code, including within functions and classes.
Example:
number = 100 # global Python variable

def print_number():
print(number) # accessing global variable

def modify_number():
global number # declaring number as global variable
number = 200 # modifying global variable

print_number() # Output: 100
modify_number()
print_number() # Output: 200
In the example above, the variable number is declared outside of any function, so it has a global scope. The function print_number() can access and print the value of the number, and the function modify_number() can modify the value of the number by declaring it as a global variable using the global keyword.

2. Local Scope: Variables created inside a function or class have a local scope. This means that they can only be accessed and modified within that function or class.
Example:
def my_function():
number = 100 # local Python variable
print(number) # accessing local variable

my_function() # Output: 100
print(number) # NameError: name ‘number’ is not defined

Basic Mathematical operations using variables:

Creating variables and assigning value to them:
a, b = 10 , 20

Performing operations and printing output:
print(a+b) #Output: 30
print(a-b) #Output: -10
print(a*b) #Output: 200
print(a/b) #Output: 0.5

Variables are an important concept in Python programming language. They allow you to store and manipulate data in your code and are important for writing effective programs. By understanding how to create, name, and use variables, you can write Python code that is easy to read and maintain.