SlideShare a Scribd company logo
3
Most read
11
Most read
14
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

PPTX
Object oriented programming with python
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PPTX
Object Oriented Programming in Python
PDF
Object oriented approach in python programming
PPTX
Python OOPs
PDF
Python - object oriented
PPTX
Object oriented programming in python
PPTX
Chapter 06 constructors and destructors
Object oriented programming with python
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Object Oriented Programming in Python
Object oriented approach in python programming
Python OOPs
Python - object oriented
Object oriented programming in python
Chapter 06 constructors and destructors

What's hot (20)

PPTX
Python-Encapsulation.pptx
PPTX
Chapter 05 classes and objects
PPTX
Chapter 03 python libraries
PPTX
CLASS OBJECT AND INHERITANCE IN PYTHON
PPTX
Inheritance in java
PPTX
Basics of Object Oriented Programming in Python
PPTX
Functions in Python
ODP
PPT
PYTHON - TKINTER - GUI - PART 1.ppt
PPTX
Chapter 07 inheritance
PDF
Python libraries
PPSX
Modules and packages in python
PPTX
INLINE FUNCTION IN C++
PDF
Python programming : Classes objects
PDF
Python list
PPT
Abstract class in java
PPTX
Packages In Python Tutorial
PPTX
OOP concepts -in-Python programming language
PPTX
File handling in Python
PPTX
Python-Polymorphism.pptx
Python-Encapsulation.pptx
Chapter 05 classes and objects
Chapter 03 python libraries
CLASS OBJECT AND INHERITANCE IN PYTHON
Inheritance in java
Basics of Object Oriented Programming in Python
Functions in Python
PYTHON - TKINTER - GUI - PART 1.ppt
Chapter 07 inheritance
Python libraries
Modules and packages in python
INLINE FUNCTION IN C++
Python programming : Classes objects
Python list
Abstract class in java
Packages In Python Tutorial
OOP concepts -in-Python programming language
File handling in Python
Python-Polymorphism.pptx
Ad

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

PDF
Python unit 3 m.sc cs
PPTX
Python advance
PPTX
Python programming computer science and engineering
PPT
Lecture topic - Python class lecture.ppt
PPT
Lecture on Python class -lecture123456.ppt
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
PPTX
Object Oriented Programming.pptx
PPTX
Python – Object Oriented Programming
PPT
Python3
PPTX
object oriented porgramming using Java programming
PPTX
Python Lecture 13
PPTX
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
PDF
Object_Oriented_Programming_Unit3.pdf
PDF
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
PDF
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
PPT
Introduction to Python - Part Three
PDF
Object oriented Programning Lanuagues in text format.
PPTX
Python_Object_Oriented_Programming.pptx
PPTX
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
Python unit 3 m.sc cs
Python advance
Python programming computer science and engineering
Lecture topic - Python class lecture.ppt
Lecture on Python class -lecture123456.ppt
Unit 3-Classes ,Objects and Inheritance.pdf
Object Oriented Programming.pptx
Python – Object Oriented Programming
Python3
object oriented porgramming using Java programming
Python Lecture 13
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
Object_Oriented_Programming_Unit3.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Introduction to Python - Part Three
Object oriented Programning Lanuagues in text format.
Python_Object_Oriented_Programming.pptx
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
Ad

More from Santosh Verma (9)

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

Recently uploaded (20)

PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
English Language Teaching from Post-.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Onica Farming 24rsclub profitable farm business
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Open Quiz Monsoon Mind Game Final Set.pptx
NOI Hackathon - Summer Edition - GreenThumber.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O7-L3 Supply Chain Operations - ICLT Program
Week 4 Term 3 Study Techniques revisited.pptx
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
Cardiovascular Pharmacology for pharmacy students.pptx
Cell Structure & Organelles in detailed.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Module 3: Health Systems Tutorial Slides S2 2025
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
English Language Teaching from Post-.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Onica Farming 24rsclub profitable farm business
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

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!