SlideShare a Scribd company logo
BCS-DS-427B: PYTHON
Ms. Swati Hans
Assistant Professor, CSE
Manav Rachna International Institute of Research and Studies, Faridabad
1
Classes & Objects
Python Lecture
Classes
❖ A class is a user-defined blueprint or prototype from which objects are created.
❖ Classes provide a means of bundling data and functionality together.
❖ Creating a new class creates a new type of object, allowing new instances of that type to be
made.
❖ Like function definitions begin with the def keyword in Python, class definitions begin with a
class keyword.
Class Definition Syntax:
class ClassName:
# variables
………..........
# functions
Example
class Rectangle:
def area(self): #function1
self.length=0 #variable1
self.breadth=0 #variable2
print("area is",self.length*self.breadth)
def perimeter(self): #function2
print("perimeter is ",2*(self.length+self.breadth))
r1=Rectangle()
r1.area()
r1.perimeter()
In this class Rectangle, we have two
Member variables, length & breadth
and two member functions area &
perimeter
Objects
❖ An object is an instance of
class
❖ When an object of a class is
created, class is said to be
instantiated.
❖ All the instances share the
attributes and the behavior
of the class.
❖ But the values of those
attributes, i.e. the state are
unique for each object.
❖ A single class may have any
number of instances.
Classes & its objects
Objects
Rectangle class
Variables included are:
length
breadth
Functions included are:
area( )
perimeter( )
Object1
length=31
breadth=43
Object2
length=35
breadth=56
Each object has its own instance variables
Considering rectangle class told earlier with its member variables length & breadth and
its functions area() & perimeter()
Accessing Attributes
Attributes of class can be accessed using Dot operator
rect1=Rectangle( ) #create objects
rect2=Rectangle( )
print(rect1.length, rect1.breadth) # access variables
rect1.area( ) # access functions
rect2.area( )
Self Parameter in Methods
In object-oriented programming, whenever we define methods for a class, we use self as the first
parameter in each case.
class car:
def setdata(self, model, color):
self.model = model
self.color = color
def show(self):
print("Model is", self.model)
print("color is", self.color)
audi = car()
volvo=car()
audi.setdata("audi a4", "blue")
Volvo.setdata("volvo 488", "silver")
audi.show() # same output as car.show(audi)
ferrari.show() # same output as car.show(ferrari)
__init__ method(Constructor )
Constructors are generally used for instantiating an object. The task of constructors is to initialize(assign values) to the data
members of the class when an object of class is created. In python constructors are defined by including init method in class
Syntax of constructor declaration :
Class A:
def __init__(self):
# body of the constructor
Types of constructors :
 default constructor
 parameterized constructor
Default Constructor
The default constructor is simple constructor which doesn’t accept any arguments. Constructor in
which no other argument included except self which is a reference to the instance being
constructed.
class Rectangle:
def __init__(self):
self.l=0 # in this default constructor, by default values are initialized with zero
self.b=0
def area(self): Output
print(self.l*self.b)
rec1=Rectangle( )
rec1.area( ) #area is displayed using default values
rect1.l=45
rect1.b=50
rec1.area( ) #area is displayed as per updated values
Parameterized constructor
Constructor with parameters is known as parameterized constructor. Constructor in
which other arguments are also included along with self which are provided by the
programmer
class Rectangle:
def __init__(self,len,bre):
self.l=len # in this constructor, member variables l,b are initialized with arguments
len,bre
self.b=bre
def area(self): Output
print(self.l*self.b)
rect1=Rectangle(3,4) #arguments are passes while creating object
Copy Constructor
This constructor copies values of one object to other object.
class Rectangle:
l=0
b=0
def __init__(self,obj1=None): #by default it will assign None to obj1,if object is not passed
if(obj1!=None):
self.l=obj1.l
self.b=obj1.b
def area(self):
print(self.l*self.b)
rec1=Rectangle() #here No object is passed in constructor
rec1.l=4
rec1.b=5
rec2=Rectangle(rec1) #rec1 object is passed, so copies the value in rec1 to rec2
rec1.area( )
rec2.area( )
Output

More Related Content

Similar to Classes and Objects _ (20)

PPTX
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
PPTX
oogshsvshsbhshhshvsvshsvsvhshshjshshhsvgps.pptx
BhojarajTheking
 
PPTX
Python_Unit_2 OOPS.pptx
ChhaviCoachingCenter
 
PPTX
constructors.pptx
Epsiba1
 
PDF
OOPs Interview Questions PDF By ScholarHat
Scholarhat
 
PPTX
OOPS.pptx
NitinSharma134320
 
PPTX
Object Oriented Programming in Python.pptx
grpvasundhara1993
 
PPT
Introduction to Python - Part Three
amiable_indian
 
PPSX
Object oriented programming 2
Aadil Ansari
 
PPTX
C# classes objects
Dr.Neeraj Kumar Pandey
 
PPTX
C++ Presen. tation.pptx
mohitsinha7739289047
 
PPT
Classes, objects and methods
farhan amjad
 
PPTX
Introduce oop in python
tuan vo
 
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
PPTX
An introduction to Constructors and destructors in c++ .pptx
olisahchristopher
 
PPTX
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
PDF
Learn C# Programming - Classes & Inheritance
Eng Teong Cheah
 
PPTX
basic concepts of object oriented programming
infotechsaasc
 
PPTX
Python - OOP Programming
Andrew Ferlitsch
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
oogshsvshsbhshhshvsvshsvsvhshshjshshhsvgps.pptx
BhojarajTheking
 
Python_Unit_2 OOPS.pptx
ChhaviCoachingCenter
 
constructors.pptx
Epsiba1
 
OOPs Interview Questions PDF By ScholarHat
Scholarhat
 
Object Oriented Programming in Python.pptx
grpvasundhara1993
 
Introduction to Python - Part Three
amiable_indian
 
Object oriented programming 2
Aadil Ansari
 
C# classes objects
Dr.Neeraj Kumar Pandey
 
C++ Presen. tation.pptx
mohitsinha7739289047
 
Classes, objects and methods
farhan amjad
 
Introduce oop in python
tuan vo
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
An introduction to Constructors and destructors in c++ .pptx
olisahchristopher
 
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
Learn C# Programming - Classes & Inheritance
Eng Teong Cheah
 
basic concepts of object oriented programming
infotechsaasc
 
Python - OOP Programming
Andrew Ferlitsch
 

More from swati463221 (12)

PPTX
python ..... _
swati463221
 
PPTX
python _
swati463221
 
PPTX
matplotlib _
swati463221
 
PPTX
image processing _
swati463221
 
PPTX
numpy2 _
swati463221
 
PPTX
Inheritance Super and MRO _
swati463221
 
PDF
Function overloading or Polymorphism.pdf
swati463221
 
PPTX
kruskal and prims algorithm _
swati463221
 
PPT
switching.ppt
swati463221
 
PPT
stop and wait
swati463221
 
PPTX
subnetting
swati463221
 
PPTX
FIREWALL
swati463221
 
python ..... _
swati463221
 
python _
swati463221
 
matplotlib _
swati463221
 
image processing _
swati463221
 
numpy2 _
swati463221
 
Inheritance Super and MRO _
swati463221
 
Function overloading or Polymorphism.pdf
swati463221
 
kruskal and prims algorithm _
swati463221
 
switching.ppt
swati463221
 
stop and wait
swati463221
 
subnetting
swati463221
 
FIREWALL
swati463221
 
Ad

Recently uploaded (20)

PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPT
Electrical Safety Presentation for Basics Learning
AliJaved79382
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PDF
smart lot access control system with eye
rasabzahra
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Electrical Safety Presentation for Basics Learning
AliJaved79382
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
Design Thinking basics for Engineers.pdf
CMR University
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
smart lot access control system with eye
rasabzahra
 
Ad

Classes and Objects _

  • 1. BCS-DS-427B: PYTHON Ms. Swati Hans Assistant Professor, CSE Manav Rachna International Institute of Research and Studies, Faridabad 1
  • 3. Classes ❖ A class is a user-defined blueprint or prototype from which objects are created. ❖ Classes provide a means of bundling data and functionality together. ❖ Creating a new class creates a new type of object, allowing new instances of that type to be made. ❖ Like function definitions begin with the def keyword in Python, class definitions begin with a class keyword. Class Definition Syntax: class ClassName: # variables ……….......... # functions
  • 4. Example class Rectangle: def area(self): #function1 self.length=0 #variable1 self.breadth=0 #variable2 print("area is",self.length*self.breadth) def perimeter(self): #function2 print("perimeter is ",2*(self.length+self.breadth)) r1=Rectangle() r1.area() r1.perimeter() In this class Rectangle, we have two Member variables, length & breadth and two member functions area & perimeter
  • 5. Objects ❖ An object is an instance of class ❖ When an object of a class is created, class is said to be instantiated. ❖ All the instances share the attributes and the behavior of the class. ❖ But the values of those attributes, i.e. the state are unique for each object. ❖ A single class may have any number of instances.
  • 6. Classes & its objects
  • 7. Objects Rectangle class Variables included are: length breadth Functions included are: area( ) perimeter( ) Object1 length=31 breadth=43 Object2 length=35 breadth=56 Each object has its own instance variables Considering rectangle class told earlier with its member variables length & breadth and its functions area() & perimeter()
  • 8. Accessing Attributes Attributes of class can be accessed using Dot operator rect1=Rectangle( ) #create objects rect2=Rectangle( ) print(rect1.length, rect1.breadth) # access variables rect1.area( ) # access functions rect2.area( )
  • 9. Self Parameter in Methods In object-oriented programming, whenever we define methods for a class, we use self as the first parameter in each case. class car: def setdata(self, model, color): self.model = model self.color = color def show(self): print("Model is", self.model) print("color is", self.color) audi = car() volvo=car() audi.setdata("audi a4", "blue") Volvo.setdata("volvo 488", "silver") audi.show() # same output as car.show(audi) ferrari.show() # same output as car.show(ferrari)
  • 10. __init__ method(Constructor ) Constructors are generally used for instantiating an object. The task of constructors is to initialize(assign values) to the data members of the class when an object of class is created. In python constructors are defined by including init method in class Syntax of constructor declaration : Class A: def __init__(self): # body of the constructor Types of constructors :  default constructor  parameterized constructor
  • 11. Default Constructor The default constructor is simple constructor which doesn’t accept any arguments. Constructor in which no other argument included except self which is a reference to the instance being constructed. class Rectangle: def __init__(self): self.l=0 # in this default constructor, by default values are initialized with zero self.b=0 def area(self): Output print(self.l*self.b) rec1=Rectangle( ) rec1.area( ) #area is displayed using default values rect1.l=45 rect1.b=50 rec1.area( ) #area is displayed as per updated values
  • 12. Parameterized constructor Constructor with parameters is known as parameterized constructor. Constructor in which other arguments are also included along with self which are provided by the programmer class Rectangle: def __init__(self,len,bre): self.l=len # in this constructor, member variables l,b are initialized with arguments len,bre self.b=bre def area(self): Output print(self.l*self.b) rect1=Rectangle(3,4) #arguments are passes while creating object
  • 13. Copy Constructor This constructor copies values of one object to other object. class Rectangle: l=0 b=0 def __init__(self,obj1=None): #by default it will assign None to obj1,if object is not passed if(obj1!=None): self.l=obj1.l self.b=obj1.b def area(self): print(self.l*self.b) rec1=Rectangle() #here No object is passed in constructor rec1.l=4 rec1.b=5 rec2=Rectangle(rec1) #rec1 object is passed, so copies the value in rec1 to rec2 rec1.area( ) rec2.area( ) Output