SlideShare a Scribd company logo
3
Most read
4
Most read
5
Most read
OOPs CONCEPT IN PYTHON
SANTOSH VERMA
Faculty Development Program: Python Programming
JK Lakshmipat University, Jaipur
(3-7, June 2019)
Important Note
 Please remember that Python completely works on
indentation.
 Use the tab key to provide indentation to your code.
Classes and Objects
Classes
Just like every other Object Oriented Programming language
Python supports classes. Let’s look at some points on Python
classes. Classes are created by keyword class.
 Attributes are the variables that belong to class.
 Attributes are always public and can be accessed using dot (.)
operator. Eg.: Myclass.Myattribute
# A simple example class
class Test:
# A sample method
def fun(self):
print("Hello")
# Driver code
obj = Test()
obj.fun()
Output: Hello
The self
 Class methods must have an extra first parameter in method definition.
do not give a value for this parameter when we call the method, Python
provides it.
 If we have a method which takes no arguments, then we still have to
one argument – the self. See fun() in above simple example.
 This is similar to this pointer in C++ and this reference in Java.
 When we call a method of this object as myobject.method(arg1, arg2),
is automatically converted by Python into MyClass.method(myobject,
arg2) – this is all the special self is about.
# creates a class named MyClass
class MyClass:
# assign the values to the MyClass attributes
number = 0
name = "noname"
def Main():
# Creating an object of the MyClass.Here, 'me' is the object
me = MyClass()
# Accessing the attributes of MyClass, using the dot(.) operator
me.number = 310
me.name = "Santosh"
# str is an build-in function that, creates an string
print(me.name + " " + str(me.number))
# telling python that there is main in the program.
if __name__=='__main__':
Main()
Output: Santosh 310
 The __init__ method
The __init__ method is similar to constructors in C++ and Java. It is run as
soon as an object of a class is instantiated. The method is useful to do any
initialization you want to do with your object.
class Person:
# init method or constructor
def __init__(self, name):
self.name = name
# Sample Method
def say_hi(self):
print('Hello, my name is', self.name)
p = Person(‘Santosh')
p.say_hi()
Methods
 Method is a bunch of code that is intended to perform a
particular task in your Python’s code.
 Function that belongs to a class is called an Method.
 All methods require ‘self’ first parameter. If you have coded in
other OOP language you can think of ‘self’ as the ‘this’ keyword
which is used for the current object. It unhides the current
instance variable. ’self’ mostly work like ‘this’.
 ‘def’ keyword is used to create a new method.
class Vector2D:
x = 0.0
y = 0.0
# Creating a method named Set
def Set(self, x, y):
self.x = x
self.y = y
def Main():
# vec is an object of class Vector2D
vec = Vector2D()
# Passing values to the function Set
# by using dot(.) operator.
vec.Set(5, 6)
print("X: " + vec.x + ", Y: " + vec.y)
if __name__=='__main__':
Main()
Output: X: 5, Y: 6
Python In-built class functions
Inheritance
 Inheritance is the capability of one class to derive or inherit the properties
from some another class. The benefits of inheritance are:
 It represents real-world relationships well.
 It provides reusability of a code. We don’t have to write the same code again
and again. Also, it allows us to add more features to a class without modifying
it.
 It is transitive in nature, which means that if class B inherits from another class
A, then all the subclasses of B would automatically inherit from class A.
 Inheritance is defined as a way in which a particular class inherits
features from its base class.
 Base class is also knows as ‘Superclass’ and the class which inherits from
the Superclass is knows as ‘Subclass’
 # Syntax for inheritance
 class derived-classname(superclass-name)
class Person(object): # class Person: of Python 3.x is same as defined here.
def __init__(self, name): # Constructor
self.name = name
def getName(self): # To get name
return self.name
def isEmployee(self): # To check if this person is employee
return False
# Inherited or Sub class (Note Person in bracket)
class Employee(Person):
def isEmployee(self): # Here we return true
return True
# Driver code
emp = Person(“Ram") # An Object of Person
print(emp.getName(), emp.isEmployee())
emp = Employee(“Shyam") # An Object of Employee
print(emp.getName(), emp.isEmployee())
OUTPUT:
Ram False
Shyam True
Explore more on Inheritance Type
Thank You!

More Related Content

What's hot (20)

PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
PPTX
Intro to Python Programming Language
Dipankar Achinta
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PPTX
Python dictionary
Mohammed Sikander
 
PDF
Object oriented approach in python programming
Srinivas Narasegouda
 
PPT
Introduction to Python
amiable_indian
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
PPTX
Packages In Python Tutorial
Simplilearn
 
PPTX
Array of objects.pptx
RAGAVIC2
 
PPTX
Regular expressions in Python
Sujith Kumar
 
PPTX
Functions in Python
Kamal Acharya
 
PPTX
Data Structures in Python
Devashish Kumar
 
PDF
Introduction to python programming
Srinivas Narasegouda
 
PPTX
Chapter 03 python libraries
Praveen M Jigajinni
 
PPTX
Python Exception Handling
Megha V
 
PPTX
Chapter 07 inheritance
Praveen M Jigajinni
 
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
PDF
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
Intro to Python Programming Language
Dipankar Achinta
 
Modules and packages in python
TMARAGATHAM
 
Python dictionary
Mohammed Sikander
 
Object oriented approach in python programming
Srinivas Narasegouda
 
Introduction to Python
amiable_indian
 
Python Modules
Nitin Reddy Katkam
 
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Packages In Python Tutorial
Simplilearn
 
Array of objects.pptx
RAGAVIC2
 
Regular expressions in Python
Sujith Kumar
 
Functions in Python
Kamal Acharya
 
Data Structures in Python
Devashish Kumar
 
Introduction to python programming
Srinivas Narasegouda
 
Chapter 03 python libraries
Praveen M Jigajinni
 
Python Exception Handling
Megha V
 
Chapter 07 inheritance
Praveen M Jigajinni
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
Arrays In Python | Python Array Operations | Edureka
Edureka!
 

Similar to Class, object and inheritance in python (20)

PPTX
UNIT-5 object oriented programming lecture
w6vsy4fmpy
 
PPT
Introduction to Python - Part Three
amiable_indian
 
PDF
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
SudhanshiBakre1
 
PDF
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
SudhanshiBakre1
 
PDF
Python introduction 2
Ahmad Hussein
 
PPTX
Module-5-Classes and Objects for Python Programming.pptx
YogeshKumarKJMIT
 
PPTX
Object Oriented Programming in Python.pptx
grpvasundhara1993
 
PPTX
oogshsvshsbhshhshvsvshsvsvhshshjshshhsvgps.pptx
BhojarajTheking
 
DOCX
Python inheritance
ISREducations
 
PPTX
OOPS.pptx
NitinSharma134320
 
PPT
Python3
Ruchika Sinha
 
PPTX
Python programming computer science and engineering
IRAH34
 
PPTX
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
hpearl130
 
PPTX
Classes and Objects _
swati463221
 
PDF
Python unit 3 m.sc cs
KALAISELVI P
 
PPTX
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
 
PPTX
Python_Unit_2 OOPS.pptx
ChhaviCoachingCenter
 
PPTX
python1 object oriented programming.pptx
PravinBhargav1
 
PPTX
Presentation 3rd
Connex
 
UNIT-5 object oriented programming lecture
w6vsy4fmpy
 
Introduction to Python - Part Three
amiable_indian
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
SudhanshiBakre1
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
SudhanshiBakre1
 
Python introduction 2
Ahmad Hussein
 
Module-5-Classes and Objects for Python Programming.pptx
YogeshKumarKJMIT
 
Object Oriented Programming in Python.pptx
grpvasundhara1993
 
oogshsvshsbhshhshvsvshsvsvhshshjshshhsvgps.pptx
BhojarajTheking
 
Python inheritance
ISREducations
 
Python3
Ruchika Sinha
 
Python programming computer science and engineering
IRAH34
 
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
hpearl130
 
Classes and Objects _
swati463221
 
Python unit 3 m.sc cs
KALAISELVI P
 
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
 
Python_Unit_2 OOPS.pptx
ChhaviCoachingCenter
 
python1 object oriented programming.pptx
PravinBhargav1
 
Presentation 3rd
Connex
 
Ad

More from Santosh Verma (9)

PPTX
Migrating localhost to server
Santosh Verma
 
PPTX
Wordpress tutorial
Santosh Verma
 
PPTX
Sorting tech comparision
Santosh Verma
 
PPTX
Functions in python
Santosh Verma
 
PPTX
Access modifiers in Python
Santosh Verma
 
PPTX
SoC: System On Chip
Santosh Verma
 
PPTX
Embedded system design using arduino
Santosh Verma
 
PPTX
Snapdragon SoC and ARMv7 Architecture
Santosh Verma
 
PPTX
Trends and innovations in Embedded System Education
Santosh Verma
 
Migrating localhost to server
Santosh Verma
 
Wordpress tutorial
Santosh Verma
 
Sorting tech comparision
Santosh Verma
 
Functions in python
Santosh Verma
 
Access modifiers in Python
Santosh Verma
 
SoC: System On Chip
Santosh Verma
 
Embedded system design using arduino
Santosh Verma
 
Snapdragon SoC and ARMv7 Architecture
Santosh Verma
 
Trends and innovations in Embedded System Education
Santosh Verma
 
Ad

Recently uploaded (20)

PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
infertility, types,causes, impact, and management
Ritu480198
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 

Class, object and inheritance in python

  • 1. OOPs CONCEPT IN PYTHON SANTOSH VERMA Faculty Development Program: Python Programming JK Lakshmipat University, Jaipur (3-7, June 2019)
  • 2. Important Note  Please remember that Python completely works on indentation.  Use the tab key to provide indentation to your code.
  • 3. Classes and Objects Classes Just like every other Object Oriented Programming language Python supports classes. Let’s look at some points on Python classes. Classes are created by keyword class.  Attributes are the variables that belong to class.  Attributes are always public and can be accessed using dot (.) operator. Eg.: Myclass.Myattribute
  • 4. # A simple example class class Test: # A sample method def fun(self): print("Hello") # Driver code obj = Test() obj.fun() Output: Hello
  • 5. The self  Class methods must have an extra first parameter in method definition. do not give a value for this parameter when we call the method, Python provides it.  If we have a method which takes no arguments, then we still have to one argument – the self. See fun() in above simple example.  This is similar to this pointer in C++ and this reference in Java.  When we call a method of this object as myobject.method(arg1, arg2), is automatically converted by Python into MyClass.method(myobject, arg2) – this is all the special self is about.
  • 6. # creates a class named MyClass class MyClass: # assign the values to the MyClass attributes number = 0 name = "noname" def Main(): # Creating an object of the MyClass.Here, 'me' is the object me = MyClass() # Accessing the attributes of MyClass, using the dot(.) operator me.number = 310 me.name = "Santosh" # str is an build-in function that, creates an string print(me.name + " " + str(me.number)) # telling python that there is main in the program. if __name__=='__main__': Main() Output: Santosh 310
  • 7.  The __init__ method The __init__ method is similar to constructors in C++ and Java. It is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object. class Person: # init method or constructor def __init__(self, name): self.name = name # Sample Method def say_hi(self): print('Hello, my name is', self.name) p = Person(‘Santosh') p.say_hi()
  • 8. Methods  Method is a bunch of code that is intended to perform a particular task in your Python’s code.  Function that belongs to a class is called an Method.  All methods require ‘self’ first parameter. If you have coded in other OOP language you can think of ‘self’ as the ‘this’ keyword which is used for the current object. It unhides the current instance variable. ’self’ mostly work like ‘this’.  ‘def’ keyword is used to create a new method.
  • 9. class Vector2D: x = 0.0 y = 0.0 # Creating a method named Set def Set(self, x, y): self.x = x self.y = y def Main(): # vec is an object of class Vector2D vec = Vector2D() # Passing values to the function Set # by using dot(.) operator. vec.Set(5, 6) print("X: " + vec.x + ", Y: " + vec.y) if __name__=='__main__': Main() Output: X: 5, Y: 6
  • 11. Inheritance  Inheritance is the capability of one class to derive or inherit the properties from some another class. The benefits of inheritance are:  It represents real-world relationships well.  It provides reusability of a code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.  It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.  Inheritance is defined as a way in which a particular class inherits features from its base class.  Base class is also knows as ‘Superclass’ and the class which inherits from the Superclass is knows as ‘Subclass’
  • 12.  # Syntax for inheritance  class derived-classname(superclass-name)
  • 13. class Person(object): # class Person: of Python 3.x is same as defined here. def __init__(self, name): # Constructor self.name = name def getName(self): # To get name return self.name def isEmployee(self): # To check if this person is employee return False # Inherited or Sub class (Note Person in bracket) class Employee(Person): def isEmployee(self): # Here we return true return True # Driver code emp = Person(“Ram") # An Object of Person print(emp.getName(), emp.isEmployee()) emp = Employee(“Shyam") # An Object of Employee print(emp.getName(), emp.isEmployee()) OUTPUT: Ram False Shyam True
  • 14. Explore more on Inheritance Type Thank You!