0% found this document useful (0 votes)
7 views

1. Module1-OOPs in Python

The document provides an overview of Object-Oriented Programming (OOP) concepts in Python, including classes, objects, and the types of variables and methods. It discusses the limitations of procedure-oriented programming and highlights key OOP features such as encapsulation, inheritance, and polymorphism. Additionally, it includes programming questions to apply these concepts, such as creating classes and demonstrating interactions between them.

Uploaded by

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

1. Module1-OOPs in Python

The document provides an overview of Object-Oriented Programming (OOP) concepts in Python, including classes, objects, and the types of variables and methods. It discusses the limitations of procedure-oriented programming and highlights key OOP features such as encapsulation, inheritance, and polymorphism. Additionally, it includes programming questions to apply these concepts, such as creating classes and demonstrating interactions between them.

Uploaded by

tovilas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Python

Semester-2
1. OOPs in Python
● Introduction to OOPs
● Problems in Procedure-Oriented Approach
● Features of Object-Oriented Programming System (OOPS)
● Constructors and Destructors
● Classes and Objects
● Creating a Class
● Self-Variable
● Types of Variables
● Types of Methods
● Passing Members of One Class to Another Class
● Problems in Procedure-Oriented Approach

❖ Problems in Procedure-Oriented Approach

❖ No proper way to model real-world objects.

❖ Functions and data are separate, leading to confusion.

❖ Difficult to scale for large programs.


● Features of Object-Oriented Programming System (OOPS)

❖ Encapsulation: Binding data and methods together.

❖ Abstraction: Hiding implementation details.

❖ Inheritance: Reusing code through parent-child relationships.

❖ Polymorphism: Same function name with different behaviors.


Class and Objects
Blueprint for creating objects. Example:
class Car:
Syntax:
def _init_(self, brand):
class ClassName:
self.brand = brand
Pass
def show(self):
print(f"Car brand: {self.brand}")
Object: Instance of a class.
Syntax: obj = ClassName()
car1 = Car("Toyota")
car1.show()
Example:
class Car: Where,
def _init_(self, brand): self: The self parameter represents the instance of the class and is
self.brand = brand used to access the attributes and methods associated with the specific
object.
def show(self):
__init__: The __init__ method is the constructor in Python,
print(f"Car brand: {self.brand}") automatically called when an object is created, used here to initialize
the brand attribute of the Car instance.
car1 = Car("Toyota")
car1.show()
Types of Variables in Python
Variables in Python store data. Based on their association, they can be categorized into Instance
Variables, Class Variables, and Local Variables.

(i) Instance Variables:


These variables are defined inside methods and belong to each object. They are accessed using
self.
self.variable_name = value
Example:
class Student:
def __init__(self, name, age):
self.name = name # Instance variable
self.age = age # Instance variable

s1 = Student("Aditya", 18)
s2 = Student("Siddhi", 19)
print(s1.name) # Output: Aditya
print(s2.name) # Output: Siddhi
***
(ii) Class Variables:
These variables are shared among all instances of a class and are defined
outside methods but within the class.
class ClassName:
variable_name = value

Example:
class School:
school_name = "VPM College" # Class variable
print(School.school_name) # Output: VPM College
(iii) Local Variables:
These variables are defined inside a method and can only be used within that method.
def method_name(self):
variable_name = value

Example:
class Calculator:
def add(self, a, b):
result = a + b # Local variable
return result

calc = Calculator()
print(calc.add(10, 5)) # Output: 15
2. Types of Methods in Python

Methods in Python are functions inside a class. They can be Instance Methods, Class Methods, or
Static Methods.

(i) Instance Methods:


These methods work on instance variables and require self as the first parameter.
def method_name(self, args):
# method body

Example:
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self): # Instance method
return 3.14 * self.radius ** 2
c1 = Circle(5)
***
(ii) Class Methods:
These methods operate on class variables and use the @classmethod decorator. They require cls
as the first parameter.

@classmethod
def method_name(cls, args):
# method body

class School:
school_name = "XYZ College"

@classmethod
def get_school_name(cls):
return cls.school_name

print(f"School name: {School.get_school_name()}") # Output: School name: XYZ College


***
(iii) Static Methods:
These methods neither work on instance nor class variables. They are defined using
@staticmethod and used for utility purposes.
@staticmethod
def method_name(args):
# method body

class Math:
@staticmethod
def add(a, b):
return a + b

result = Math.add(20, 15)


print(f"Sum: {result}") # Output: Sum: 35
Example:
class ClassGroup:
students = ["Aditya", "Siddhi", "Richa", "Sonali"] # Class variable
@classmethod
def show_students(cls):
return cls.students

# Create an instance of ClassGroup


group = ClassGroup()
# Call the class method to display the list of students
print("List of Students:") Output:
for student in group.show_students(): List of Students:
print(student) Aditya
Siddhi
Richa
Sonali
5. Passing Members of One Class to Another Class
You can pass an instance of one class to another class
as an attribute to enable interaction between them. This program defines two classes: Engine and Car.
class ClassA: The Engine class includes an __init__ method that
def __init__(self, value):
self.value = value initializes the horsepower attribute. The Car class

class ClassB: has its own __init__ method, which accepts make and
def __init__(self, class_a_instance): engine as arguments and assigns the engine object
self.class_a = class_a_instance
to the engine attribute. An Engine instance with a
Example: horsepower of 150 is created, followed by a Car
class Engine:
def __init__(self, horsepower): instance with the make "Toyota" and the engine object.
self.horsepower = horsepower
This demonstrates that the Engine object is passed to
class Car: the Car class, so car.engine.horsepower will return
def __init__(self, make, engine):
self.make = make 150, confirming the successful integration of the
self.engine = engine
# Creating instances engine into the car.
engine = Engine(150)
car = Car("Toyota", engine)
print(car.make) # Output: Toyota
print(car.engine.horsepower) # Output: 150
Programming Questions
1. Introduction to OOPs: Create a class Student with attributes name and age. Implement a method to display
the student's details.

2. Problems in Procedure-Oriented Approach: Write a function to calculate the area of a rectangle, then
rewrite it using a class with attributes length and width.

3. Features of Object-Oriented Programming System (OOPS): Create a class Car and a subclass
ElectricCar. Add attributes and a method to display car details, demonstrating inheritance and polymorphism.

4. Constructors and Destructors: Create a class Book with attributes title and author. Use a constructor to
initialize and a destructor to print a message on deletion.

5. Passing Members of One Class to Another Class: Create two classes: Engine with horsepower, and Car
that accepts an Engine object. Display the engine's horsepower from the car.

You might also like