SlideShare a Scribd company logo
1 of 27
MODULE 5
 Chapter 15:
 Classes and
objects
 Chapter 16:
 Classes and
functions
What is a Class and Objects in Python?
Class: The class is a user-defined data structure that binds the data members and
methods into a single unit.
Class is a blueprint or code template for object creation. Using a class, you can
create as many objects as you want.
•Object: An object is an instance of a class. It is a collection of attributes
(variables) and methods. We use the object of a class to perform actions.
Objects have two characteristics: They have states and behaviors (object has
attributes and methods attached to it) Attributes represent its state, and methods
represent its behavior. Using its methods, we can modify its state.
An object is a real-life entity. It is the collection of various data and functions
that operate on those data.
Every object has the following property.
Identity: Every object must be uniquely identified.
State: An object has an attribute that represents a state of an object,
and it also reflects the property of an object.
Behavior: An object has methods that represent its behavior.
What are classes and objects in
Python?
• Python is an object oriented programming language.
• Class is a abstract data type which can be defined
as a template or blueprint that describes the
behavior / state that the object of its type support.
• An object is an instance of class . Objects have
states and behaviors. Example: A dog has states -
color, name, breed as well as behaviors – wagging
the tail, barking, eating
• Object is simply a collection of data (variables)
and methods (functions) that act on those
data.
• The process of creating this object is
called instantiation
Defining a Class in Python
• We define a class using the keyword
class.
• The first string is called docstring and has
a brief description about the class.
Although not mandatory, this is
recommended.
• A class attributes are data
members( variables) and
functions.
Syntax
The class statement creates a new class
definition.
The name of the class immediately follows
the keyword class followed by a colon as
follows −
classClassName:
“””Optional class documentation string' class_suite”””
class_suite
• The class has a documentation string, which can be accessed via
ClassName. doc .
• The class_suite consists of all the component statements
defining class members, data attributes and functions.
Example
The header indicates that the new class is called Point.
The body is a docstring that explains what the class is for. You can define
variables and
methods inside a class definition.
Creating an Object in Python
• Class can be used to create new
object
instances (instantiation) of that
class.
• The procedure to create an object is
similar to a function call.
• Example: blank = Point()
The return value is a reference to a Point object, which we
assign to blank.
Example 1 1e1
Example 2
• The variable empCount is a class variable whose
value is shared among all instances of a this
class. This can be accessed as
Employee.empCount from inside the class or
outside the class.
• The first method init () is a special method, which
is called class constructor or initialization
method that Python calls when you create a
new instance of this class.
• We can declare other class methods like normal
functions with the exception that the first
argument to each method is self.
• Python adds the self argument to the list for
you; you do not need to include it when you call
the methods.
Creating Instance Objects
• To create instances of a class, you call the
class using class name and pass in whatever
arguments itsinit method accepts
emp1 = Employee(“Raju",
2000) emp2 =
Employee(“Swamy", 5000)
Accessing Attributes
• We can access the object's attributes
using the dot operator with object.
• Class variable would be accessed using
class
name as follows −
emp1.displayEmploy
ee()
emp2.displayEmploy
ee()
print ("Total Employee %d" %
Employee.empCount)
Example2 : Rectangle
• Rectangle can be created using following
two possibilities:
– You could specify one corner of the
rectangle (or the center), the width, and the
height.
– You could specify two opposing corners.
Instances as return values
• Functions can return
instances.
• For example, find_center
takes a Rectangle as an
argument and returns a Point
that contains the coordinates
of the center of the
Rectangle:
Objects are mutable
• You can change the state of an object by
making an assignment to one of its
attributes. For example, to change the
size of a rectangle without changing its
position, you can modify the values of
width and height:
• box.width = box.width + 50
• box.height = box.height + 100
Modifying through functions
• You can also write functions that
modify objects.
• For example, grow_rectangle takes a
Rectangle object and two numbers,
dwidth and dheight, and adds the
numbers to the width and height of the
rectangle:
def grow_rectangle(rect, dwidth,
dheight): rect.width +=
dwidth rect.height +=
dheight
Copying
• Copying an object is often an alternative
to aliasing. The copy module contains a
function called copy that can duplicate
any object:
Exercise:
1. Write a definition for a class named Circle with attributes center and
radius, where center is a Point object and radius is a number.
2. Write a function called draw_rect that takes a Turtle object and a
Rectangle and uses the Turtle to draw the Rectangle

More Related Content

Similar to IPP-M5-C1-Classes _ Objects python -S2.pptx

Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
Getachew Ganfur
 

Similar to IPP-M5-C1-Classes _ Objects python -S2.pptx (20)

Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
 
Ch 2 Library Classes.pptx
Ch 2 Library Classes.pptxCh 2 Library Classes.pptx
Ch 2 Library Classes.pptx
 
Ch 2 Library Classes.pdf
Ch 2 Library Classes.pdfCh 2 Library Classes.pdf
Ch 2 Library Classes.pdf
 
OOSD1-unit1_1_16_09.pptx
OOSD1-unit1_1_16_09.pptxOOSD1-unit1_1_16_09.pptx
OOSD1-unit1_1_16_09.pptx
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Pythonclass
PythonclassPythonclass
Pythonclass
 
Oops
OopsOops
Oops
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
how to create object
how to create objecthow to create object
how to create object
 

Recently uploaded

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
rahulmanepalli02
 
Scouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring methodScouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring method
vimal412355
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
HenryBriggs2
 

Recently uploaded (20)

8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
History of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationHistory of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & Modernization
 
Overview of Transformation in Computer Graphics
Overview of Transformation in Computer GraphicsOverview of Transformation in Computer Graphics
Overview of Transformation in Computer Graphics
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
 
Call for Papers - Journal of Electrical Systems (JES), E-ISSN: 1112-5209, ind...
Call for Papers - Journal of Electrical Systems (JES), E-ISSN: 1112-5209, ind...Call for Papers - Journal of Electrical Systems (JES), E-ISSN: 1112-5209, ind...
Call for Papers - Journal of Electrical Systems (JES), E-ISSN: 1112-5209, ind...
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
DFT - Discrete Fourier Transform and its Properties
DFT - Discrete Fourier Transform and its PropertiesDFT - Discrete Fourier Transform and its Properties
DFT - Discrete Fourier Transform and its Properties
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Scouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring methodScouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring method
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Fundamentals of Structure in C Programming
Fundamentals of Structure in C ProgrammingFundamentals of Structure in C Programming
Fundamentals of Structure in C Programming
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
Dr Mrs A A Miraje C Programming PPT.pptx
Dr Mrs A A Miraje C Programming PPT.pptxDr Mrs A A Miraje C Programming PPT.pptx
Dr Mrs A A Miraje C Programming PPT.pptx
 
Presentation on Slab, Beam, Column, and Foundation/Footing
Presentation on Slab,  Beam, Column, and Foundation/FootingPresentation on Slab,  Beam, Column, and Foundation/Footing
Presentation on Slab, Beam, Column, and Foundation/Footing
 

IPP-M5-C1-Classes _ Objects python -S2.pptx

  • 1. MODULE 5  Chapter 15:  Classes and objects  Chapter 16:  Classes and functions
  • 2. What is a Class and Objects in Python? Class: The class is a user-defined data structure that binds the data members and methods into a single unit. Class is a blueprint or code template for object creation. Using a class, you can create as many objects as you want. •Object: An object is an instance of a class. It is a collection of attributes (variables) and methods. We use the object of a class to perform actions. Objects have two characteristics: They have states and behaviors (object has attributes and methods attached to it) Attributes represent its state, and methods represent its behavior. Using its methods, we can modify its state. An object is a real-life entity. It is the collection of various data and functions that operate on those data.
  • 3. Every object has the following property. Identity: Every object must be uniquely identified. State: An object has an attribute that represents a state of an object, and it also reflects the property of an object. Behavior: An object has methods that represent its behavior.
  • 4.
  • 5.
  • 6. What are classes and objects in Python? • Python is an object oriented programming language. • Class is a abstract data type which can be defined as a template or blueprint that describes the behavior / state that the object of its type support. • An object is an instance of class . Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating • Object is simply a collection of data (variables) and methods (functions) that act on those data. • The process of creating this object is called instantiation
  • 7. Defining a Class in Python • We define a class using the keyword class. • The first string is called docstring and has a brief description about the class. Although not mandatory, this is recommended. • A class attributes are data members( variables) and functions.
  • 8. Syntax The class statement creates a new class definition. The name of the class immediately follows the keyword class followed by a colon as follows − classClassName: “””Optional class documentation string' class_suite””” class_suite • The class has a documentation string, which can be accessed via ClassName. doc . • The class_suite consists of all the component statements defining class members, data attributes and functions.
  • 9. Example The header indicates that the new class is called Point. The body is a docstring that explains what the class is for. You can define variables and methods inside a class definition.
  • 10. Creating an Object in Python • Class can be used to create new object instances (instantiation) of that class. • The procedure to create an object is similar to a function call. • Example: blank = Point() The return value is a reference to a Point object, which we assign to blank.
  • 13. • The variable empCount is a class variable whose value is shared among all instances of a this class. This can be accessed as Employee.empCount from inside the class or outside the class. • The first method init () is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class. • We can declare other class methods like normal functions with the exception that the first argument to each method is self. • Python adds the self argument to the list for you; you do not need to include it when you call the methods.
  • 14. Creating Instance Objects • To create instances of a class, you call the class using class name and pass in whatever arguments itsinit method accepts emp1 = Employee(“Raju", 2000) emp2 = Employee(“Swamy", 5000)
  • 15. Accessing Attributes • We can access the object's attributes using the dot operator with object. • Class variable would be accessed using class name as follows − emp1.displayEmploy ee() emp2.displayEmploy ee() print ("Total Employee %d" % Employee.empCount)
  • 16.
  • 17.
  • 18. Example2 : Rectangle • Rectangle can be created using following two possibilities: – You could specify one corner of the rectangle (or the center), the width, and the height. – You could specify two opposing corners.
  • 19.
  • 20.
  • 21. Instances as return values • Functions can return instances. • For example, find_center takes a Rectangle as an argument and returns a Point that contains the coordinates of the center of the Rectangle:
  • 22.
  • 23. Objects are mutable • You can change the state of an object by making an assignment to one of its attributes. For example, to change the size of a rectangle without changing its position, you can modify the values of width and height: • box.width = box.width + 50 • box.height = box.height + 100
  • 24. Modifying through functions • You can also write functions that modify objects. • For example, grow_rectangle takes a Rectangle object and two numbers, dwidth and dheight, and adds the numbers to the width and height of the rectangle: def grow_rectangle(rect, dwidth, dheight): rect.width += dwidth rect.height += dheight
  • 25. Copying • Copying an object is often an alternative to aliasing. The copy module contains a function called copy that can duplicate any object:
  • 26.
  • 27. Exercise: 1. Write a definition for a class named Circle with attributes center and radius, where center is a Point object and radius is a number. 2. Write a function called draw_rect that takes a Turtle object and a Rectangle and uses the Turtle to draw the Rectangle