0% found this document useful (0 votes)
15 views16 pages

Advanced Python OOP: Classes & Objects

The document outlines a practical course on Advanced Python Programming focusing on classes and objects, constructors, destructors, and various types of methods. It introduces Object-Oriented Programming (OOP) principles, components, and features, along with detailed explanations and examples of class and object creation in Python. Additionally, it includes practical exercises for students to implement their understanding through coding tasks.

Uploaded by

bnchavan519
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views16 pages

Advanced Python OOP: Classes & Objects

The document outlines a practical course on Advanced Python Programming focusing on classes and objects, constructors, destructors, and various types of methods. It introduces Object-Oriented Programming (OOP) principles, components, and features, along with detailed explanations and examples of class and object creation in Python. Additionally, it includes practical exercises for students to implement their understanding through coding tasks.

Uploaded by

bnchavan519
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Smt.

Chandibai Himathmal Mansukhani College

SEC: Advanced Python Programming


Practical 01: Classes and Objects

Learning Objectives

Students will be able to:

Content:
➢ Working with Classes and Objects.
➢ Understanding Constructors and Destructors.
➢ Exploring Self-Variable and Types of Variables
➢ Understanding Types of Methods.
➢ Passing Members of One Class to Another Class
Process:
➢ Creating classes with constructors and destructors.
➢ Defining and initializing class attributes using self-variable and different types of variables.
➢ Implementing different types of methods (instance, class, static).
Prior Knowledge:
➢ Basic knowledge of Object Oriented Programming.
➢ Familiarity with Python Language

❖ What is Object-Oriented Programming (OOPs)?

Object-Oriented Programming (OOPs) is a programming paradigm that organizes code using objects
and classes. It focuses on structuring programs in a way that mimics real-world entities by grouping data
and behavior together.

❖ Principles of OOPs
OOPs is based on four main concepts:

Encapsulation Wrapping data and methods into a single unit (class) and restricting direct access
to some components.
Abstraction Hiding complex implementation details and showing only the necessary features to
the user.
Inheritance Allowing a class (child) to inherit properties and methods from another class
(parent), promoting code reusability.
Polymorphism Enabling a single interface to represent different underlying forms (e.g., method
overloading and overriding).

Batch No:___________/ Roll No:_____________ 1


[Link] Himathmal Mansukhani College

❖ Components of OOPs

Component Description
Class A blueprint or template for creating objects that defines attributes and methods.
Object An instance of a class that holds actual data and can use class methods.
Attributes Variables that store data related to an object (also called properties or data
members).
Methods Functions defined inside a class that describe the behavior of objects.

❖ Features of Object-Oriented Programming System (OOPS)

Feature Description
Encapsulation Bundling data and methods together into a single unit (class) while restricting
direct access.
Abstraction Hiding complex implementation details and exposing only essential
functionalities to the user.
Inheritance Allowing a class to inherit properties and behaviors from another class,
promoting code reuse.
Polymorphism Enabling a single interface to represent multiple underlying implementations,
such as method overloading and overriding.
Class & Object A class is a blueprint for creating objects, and an object is an instance of a
class.
Message Objects communicate with each other by calling methods and exchanging
Passing information.
Modularity Breaking a large program into smaller, independent modules (classes) for
better organization and maintenance.
Code The ability to reuse existing classes and methods to reduce redundancy and
Reusability improve efficiency.

❖ Class:

A class is a blueprint for creating objects. It defines attributes (variables) and behaviors (methods) that
objects of that class will have.

Syntax:

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 2


[Link] Himathmal Mansukhani College

❖ What is an Object in Python?

An object is an instance of a class that contains attributes (data) and methods (functions) defined in the
class.

❖ Points About Objects

✔ Objects store actual values (unlike classes, which are just blueprints).
✔ Multiple objects can be created from the same class, each having its own data.
✔ Objects allow data encapsulation, meaning data and behavior are bundled together

❖ What is self in Python?

The self variable represents the current instance of the class. It is used to access instance variables and
methods inside the class.

❖ Why is self Needed?

• self allows each object of a class to have its own data.


• It helps in differentiating between instance variables and local variables inside a method.
• It must be the first parameter of any instance method in a class.

❖ Constructors and Destructors in Python

1) Constructor (__init__ Method)

• A constructor is a special method in Python used to initialize objects.


• It is called automatically when an object is created.
• Defined using __init__(self, ...).

Batch No:___________/ Roll No:_____________ 3


[Link] Himathmal Mansukhani College
2) Destructor (__del__ Method)
• A destructor is a special method in Python used to clean up resources when an object is deleted or goes
out of scope.
• Defined using __del__(self).

✔ Both help in efficient memory management in Object-Oriented Programming.

Q1. Write a Python program to create a class Student with a constructor to


initialize name and age. The class should also have a method display() to print
the student's details. Create an object of the class and call the display() method.
Source Code:

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 4


[Link] Himathmal Mansukhani College
Output:

Q2. Write a Python program to create a class BankAccount that simulates a simple
bank account. The class should include:

A constructor to initialize the account holder's name and account balance.


A method deposit()to add money to the account.
A method withdraw() to withdraw money, ensuring the balance is sufficient before
allowing the transaction.
A destructor () to display a message when the account is closed.
Create an object of the class, perform deposit and withdrawal operations, and then
delete the object manually to trigger the destructor.
Source Code:

Batch No:___________/ Roll No:_____________ 5


[Link] Himathmal Mansukhani College

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 6


[Link] Himathmal Mansukhani College
Output:

❖ Types of Variables in Python OOP

Type of Variable Description Defined Inside Accessed Using


Instance Variable Belongs to a specific object; Inside the constructor self.variable_name within
different for each instance of (__init__ method) instance methods.
the class. using self.
Class Variable Shared among all instances Inside the class but ClassName.variable_name
of a class; same for every outside any method. or self.variable_name.
object.
Local Variable Declared inside a method Inside a method, used Only within the method
and only accessible within temporarily. where it is defined.
that method.

Batch No:___________/ Roll No:_____________ 7


[Link] Himathmal Mansukhani College

Q3. Write a Python program to create a class Employee that demonstrates


different types of variables:
1. Class Variable (company) → Shared among all employees (represents the
company name).
2. Instance Variables (name, salary) → Unique for each employee (stores
name and salary).
3. Local Variable (new_salary) → Used inside a method to calculate the
updated salary.
Program Requirements:
• The class should have:
o A constructor to initialize name and salary.
o A method to display employee details.
o A method to update salary by adding an increment.
• Take user input for employee name, salary, and salary increment.
• Modify the company name and display updated details.
Source Code:

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 8


[Link] Himathmal Mansukhani College

Batch No:___________/ Roll No:_____________ 9


[Link] Himathmal Mansukhani College
Output:

❖ Types of Methods in Python OOP


Methods in a class can be categorized into three types:

Type of Method Description Defined Using Accessed Using


Instance Method Works with instance def Called using
variables; can modify method_name(self): object_name.method_name()
object-specific data.
Class Method Works with class @classmethod and def Called using
variables; applies to the method_name(cls): ClassName.method_name()
class rather than instances. or
object_name.method_name()
Static Method Independent of class and @staticmethod and def Called using
instance; used for utility method_name(): ClassName.method_name()
functions. or
object_name.method_name()

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 10


[Link] Himathmal Mansukhani College
Q4. Write a Python program to demonstrate the three types of methods in a
class: Instance Method, Class Method, and Static Method.

Create a class Calculator that has:

Instance Method (avg) → Calculates and returns the average of two numbers.
Class Method (info) → Returns general information about the calculator.
Static Method (add) → Returns the sum of two given numbers.

Source Code:

Batch No:___________/ Roll No:_____________ 11


[Link] Himathmal Mansukhani College

Output:

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 12


[Link] Himathmal Mansukhani College
❖ Passing Members of One Class to Another Class
In Python, we can pass objects of one class as arguments to another class. This allows
one class to use the members (variables & methods) of another class.

Q5. Develop a Python program where a Student class holds student details such as
name and age, and a method to display the details. Create another class, Course,
which takes multiple student objects and stores them. The Course class should have
a method to display the course name along with the details of all enrolled students.
Allow user input for student details and ensure the program handles multiple
students dynamically.
Source Code:

Batch No:___________/ Roll No:_____________ 13


[Link] Himathmal Mansukhani College

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 14


[Link] Himathmal Mansukhani College
Output:

Practical Time: ________________________________

Reported At: ________________________________

Date: ________________________ Teacher’s Signature: _________________________

Batch No:___________/ Roll No:_____________ 15


[Link] Himathmal Mansukhani College
Notes:

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 16

You might also like