1 Explain object-oriented principles with examples.
Introduction
Object-Oriented Programming (OOP) is a programming approach that organizes a program using classes
and objects. It helps in creating reusable, secure, and easy-to-maintain programs.
The four main principles of OOP are:
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
1. Encapsulation
Definition
Encapsulation is the process of combining data and methods into a single unit called a class and restricting
direct access to data.
Example
class Student:
def __init__(self):
[Link] = "Ammu"
s = Student()
print([Link])
Advantage
• Protects data from unauthorized access.
2. Abstraction
Definition
Abstraction means hiding unnecessary details and showing only the essential features of an object.
Example
When using an ATM, we withdraw money without knowing the internal working of the machine.
Advantage
• Reduces complexity.
• Makes programs easier to use.
3. Inheritance
Definition
Inheritance allows one class to acquire the properties and methods of another class.
Example
class Parent:
def display(self):
print("Parent Class")
class Child(Parent):
pass
c = Child()
[Link]()
Output
Parent Class
Advantage
• Promotes code reusability.
4. Polymorphism
Definition
Polymorphism means "many forms". The same method can perform different actions depending on the
object.
Example
class Cat:
def sound(self):
print("Meow")
class Dog:
def sound(self):
print("Bark")
Advantage
• Increases flexibility and extensibility.
Summary of OOP Principles
Principle Purpose
Encapsulation Protects data
Abstraction Hides implementation details
Inheritance Reuses existing code
Principle Purpose
Polymorphism One interface, many forms
Conclusion
Object-Oriented Programming is based on Encapsulation, Abstraction, Inheritance, and Polymorphism.
These principles help in developing reusable, secure, and maintainable software.
2 Define class and object? How to create the class and object? How to access the
members of class using the object reference?
Introduction
Class and Object are the basic concepts of Object-Oriented Programming (OOP). A class acts as a blueprint,
while an object is an instance of that class.
Class
Definition
A class is a user-defined data type that contains data members (variables) and member functions (methods).
It serves as a blueprint for creating objects.
Syntax
class ClassName:
statements
Example
class Student:
name = "Ammu"
Object
Definition
An object is an instance of a class. It is used to access the variables and methods defined inside the class.
Syntax
object_name = ClassName()
Example
s = Student()
Creating a Class and Object
Program
class Student:
name = "Ammu"
s = Student()
Here,
• Student is the class.
• s is the object.
Accessing Members Using Object Reference
The members of a class can be accessed using the dot (.) operator with the object reference.
Syntax
object_name.member_name
Program
class Student:
name = "Ammu"
s = Student()
print([Link])
Output
Ammu
Explanation
• s is the object reference.
• [Link] accesses the variable name of the class.
Accessing Methods Using Object Reference
Program
class Student:
def display(self):
print("Welcome to Python")
s = Student()
[Link]()
Output
Welcome to Python
Advantages of Classes and Objects
1. Supports code reusability.
2. Makes programs easy to manage.
3. Provides data security through OOP concepts.
Conclusion
A class is a blueprint that defines properties and behaviors, while an object is an instance of a class. Objects
are created from classes and are used to access class members using the dot (.) operator.
3 Write a short notes on following terms.
i. Constructor
ii. Method
iii. Destructor
Introduction
In Python, Constructors, Methods, and Destructors are important parts of Object-Oriented Programming
(OOP). They help in creating, managing, and destroying objects.
i) Constructor
Definition
A constructor is a special method that is automatically called when an object is created. It is used to initialize
the object's data members.
Syntax
def __init__(self):
statements
Example
class Student:
def __init__(self):
print("Constructor Called")
s = Student()
Output
Constructor Called
Key Point
• Constructor name in Python is __init__().
• It is called automatically when an object is created.
ii) Method
Definition
A method is a function defined inside a class. It is used to perform a specific task and can access the data of
the class.
Syntax
def method_name(self):
statements
Example
class Student:
def display(self):
print("Welcome to Python")
s = Student()
[Link]()
Output
Welcome to Python
Key Point
• Methods define the behavior of an object.
• They are called using the object name.
iii) Destructor
Definition
A destructor is a special method that is automatically called when an object is destroyed. It is used to release
resources and perform cleanup operations.
Syntax
def __del__(self):
statements
Example
class Student:
def __del__(self):
print("Object Destroyed")
s = Student()
del s
Output
Object Destroyed
Key Point
• Destructor name in Python is __del__().
• It is called automatically when an object is deleted.
Difference Between Constructor, Method, and Destructor
Term Purpose
Constructor Initializes an object
Method Performs operations on an object
Destructor Destroys an object and frees resources
Conclusion
A Constructor initializes an object, a Method performs tasks, and a Destructor destroys the object and
releases resources. These are essential components of Object-Oriented Programming in Python.
4 Explain different types of methods used in classes? How to call those methods?
Introduction
A method is a function defined inside a class. Methods are used to perform operations on the data of an
object. In Python, methods are mainly of three types:
1. Instance Method
2. Class Method
3. Static Method
1. Instance Method
Definition
An instance method works with object data. It uses the self parameter and can access instance variables.
Example
class Student:
def display(self):
print("Instance Method")
s = Student()
[Link]()
How to Call?
[Link]()
2. Class Method
Definition
A class method works with class variables and uses the cls parameter. It is declared using the @classmethod
decorator.
Example
class Student:
college = "ABC College"
@classmethod
def show(cls):
print([Link])
[Link]()
How to Call?
[Link]()
3. Static Method
Definition
A static method does not use self or cls. It performs a general task related to the class and is declared using
the @staticmethod decorator.
Example
class Student:
@staticmethod
def greet():
print("Welcome")
[Link]()
How to Call?
[Link]()
Difference Between Methods
Method Type Parameter Used Called Using
Instance Method self Object
Class Method cls Class Name
Static Method None Class Name
Conclusion
Python classes mainly use Instance Methods, Class Methods, and Static Methods. Instance methods work
with object data, class methods work with class data, and static methods perform general utility tasks.
5 Define variable? Explain different types of variables in classes? How to access those
variables?
Introduction
A variable is a name used to store data values in a program. In Python classes, variables are used to store the
properties of objects and classes.
There are mainly two types of variables in classes:
1. Instance Variables
2. Class Variables (Static Variables)
1. Instance Variable
Definition
An instance variable is a variable whose value is different for each object. It is declared using self inside a
constructor or method.
Example
class Student:
def __init__(self):
[Link] = "Ammu"
s = Student()
print([Link])
Output
Ammu
How to Access?
Instance variables are accessed using the object name.
[Link]
2. Class Variable (Static Variable)
Definition
A class variable is shared by all objects of a class. It is declared inside the class but outside the methods.
Example
class Student:
college = "ABC College"
s = Student()
print([Link])
Output
ABC College
How to Access?
Class variables are accessed using the class name.
[Link]
Difference Between Instance Variable and Class Variable
Instance Variable Class Variable
Unique for each object Shared by all objects
Declared using self Declared inside class
Accessed using object Accessed using class name
Summary
Variable Type Access Method
Instance Variable [Link]
Class Variable [Link]
Conclusion
A variable stores data in a program. In Python classes, variables are mainly of two types: Instance Variables
and Class Variables. Instance variables belong to objects, while class variables belong to the class and are
shared by all objects.
6 Define Inheritance? Explain different types of inheritances supported by python.
Introduction
Inheritance is an important feature of Object-Oriented Programming (OOP). It allows one class to acquire the
properties and methods of another class. The existing class is called the Parent (Base) Class, and the new
class is called the Child (Derived) Class.
Syntax
class Parent:
pass
class Child(Parent):
pass
Advantages
• Code Reusability
• Reduces Program Length
• Easy Maintenance
Types of Inheritance in Python
1. Single Inheritance
Definition
A child class inherits from a single parent class.
Example
class Parent:
def show(self):
print("Parent Class")
class Child(Parent):
pass
c = Child()
[Link]()
2. Multiple Inheritance
Definition
A child class inherits from more than one parent class.
Example
class A:
pass
class B:
pass
class C(A, B):
pass
3. Multilevel Inheritance
Definition
A class inherits from another derived class, forming a chain.
Example
class GrandParent:
pass
class Parent(GrandParent):
pass
class Child(Parent):
pass
4. Hierarchical Inheritance
Definition
Multiple child classes inherit from the same parent class.
Example
class Parent:
pass
class Child1(Parent):
pass
class Child2(Parent):
pass
5. Hybrid Inheritance
Definition
Hybrid inheritance is a combination of two or more types of inheritance.
Example
class A:
pass
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
Summary of Types
Type Description
Single One Parent → One Child
Multiple Many Parents → One Child
Multilevel Grandparent → Parent → Child
Hierarchical One Parent → Many Children
Hybrid Combination of Inheritance Types
Conclusion
Inheritance allows a class to reuse the properties and methods of another class. Python supports Single,
Multiple, Multilevel, Hierarchical, and Hybrid Inheritance, making programs reusable, organized, and easy
to maintain.
7 Explain following terms.
i. Operator overloading
ii. Method overloading and overriding
iii. Constructor overloading and overriding.
Introduction
Operator Overloading, Method Overloading/Overriding, and Constructor Overloading/Overriding are
important concepts in Object-Oriented Programming (OOP). They provide flexibility and improve code
reusability.
i) Operator Overloading
Definition
Operator Overloading means giving a special meaning to an existing operator for user-defined objects.
Example
The + operator adds numbers and can also join strings.
print(10 + 20) # Addition
print("Hello" + "Python") # Concatenation
Advantage
• Makes code simple and readable.
ii) Method Overloading and Method Overriding
Method Overloading
Definition
Method Overloading means having methods with the same name but different parameters.
Example
class Demo:
def add(self, a, b=0):
print(a + b)
d = Demo()
[Link](10)
[Link](10, 20)
Advantage
• Same method can perform different tasks.
Method Overriding
Definition
Method Overriding occurs when a child class provides its own implementation of a method already defined in
the parent class.
Example
class Parent:
def show(self):
print("Parent Class")
class Child(Parent):
def show(self):
print("Child Class")
c = Child()
[Link]()
Output
Child Class
Advantage
• Allows customization of inherited methods.
iii) Constructor Overloading and Constructor Overriding
Constructor Overloading
Definition
Constructor Overloading means creating constructors that can accept different numbers of arguments.
Python achieves this using default arguments.
Example
class Demo:
def __init__(self, a=0):
print(a)
d1 = Demo()
d2 = Demo(10)
Advantage
• Creates objects in different ways.
Constructor Overriding
Definition
Constructor Overriding occurs when a child class defines its own constructor instead of using the parent
class constructor.
Example
class Parent:
def __init__(self):
print("Parent Constructor")
class Child(Parent):
def __init__(self):
print("Child Constructor")
c = Child()
Output
Child Constructor
Summary
Concept Meaning
Operator Overloading Giving new meaning to operators
Method Overloading Same method, different parameters
Method Overriding Child class redefines parent method
Constructor Overloading Constructor with different arguments
Constructor Overriding Child class redefines parent constructor
Conclusion
Operator overloading, method overloading/overriding, and constructor overloading/overriding are important
OOP concepts that improve flexibility, code reusability, and program efficiency.
8 Explain super() function with example.
Introduction
The super() function is used in inheritance to access the members (variables and methods) of the parent
class from the child class. It helps in reusing the code of the parent class without explicitly mentioning the
parent class name.
Definition
The super() function returns a temporary object of the parent class, allowing the child class to access parent
class methods and constructors.
Syntax
super().method_name()
or
super().__init__()
Example Program
class Parent:
def display(self):
print("This is Parent Class")
class Child(Parent):
def show(self):
super().display()
print("This is Child Class")
c = Child()
[Link]()
Output
This is Parent Class
This is Child Class
Using super() with Constructor
class Parent:
def __init__(self):
print("Parent Constructor")
class Child(Parent):
def __init__(self):
super().__init__()
print("Child Constructor")
c = Child()
Output
Parent Constructor
Child Constructor
Advantages of super()
1. Accesses parent class methods and constructors.
2. Promotes code reusability.
3. Avoids writing the parent class name repeatedly.
4. Makes inheritance easier to manage.
Conclusion
The super() function is used in inheritance to call the methods and constructors of the parent class from the
child class. It improves code reusability and simplifies program development.
9 Explain the file handling functions with examples.
Introduction
File Handling in Python is used to store and retrieve data from files. Python provides built-in functions to
create, read, write, and manage files.
File Handling Functions
1. open() Function
Definition
The open() function is used to open a file.
Syntax
file = open("[Link]", "mode")
Example
f = open("[Link]", "r")
2. read() Function
Definition
The read() function reads the entire contents of a file.
Example
f = open("[Link]", "r")
print([Link]())
3. write() Function
Definition
The write() function writes data into a file.
Example
f = open("[Link]", "w")
[Link]("Hello Python")
4. append() Mode (a)
Definition
Append mode adds new data to the end of an existing file without deleting old data.
Example
f = open("[Link]", "a")
[Link](" Welcome")
5. close() Function
Definition
The close() function closes the file after use.
Example
f = open("[Link]", "r")
[Link]()
File Modes
Mode Purpose
r Read file
w Write file
a Append data
r+ Read and Write
Complete Example
f = open("[Link]", "w")
[Link]("Python Programming")
[Link]()
f = open("[Link]", "r")
print([Link]())
[Link]()
Output
Python Programming
Advantages of File Handling
1. Stores data permanently.
2. Easy retrieval of information.
3. Useful for managing large amounts of data.
Conclusion
File handling in Python allows users to create, read, write, and append data in files. Important functions
include open(), read(), write(), and close(), which help manage files efficiently.
10 Explain file opening modes with examples
Introduction
When a file is opened in Python, a mode specifies the purpose of opening the file, such as reading, writing, or
appending data. Python provides several file opening modes.
File Opening Modes in Python
1. Read Mode (r)
Definition
The read mode is used to read data from an existing file. It is the default mode.
Example
f = open("[Link]", "r")
print([Link]())
[Link]()
2. Write Mode (w)
Definition
The write mode is used to write data into a file. If the file already exists, its contents are overwritten.
Example
f = open("[Link]", "w")
[Link]("Hello Python")
[Link]()
3. Append Mode (a)
Definition
The append mode adds data to the end of an existing file without deleting previous contents.
Example
f = open("[Link]", "a")
[Link](" Welcome")
[Link]()
4. Read and Write Mode (r+)
Definition
The r+ mode allows both reading and writing in the same file.
Example
f = open("[Link]", "r+")
print([Link]())
[Link](" Python")
[Link]()
5. Write and Read Mode (w+)
Definition
The w+ mode allows reading and writing. It creates a new file or overwrites an existing file.
Example
f = open("[Link]", "w+")
[Link]("Python")
[Link]()
6. Append and Read Mode (a+)
Definition
The a+ mode allows appending and reading data from a file.
Example
f = open("[Link]", "a+")
[Link](" Programming")
[Link]()
Summary of File Modes
Mode Purpose
r Read only
w Write only
a Append only
r+ Read and Write
w+ Write and Read
a+ Append and Read
Conclusion
File opening modes determine how a file is accessed in Python. Common modes include r, w, a, r+, w+, and
a+. Choosing the correct mode helps in efficient file handling and data management.
Quick Revision
• r → Read
• w → Write
• a → Append
• r+ → Read + Write
• w+ → Write + Read
• a+ → Append + Read
11 Explain log files and config files with examples
Introduction
Log files and Config files are commonly used in software applications. Log files store information about
program activities, while config files store settings used by a program.
1. Log Files
Definition
A log file is a file that records events, activities, errors, and messages generated by a program while it is
running.
Example
A website stores information such as:
• User login details
• Error messages
• System activities
Example Program
f = open("[Link]", "w")
[Link]("User logged in")
[Link]()
Sample Log File Content
User logged in
Advantages
• Helps in debugging errors.
• Maintains a record of activities.
• Useful for monitoring applications.
2. Config Files
Definition
A config (configuration) file is a file that stores settings and preferences used by a program.
Example
A configuration file may contain:
Username = Admin
Language = English
Theme = Dark
Example Program
f = open("[Link]", "w")
[Link]("Theme = Dark")
[Link]()
Advantages
• Easy to change program settings.
• No need to modify source code.
• Makes applications flexible.
Difference Between Log Files and Config Files
Log File Config File
Stores program activities Stores program settings
Used for tracking and debugging Used for configuration
Changes frequently Changes occasionally
Conclusion
A log file records the activities and errors of a program, whereas a config file stores settings and preferences
used by the program. Both are important for efficient software development and maintenance.