SlideShare a Scribd company logo
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
you must specify self explicitly when defining the method, you don’t
include it when calling the method
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Classes are defined using keyword “Class”
followed by user defined “ClassName” and a
colon.
Variables are also called attributes
Functions are also called methods.
Syntax: class className:
<statement-1>
<statement-2>
;
<statement-n>
 class Mobile:
 def __init__(self, name):
 self.mobile_name=name
 print("Constructor")
 def receive_msg(self):
 print(f"Receive Message from {self.mobile_name}")
 def send_msg(self):
 print(f"Send Message from {self.mobile_name}")
 def main():
 nokia = Mobile("Nokia")
 nokia.receive_msg()
 nokia.send_msg()
 if __name__=="__main__":
 main()
 Output:
 Constructor
 Receive Message from Nokia
 Send Message from Nokia
•Class Mobile has 2 methods:
-Receive_msg()
- Send_msg()
•Self has no meaning in Python.
•It is used to improve readability.
•__init__ function is called when an
object is instantiated
 Object refers to a particular instance of a class.
 It contains variables and methods defined in the class.
 Class objects support 2 kinds of operations.
◦ A. Attribute references – The names in class are referenced by
objects and are called attribute references. There are two
kinds of attribute references.
 A. Data attributes – Variables defined within methods are called
instance variables / data attributes which are used to store data.
 B. Method attributes – Functions that are inside a class and are
referenced by objects of a class.
B. Instantiation – Act of creating an object from a class.
 class student:
 def __init__(self,name,marks):
 self.name = name
 self.marks = marks
 def check_pass_fail(self):
 if self.marks >= 40:
 return True
 else:
 return False
 def main():
 student1 = student("Harry", 30)
 student2 = student("Jane", 99)
 did_pass = student1.check_pass_fail()
 print(did_pass)
 did_pass = student2.check_pass_fail()
 print(did_pass)
 if __name__=="__main__":
 main()
•Student1 and Student2 are the objects of
Student Class.
•Student1 is instantiated with name as Harry
and marks 30
•Student2 is instantiated with name as Jane
and marks 99
•Whenever you call a method using an object,
the object is automatically passed in as the
first parameter to the self parameter variable
Output
False
True
 class Birds:
 def __init__(self, bird_name):
 self.bird_name = bird_name
 def flying_birds(self):
 print(f"{self.bird_name} flies above clouds")
 def non_flying_birds(self):
 print(f"{self.bird_name} is the national bird of Australia")
 def main():
 vulture=Birds("Griffon Vulture")
 Crane = Birds("Common Crane")
 Emu = Birds("Emu")
 vulture.flying_birds()
 Crane.flying_birds()
 Emu.non_flying_birds()
 if __name__=="__main__":
 main()
Output:
Griffon Vulture flies above clouds
Common Crane flies above clouds
Emu is the national bird of Australia
vulture, Crane and Emu are the objects
of class Birds
 Only one constructor can be defined per class
 Syntax:
 def __init__(self,parameter_1,….parameter_n)
 It defines and initializes the instance variables
 It is called as soon as an object of a class is
instantiated.
 All the data attributes of a class are initialised
in the init function itself.
 An object can be passed as an argument to a calling
function
 class Music:
 def __init__(self,song,artist):
 self.song=song
 self.artist=artist
 def print_track_info(vocalist):
 print(f"Song is {vocalist.song}")
 print(f"Artist is {vocalist.artist}")
 singer = Music("Boy With Love","BTS")
 print_track_info(singer)
Output:
Song is Boy With Love
Artist is BTS2
 class sum:
 def __init__(self,num1,num2):
 self.num1 = num1
 self.num2 = num2
 self.sum =" "
 def add_sum(self):
 self.sum = self.num1 + self.num2
 return self
 def main():
 number =sum(4,5)
 returned_object = number.add_sum()
 print(returned_object.sum)
 print(id(returned_object))
 print(isinstance(returned_object,sum))
 if __name__=="__main__":
 main()
Output:
9
1787802679712
True
•id(object) is a function that is used to find
the identity of the location of the object in
memory
•Isinstance(object,classinfo) is a function
that returns a boolean stating if the object
is an instance of class or not.
 class Dog:
 kind ='Canine'
 def __init__(self,name):
 self.dog_name=name
 d=Dog('Fido')
 e=Dog("Buddy")
 print(f"{d.kind}")
 print(f"{e.kind}")
 print(f"{d.dog_name}")
 print(f"{e.dog_name}")
Output:
Canine
Canine
Fido
Buddy
Class Attributes:
Are Class variables that is
shared by all objects of a
class.
Data Attributes
Are instance variables
unique to each object of a
class.
 Encapsulation -> Information hiding
 Abstraction -> Implementation hiding
 It is the process of combining variables that store data and
methods that work on those variables into a single unit called
class.
 class foo:
 def __init__(self,a,b):
 self.a = a
 self.b = b
 def add(self):
 return self.a + self.b
 foo_object =foo(3,4)
 print(foo_object.add())
Output:
7
The internal representation of the foo
class is hidden outside the class ->
Encapsulation
The implementation of add() function
is hidden from the object. ->
Abstraction
 class Demo:
 def __init__(self):
 self.nonprivate="I am not a private instance"
 self.__private="I am a private instance"
 def display_privateinstance(self):
 print(f"{self.__private} used within the method of a class")
 def main():
 demo_obj = Demo()
 demo_obj.display_privateinstance()
 print(demo_obj.nonprivate)
 # print(demo_obj.__private)
 if __name__=="__main__":
 main()
Output:
I am a private instance used within
the method of a class
I am not a private instance
 The private instance variables cannot be accessed outside the
class, but only through a method defined inside the class
 In Python, an identifier with double underscore is treated as
private.
 Name mangling is intended to give the class an easy way to
define private instance variables and methods
 class Student:
 def __init__(self, name):
 self.__name = name // private attribute

 s1 = Student("Santa")
 print(s1._Student__name)
 //Access using _class__private attribute
 The class that is used as the basis for inheritance is called
a superclass or base class.
 A class that inherits from a base class is called a subclass
or derived class.
 The base class and derived class exhibit “is a” relationship
in inheritance
 Eg: Sitar is a stringed instrument
 Syntax of derived class:
 Class DerivedClassName(BaseClassName):
 <statement-1>
 .
 .
 <statement-N>
 It is possible to define the derived base when the base class is defined in another module.
 Class DerivedClassName(modname.BaseClassName)
 # module1.py
 class Hello:
 def show(self):
 print("Module1.Hello.Show Welcome")
 #modulederived.py
 ____________________________________________________________________________________________________
import module1
 class derived_module1(module1.Hello): # derived class of the Hello class in Module 1
 def derived_class(self):
 print("Hi")
 def main():
 check = derived_module1() # object of the derived_module1 class
 check.show()
 if __name__=="__main__":
 main()
Output:
Module1.Hello.Show Welcome
 class someclass(object): # this is derived out of the "class object"
 def __init__(self):
 print("Constructor")
 def someclass_function(self):
 print("Hello India")
 def main():
 obj = someclass()
 obj.someclass_function()
 if __name__=="__main__":
 main()
•All class except “Class object” are
derived classes.
•Class object is the base of
inheritance hierarchy and hence
has no derived classes.
•Classes without baseclassname are
derived from the class object.
 class cricket:
 def __init__(self,IPLteam,owner,times_won):
 self.IPLteam = IPLteam
 self.owner = owner
 self.times_won = times_won
 def IPLOwner(self):
 print(f"The IPL team {self.IPLteam} is owned by {self.owner}")
 class derived_cricket(cricket):
 def IPLresults(self):
 print(f"The IPL team {self.IPLteam} has won {self.times_won}")
 def main():
 cricket_fans=derived_cricket("KKR","SRK", 9)
 cricket_fans.IPLOwner()
 cricket_fans.IPLresults()
 if __name__=="__main__":
 main()
Output:
The IPL team KKR is
owned by SRK
The IPL team KKR has
won 9
 Derived_cricket is the derived class and cricket is the base
class
 Derived class inherits variables and methods of base class
 __init__() method is also derived from base class. Derived
class has access of __init__() method of the base class.
 The base class has 3 data attributes, IPLteam, owner and
times_won. It has a method IPLOwner
 Derived class has access to the data attributes and methods
of the base class.
 In Single Inheritance, built-in super() function is used to refer
to base class without explicitly naming it.
 If derived class has __init__() method and needs to access the
base class __init__() method explicitly, then this is done using
super().
 If the derived class needs no attributes from base class, then
we do not need to use super() method to invoke base class
__init__() method.
 Super().__init__(base_class_parameters)
 Its usage is as below.
 Class DerivedClass(BaseClass):
 def__init__(self, derived_class_params, base_class_params)
 super().__init__(base_class_params)
 self.derived_class_params = derived_class_params
 The derived class __init__() method has its own parameters
plus the base class parameters.
 We do not need to specify self for base class init() method
 Base class methods may be overridden (method overriding)
 Derived class should have a method with same name as those
in base class. Also signature (method name, order and total
number of parameters) should be same.
 class country:
 def __init__(self,country_name):
 self.country_name = country_name
 def country_details(self):
 print(f"Happiest country in world is {self.country_name}")
 class HappyCountry(country):
 def __init__(self,country_name,continent):
 super().__init__(country_name)
 self.continent = continent
 def Happy_Country_Details(self):
 print(f"Happiest country in the world is {self.country_name} and it is in {self.continent}")
 def main():
 obj = HappyCountry("Finland","Europe")
 obj.Happy_Country_Details()
 if __name__=="__main__":
 main()
Output:
Happiest country in the world
is Finland and it is in Europe
Ad

Recommended

Object_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdf
Koteswari Kasireddy
 
Unit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
 
Python advance
Python advance
Mukul Kirti Verma
 
Python unit 3 m.sc cs
Python unit 3 m.sc cs
KALAISELVI P
 
Object Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Python Lecture 13
Python Lecture 13
Inzamam Baig
 
Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
Python3
Python3
Ruchika Sinha
 
Python programming computer science and engineering
Python programming computer science and engineering
IRAH34
 
Class, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
Lecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.ppt
Reji K Dhaman
 
Lecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
 
Unit - 3.pptx
Unit - 3.pptx
Kongunadu College of Engineering and Technology
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
SudhanshiBakre1
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
SudhanshiBakre1
 
Python session 7 by Shan
Python session 7 by Shan
Navaneethan Naveen
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
OOPS.pptx
OOPS.pptx
NitinSharma134320
 
Introduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
python note.pdf
python note.pdf
Nagendra504676
 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
thuy27042005thieu
 
Module-5-Classes and Objects for Python Programming.pptx
Module-5-Classes and Objects for Python Programming.pptx
YogeshKumarKJMIT
 
Python_Unit_2 OOPS.pptx
Python_Unit_2 OOPS.pptx
ChhaviCoachingCenter
 
what is class in C++ and classes_objects.ppt
what is class in C++ and classes_objects.ppt
malikliyaqathusain
 
Inheritance
Inheritance
JayanthiNeelampalli
 
DA Syllabus outline (2).pptx
DA Syllabus outline (2).pptx
Koteswari Kasireddy
 
Chapter-7-Sampling & sampling Distributions.pdf
Chapter-7-Sampling & sampling Distributions.pdf
Koteswari Kasireddy
 

More Related Content

Similar to Python_Object_Oriented_Programming.pptx (20)

Python programming computer science and engineering
Python programming computer science and engineering
IRAH34
 
Class, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
Lecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.ppt
Reji K Dhaman
 
Lecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
 
Unit - 3.pptx
Unit - 3.pptx
Kongunadu College of Engineering and Technology
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
SudhanshiBakre1
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
SudhanshiBakre1
 
Python session 7 by Shan
Python session 7 by Shan
Navaneethan Naveen
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
OOPS.pptx
OOPS.pptx
NitinSharma134320
 
Introduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
python note.pdf
python note.pdf
Nagendra504676
 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
thuy27042005thieu
 
Module-5-Classes and Objects for Python Programming.pptx
Module-5-Classes and Objects for Python Programming.pptx
YogeshKumarKJMIT
 
Python_Unit_2 OOPS.pptx
Python_Unit_2 OOPS.pptx
ChhaviCoachingCenter
 
what is class in C++ and classes_objects.ppt
what is class in C++ and classes_objects.ppt
malikliyaqathusain
 
Inheritance
Inheritance
JayanthiNeelampalli
 
Python programming computer science and engineering
Python programming computer science and engineering
IRAH34
 
Class, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
Lecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.ppt
Reji K Dhaman
 
Lecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
SudhanshiBakre1
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
SudhanshiBakre1
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
Introduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
thuy27042005thieu
 
Module-5-Classes and Objects for Python Programming.pptx
Module-5-Classes and Objects for Python Programming.pptx
YogeshKumarKJMIT
 
what is class in C++ and classes_objects.ppt
what is class in C++ and classes_objects.ppt
malikliyaqathusain
 

More from Koteswari Kasireddy (20)

DA Syllabus outline (2).pptx
DA Syllabus outline (2).pptx
Koteswari Kasireddy
 
Chapter-7-Sampling & sampling Distributions.pdf
Chapter-7-Sampling & sampling Distributions.pdf
Koteswari Kasireddy
 
unit-3_Chapter1_RDRA.pdf
unit-3_Chapter1_RDRA.pdf
Koteswari Kasireddy
 
DBMS_UNIT_1.pdf
DBMS_UNIT_1.pdf
Koteswari Kasireddy
 
business analytics
business analytics
Koteswari Kasireddy
 
Relational Model and Relational Algebra.pptx
Relational Model and Relational Algebra.pptx
Koteswari Kasireddy
 
CHAPTER -12 it.pptx
CHAPTER -12 it.pptx
Koteswari Kasireddy
 
WEB_DATABASE_chapter_4.pptx
WEB_DATABASE_chapter_4.pptx
Koteswari Kasireddy
 
Unit 4 chapter - 8 Transaction processing Concepts (1).pptx
Unit 4 chapter - 8 Transaction processing Concepts (1).pptx
Koteswari Kasireddy
 
Database System Concepts AND architecture [Autosaved].pptx
Database System Concepts AND architecture [Autosaved].pptx
Koteswari Kasireddy
 
Evolution Of WEB_students.pptx
Evolution Of WEB_students.pptx
Koteswari Kasireddy
 
Presentation1.pptx
Presentation1.pptx
Koteswari Kasireddy
 
Algorithm.pptx
Algorithm.pptx
Koteswari Kasireddy
 
Control_Statements_in_Python.pptx
Control_Statements_in_Python.pptx
Koteswari Kasireddy
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
Koteswari Kasireddy
 
parts_of_python_programming_language.pptx
parts_of_python_programming_language.pptx
Koteswari Kasireddy
 
linked_list.pptx
linked_list.pptx
Koteswari Kasireddy
 
matrices_and_loops.pptx
matrices_and_loops.pptx
Koteswari Kasireddy
 
algorithms_in_linkedlist.pptx
algorithms_in_linkedlist.pptx
Koteswari Kasireddy
 
Control_Statements.pptx
Control_Statements.pptx
Koteswari Kasireddy
 
Chapter-7-Sampling & sampling Distributions.pdf
Chapter-7-Sampling & sampling Distributions.pdf
Koteswari Kasireddy
 
Relational Model and Relational Algebra.pptx
Relational Model and Relational Algebra.pptx
Koteswari Kasireddy
 
Unit 4 chapter - 8 Transaction processing Concepts (1).pptx
Unit 4 chapter - 8 Transaction processing Concepts (1).pptx
Koteswari Kasireddy
 
Database System Concepts AND architecture [Autosaved].pptx
Database System Concepts AND architecture [Autosaved].pptx
Koteswari Kasireddy
 
Control_Statements_in_Python.pptx
Control_Statements_in_Python.pptx
Koteswari Kasireddy
 
parts_of_python_programming_language.pptx
parts_of_python_programming_language.pptx
Koteswari Kasireddy
 
Ad

Recently uploaded (20)

Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
How to use search fetch method in Odoo 18
How to use search fetch method in Odoo 18
Celine George
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
Intellectual Property Right (Jurisprudence).pptx
Intellectual Property Right (Jurisprudence).pptx
Vishal Chanalia
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
jutaydeonne
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
How to Customize Quotation Layouts in Odoo 18
How to Customize Quotation Layouts in Odoo 18
Celine George
 
Peer Teaching Observations During School Internship
Peer Teaching Observations During School Internship
AjayaMohanty7
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
How to use search fetch method in Odoo 18
How to use search fetch method in Odoo 18
Celine George
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
Intellectual Property Right (Jurisprudence).pptx
Intellectual Property Right (Jurisprudence).pptx
Vishal Chanalia
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
jutaydeonne
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
How to Customize Quotation Layouts in Odoo 18
How to Customize Quotation Layouts in Odoo 18
Celine George
 
Peer Teaching Observations During School Internship
Peer Teaching Observations During School Internship
AjayaMohanty7
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Ad

Python_Object_Oriented_Programming.pptx

  • 11. you must specify self explicitly when defining the method, you don’t include it when calling the method
  • 14. Classes are defined using keyword “Class” followed by user defined “ClassName” and a colon. Variables are also called attributes Functions are also called methods. Syntax: class className: <statement-1> <statement-2> ; <statement-n>
  • 15.  class Mobile:  def __init__(self, name):  self.mobile_name=name  print("Constructor")  def receive_msg(self):  print(f"Receive Message from {self.mobile_name}")  def send_msg(self):  print(f"Send Message from {self.mobile_name}")  def main():  nokia = Mobile("Nokia")  nokia.receive_msg()  nokia.send_msg()  if __name__=="__main__":  main()  Output:  Constructor  Receive Message from Nokia  Send Message from Nokia •Class Mobile has 2 methods: -Receive_msg() - Send_msg() •Self has no meaning in Python. •It is used to improve readability. •__init__ function is called when an object is instantiated
  • 16.  Object refers to a particular instance of a class.  It contains variables and methods defined in the class.  Class objects support 2 kinds of operations. ◦ A. Attribute references – The names in class are referenced by objects and are called attribute references. There are two kinds of attribute references.  A. Data attributes – Variables defined within methods are called instance variables / data attributes which are used to store data.  B. Method attributes – Functions that are inside a class and are referenced by objects of a class. B. Instantiation – Act of creating an object from a class.
  • 17.  class student:  def __init__(self,name,marks):  self.name = name  self.marks = marks  def check_pass_fail(self):  if self.marks >= 40:  return True  else:  return False  def main():  student1 = student("Harry", 30)  student2 = student("Jane", 99)  did_pass = student1.check_pass_fail()  print(did_pass)  did_pass = student2.check_pass_fail()  print(did_pass)  if __name__=="__main__":  main() •Student1 and Student2 are the objects of Student Class. •Student1 is instantiated with name as Harry and marks 30 •Student2 is instantiated with name as Jane and marks 99 •Whenever you call a method using an object, the object is automatically passed in as the first parameter to the self parameter variable Output False True
  • 18.  class Birds:  def __init__(self, bird_name):  self.bird_name = bird_name  def flying_birds(self):  print(f"{self.bird_name} flies above clouds")  def non_flying_birds(self):  print(f"{self.bird_name} is the national bird of Australia")  def main():  vulture=Birds("Griffon Vulture")  Crane = Birds("Common Crane")  Emu = Birds("Emu")  vulture.flying_birds()  Crane.flying_birds()  Emu.non_flying_birds()  if __name__=="__main__":  main() Output: Griffon Vulture flies above clouds Common Crane flies above clouds Emu is the national bird of Australia vulture, Crane and Emu are the objects of class Birds
  • 19.  Only one constructor can be defined per class  Syntax:  def __init__(self,parameter_1,….parameter_n)  It defines and initializes the instance variables  It is called as soon as an object of a class is instantiated.  All the data attributes of a class are initialised in the init function itself.
  • 20.  An object can be passed as an argument to a calling function  class Music:  def __init__(self,song,artist):  self.song=song  self.artist=artist  def print_track_info(vocalist):  print(f"Song is {vocalist.song}")  print(f"Artist is {vocalist.artist}")  singer = Music("Boy With Love","BTS")  print_track_info(singer) Output: Song is Boy With Love Artist is BTS2
  • 21.  class sum:  def __init__(self,num1,num2):  self.num1 = num1  self.num2 = num2  self.sum =" "  def add_sum(self):  self.sum = self.num1 + self.num2  return self  def main():  number =sum(4,5)  returned_object = number.add_sum()  print(returned_object.sum)  print(id(returned_object))  print(isinstance(returned_object,sum))  if __name__=="__main__":  main() Output: 9 1787802679712 True •id(object) is a function that is used to find the identity of the location of the object in memory •Isinstance(object,classinfo) is a function that returns a boolean stating if the object is an instance of class or not.
  • 22.  class Dog:  kind ='Canine'  def __init__(self,name):  self.dog_name=name  d=Dog('Fido')  e=Dog("Buddy")  print(f"{d.kind}")  print(f"{e.kind}")  print(f"{d.dog_name}")  print(f"{e.dog_name}") Output: Canine Canine Fido Buddy Class Attributes: Are Class variables that is shared by all objects of a class. Data Attributes Are instance variables unique to each object of a class.
  • 23.  Encapsulation -> Information hiding  Abstraction -> Implementation hiding  It is the process of combining variables that store data and methods that work on those variables into a single unit called class.  class foo:  def __init__(self,a,b):  self.a = a  self.b = b  def add(self):  return self.a + self.b  foo_object =foo(3,4)  print(foo_object.add()) Output: 7 The internal representation of the foo class is hidden outside the class -> Encapsulation The implementation of add() function is hidden from the object. -> Abstraction
  • 24.  class Demo:  def __init__(self):  self.nonprivate="I am not a private instance"  self.__private="I am a private instance"  def display_privateinstance(self):  print(f"{self.__private} used within the method of a class")  def main():  demo_obj = Demo()  demo_obj.display_privateinstance()  print(demo_obj.nonprivate)  # print(demo_obj.__private)  if __name__=="__main__":  main() Output: I am a private instance used within the method of a class I am not a private instance
  • 25.  The private instance variables cannot be accessed outside the class, but only through a method defined inside the class  In Python, an identifier with double underscore is treated as private.  Name mangling is intended to give the class an easy way to define private instance variables and methods  class Student:  def __init__(self, name):  self.__name = name // private attribute   s1 = Student("Santa")  print(s1._Student__name)  //Access using _class__private attribute
  • 26.  The class that is used as the basis for inheritance is called a superclass or base class.  A class that inherits from a base class is called a subclass or derived class.  The base class and derived class exhibit “is a” relationship in inheritance  Eg: Sitar is a stringed instrument  Syntax of derived class:  Class DerivedClassName(BaseClassName):  <statement-1>  .  .  <statement-N>
  • 27.  It is possible to define the derived base when the base class is defined in another module.  Class DerivedClassName(modname.BaseClassName)  # module1.py  class Hello:  def show(self):  print("Module1.Hello.Show Welcome")  #modulederived.py  ____________________________________________________________________________________________________ import module1  class derived_module1(module1.Hello): # derived class of the Hello class in Module 1  def derived_class(self):  print("Hi")  def main():  check = derived_module1() # object of the derived_module1 class  check.show()  if __name__=="__main__":  main() Output: Module1.Hello.Show Welcome
  • 28.  class someclass(object): # this is derived out of the "class object"  def __init__(self):  print("Constructor")  def someclass_function(self):  print("Hello India")  def main():  obj = someclass()  obj.someclass_function()  if __name__=="__main__":  main() •All class except “Class object” are derived classes. •Class object is the base of inheritance hierarchy and hence has no derived classes. •Classes without baseclassname are derived from the class object.
  • 29.  class cricket:  def __init__(self,IPLteam,owner,times_won):  self.IPLteam = IPLteam  self.owner = owner  self.times_won = times_won  def IPLOwner(self):  print(f"The IPL team {self.IPLteam} is owned by {self.owner}")  class derived_cricket(cricket):  def IPLresults(self):  print(f"The IPL team {self.IPLteam} has won {self.times_won}")  def main():  cricket_fans=derived_cricket("KKR","SRK", 9)  cricket_fans.IPLOwner()  cricket_fans.IPLresults()  if __name__=="__main__":  main() Output: The IPL team KKR is owned by SRK The IPL team KKR has won 9
  • 30.  Derived_cricket is the derived class and cricket is the base class  Derived class inherits variables and methods of base class  __init__() method is also derived from base class. Derived class has access of __init__() method of the base class.  The base class has 3 data attributes, IPLteam, owner and times_won. It has a method IPLOwner  Derived class has access to the data attributes and methods of the base class.
  • 31.  In Single Inheritance, built-in super() function is used to refer to base class without explicitly naming it.  If derived class has __init__() method and needs to access the base class __init__() method explicitly, then this is done using super().  If the derived class needs no attributes from base class, then we do not need to use super() method to invoke base class __init__() method.  Super().__init__(base_class_parameters)
  • 32.  Its usage is as below.  Class DerivedClass(BaseClass):  def__init__(self, derived_class_params, base_class_params)  super().__init__(base_class_params)  self.derived_class_params = derived_class_params  The derived class __init__() method has its own parameters plus the base class parameters.  We do not need to specify self for base class init() method  Base class methods may be overridden (method overriding)  Derived class should have a method with same name as those in base class. Also signature (method name, order and total number of parameters) should be same.
  • 33.  class country:  def __init__(self,country_name):  self.country_name = country_name  def country_details(self):  print(f"Happiest country in world is {self.country_name}")  class HappyCountry(country):  def __init__(self,country_name,continent):  super().__init__(country_name)  self.continent = continent  def Happy_Country_Details(self):  print(f"Happiest country in the world is {self.country_name} and it is in {self.continent}")  def main():  obj = HappyCountry("Finland","Europe")  obj.Happy_Country_Details()  if __name__=="__main__":  main() Output: Happiest country in the world is Finland and it is in Europe