SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Downloaden Sie, um offline zu lesen
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Object Oriented Programming with Python
(Lecture # 01)
by
Muhammad Haroon
1. Object Oriented Programming
Object Oriented Programming (OOP) is a programming technique is which programs are written on the
basis of objects. An object is a collection of data and function. Object may represent a person, thing or
place in real world. In OOP, data and all possible functions on data are grouped together. Object
oriented programs are easier to learn and modify.
Object Oriented Programming is a powerful technique to develop software. It is used to analyze and
design the applications in terms of objects. It deals with data and the procedures that process the data as
a single object.
Some of the object oriented languages that have developed are:
✓ Python
✓ C++
✓ Smalltalk
✓ Eiffel
✓ CLOS
✓ Java
✓ C#
1.1 Features of Object-Oriented Programming
Following are some features of object oriented programming:
✓ Objects
✓ Classes
✓ Class variable
✓ Encapsulation/ Information Hiding
✓ Polymorphism
✓ Inheritance
✓ Overloading
✓ Overriding
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
✓ Abstraction
✓ Real world Modeling
✓ Reusability
Objects
OOP provides the facility of programming based on objects. Object is an entity that consists of data and
functions.
Classes
Classes are designs for creating objects. OOP provides the facility to design classes for creating different
objects. All properties and functions of an object are specified in classes.
Classes Variable
A variable that is shared by all instances of a class. Class variables are defined within a class but outside
any of the class's methods. Class variables are not used as frequently as instance variables are.
Data member
A class variable or instance variable that holds data associated with a class and its objects.
Function overloading
The assignment of more than one behavior to a particular function. The operation performed varies by
the types of objects or arguments involved.
Instance variable
A variable that is defined inside a method and belongs only to the current instance of a class.
Inheritance
The transfer of the characteristics of a class to other classes that are derived from it.
Instance
An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an
instance of the class Circle.
Instantiation
The creation of an instance of a class.
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Method
A special kind of function that is defined in a class definition.
Operator overloading
The assignment of more than one function to a particular operator.
Real world Modeling
OOP is based on real world modeling. As in real world, things have properties and working capabilities.
Similarly, objects have data and functions. Data represents properties and functions represent working of
objects.
Reusability
OOP provides ways of reusing the data and code. Inheritance is a technique that allows a programmer to
use the code of existing program to create new programs.
Information Hiding
OOP allows the programmer to hide important data from the user. It is performed by encapsulation.
Polymorphism
Polymorphism is an ability of an object to behave in multiple ways.
Objects
An object represents an entity in the real world such as a person, thing or concept etc. An object is
identified by its name. An object consists of the following two things:
✓ Properties: Properties are the characteristics of an object.
✓ Functions: Functions are the action that can be performed by an object.
Examples
Some examples of objects of different types are as follows:
✓ Physical Objects
o Vehicles such as car, bus, truck etc.
o Electrical Components
✓ Elements of Computer User Environment
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
o Windows
o Menus
o Graphics objects
o Mouse and Keyboard
✓ User-defined Data Types
o Time
o Angles
Properties of Object
The characteristics of an objects are known as its properties or attributes. Each object has its own
properties. These properties can be used to describe the object. For example, the properties of an object
Person can be as follows:
✓ Name
✓ Age
✓ Weight
The properties of an object Car can be as follows:
✓ Color
✓ Price
✓ Model Engine
✓ Engine Power
The set of values of the attributes of a particular object is called its state. It means that the state of an
object can be determined by the values of its attributes. The following figure shows the values of the
attributes of an object Car:
Car
Model Honda City
Color Silver
Price 1200000
Engine Power 1600CC
Table 1.1: Properties of an object Car
Function of Object
An object can perform different tasks and actions. The actions that can be performed by an object are
known as functions or methods. For example, the object Car can perform the following functions:
✓ Start
✓ Stop
✓ Accelerate
✓ Reverse
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
The set of all functions of an object represents the behavior of the object. It means that the overall
behavior of an object can be determined by the list of functions of that object.
Car
Start
Stop
Accelerate
Reverse
Table 2.1: Functions of an object Car
Classes
A collection of objects with same properties and functions is known as class. A class is used to define
the characteristics of the objects. It is used as model for creating different objects of same type. For
example, a class Person can be used to define the characteristics and functions of a person. It can be
used to create many object of type person such as Haroon, Farhan, Jahanzaib, Usman etc. All objects of
Person class will have same characteristics and functions. However, the values of each object can be
different. The values are of the objects are assigned after creating an object.
Each object of a class is known as an instance of its class. For example, Haroon, Farhan and Jahanzaib
are three instances of a class Person. Similarly, myBook and youBook can be two instances of a class
Book.
Declaring a Classes
A class is declared in the same way as a structure is declared. The keyword class is used to declare a
class. A class declaration specifies the variables and functions that are common to all objects of that
class. The variables declared in a class are known as member variable or data members.
Syntax
The syntax of declaring a class is as follows:
class ClassName:
'Optional class documentation string'
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
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Below is a simple Python program that creates a class with single method.
class Name:
# A sample method
def fun(self):
print("Hello Haroon")
# Code
obj = Name()
obj.fun()
The self
✓ Class methods must have an extra first parameter in method definition. We 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 have 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), this is automatically converted by
Python into MyClass.method(myobject, arg1, arg2) – this is all the special self is about.
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.
# A Sample class with init method
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)
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
p = Person('Muhammad Haroon')
p.say_hi()
Class and Instance Variables/attributes
In the Python language, instance variables are variables whose value is assigned inside a constructor or
method with self.
Class variables are variables whose value is assigned in class.
# Python program to show that the variables with a value
# assigned in class declaration, are class variables and
# variables inside methods and constructors are instance
# variables.
# Class for Computer Science Student
class CSStudent:
# Class Variable
stream = 'cs'
# The init method or constructor
def __init__(self, roll):
# Instance Variable
self.roll = roll
# Objects of CSStudent class
a = CSStudent(1)
b = CSStudent(2)
print(a.stream) # prints "cs"
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
print(b.stream) # prints "cs"
print(a.roll) # prints 1
# Class variables can be accessed using class
# name also
print(CSStudent.stream) # prints "cs"
We can define instance variables inside normal methods also.
# Python program to show that we can create
# instance variables inside methods
# Class for Computer Science Student
class CSStudent:
# Class Variable
stream = 'cs'
# The init method or constructor
def __init__(self, roll):
# Instance Variable
self.roll = roll
# Adds an instance variable
def setAddress(self, address):
self.address = address
# Retrieves instance variable
def getAddress(self):
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
return self.address
# Code
a = CSStudent(101)
a.setAddress("Muhammad Haroon, Multan")
print(a.getAddress())
How to create an empty class?
We can create an empty class using pass statement in Python.
# An empty class
class Test:
pass
End

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationPython Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and Indentation
 
Django Interview Questions and Answers
Django Interview Questions and AnswersDjango Interview Questions and Answers
Django Interview Questions and Answers
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Basic Crud In Django
Basic Crud In DjangoBasic Crud In Django
Basic Crud In Django
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
Python Programming Language | Python Classes | Python Tutorial | Python Train...
Python Programming Language | Python Classes | Python Tutorial | Python Train...Python Programming Language | Python Classes | Python Tutorial | Python Train...
Python Programming Language | Python Classes | Python Tutorial | Python Train...
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
Python-Polymorphism.pptx
Python-Polymorphism.pptxPython-Polymorphism.pptx
Python-Polymorphism.pptx
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
Java Basic Oops Concept
Java Basic Oops ConceptJava Basic Oops Concept
Java Basic Oops Concept
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
 

Ähnlich wie Lecture 01 - Basic Concept About OOP With Python

Regex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overlodingRegex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overloding
sangumanikesh
 
Object oriented software engineering concepts
Object oriented software engineering conceptsObject oriented software engineering concepts
Object oriented software engineering concepts
Komal Singh
 

Ähnlich wie Lecture 01 - Basic Concept About OOP With Python (20)

Python-Classes.pptx
Python-Classes.pptxPython-Classes.pptx
Python-Classes.pptx
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Regex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overlodingRegex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overloding
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptx
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptx
 
OOP Presentation.pptx
OOP Presentation.pptxOOP Presentation.pptx
OOP Presentation.pptx
 
OOP Presentation.pptx
OOP Presentation.pptxOOP Presentation.pptx
OOP Presentation.pptx
 
Object oriented software engineering concepts
Object oriented software engineering conceptsObject oriented software engineering concepts
Object oriented software engineering concepts
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
 
Oops concepts
Oops conceptsOops concepts
Oops concepts
 
OOP_1_TEG
OOP_1_TEGOOP_1_TEG
OOP_1_TEG
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
Lecture # 02 - OOP with Python Language by Muhammad Haroon
Lecture # 02 - OOP with Python Language by Muhammad HaroonLecture # 02 - OOP with Python Language by Muhammad Haroon
Lecture # 02 - OOP with Python Language by Muhammad Haroon
 
oopusingc.pptx
oopusingc.pptxoopusingc.pptx
oopusingc.pptx
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
 
BIS08 Application Development - II
BIS08 Application Development - IIBIS08 Application Development - II
BIS08 Application Development - II
 
Ooad notes
Ooad notesOoad notes
Ooad notes
 
Lecture01
Lecture01Lecture01
Lecture01
 

Mehr von National College of Business Administration & Economics ( NCBA&E)

Mehr von National College of Business Administration & Economics ( NCBA&E) (20)

Lecturre 07 - Chapter 05 - Basic Communications Operations
Lecturre 07 - Chapter 05 - Basic Communications  OperationsLecturre 07 - Chapter 05 - Basic Communications  Operations
Lecturre 07 - Chapter 05 - Basic Communications Operations
 
Lecture 05 - Chapter 03 - Examples
Lecture 05 - Chapter 03 - ExamplesLecture 05 - Chapter 03 - Examples
Lecture 05 - Chapter 03 - Examples
 
Lecture 06 - Chapter 4 - Communications in Networks
Lecture 06 - Chapter 4 - Communications in NetworksLecture 06 - Chapter 4 - Communications in Networks
Lecture 06 - Chapter 4 - Communications in Networks
 
Lecture 05 - Chapter 3 - Models of parallel computers and interconnections
Lecture 05 - Chapter 3 - Models of parallel computers and  interconnectionsLecture 05 - Chapter 3 - Models of parallel computers and  interconnections
Lecture 05 - Chapter 3 - Models of parallel computers and interconnections
 
Lecture01 Part(B) - Installing Visual Studio Code On All Version Of Windows O...
Lecture01 Part(B) - Installing Visual Studio Code On All Version Of Windows O...Lecture01 Part(B) - Installing Visual Studio Code On All Version Of Windows O...
Lecture01 Part(B) - Installing Visual Studio Code On All Version Of Windows O...
 
Lecture02 - Fundamental Programming with Python Language
Lecture02 - Fundamental Programming with Python LanguageLecture02 - Fundamental Programming with Python Language
Lecture02 - Fundamental Programming with Python Language
 
Lecture01 - Fundamental Programming with Python Language
Lecture01 - Fundamental Programming with Python LanguageLecture01 - Fundamental Programming with Python Language
Lecture01 - Fundamental Programming with Python Language
 
Lecture 04 (Part 01) - Measure of Location
Lecture 04 (Part 01) - Measure of LocationLecture 04 (Part 01) - Measure of Location
Lecture 04 (Part 01) - Measure of Location
 
Lecture 04 chapter 2 - Parallel Programming Platforms
Lecture 04  chapter 2 - Parallel Programming PlatformsLecture 04  chapter 2 - Parallel Programming Platforms
Lecture 04 chapter 2 - Parallel Programming Platforms
 
Lecture 04 Chapter 1 - Introduction to Parallel Computing
Lecture 04  Chapter 1 - Introduction to Parallel ComputingLecture 04  Chapter 1 - Introduction to Parallel Computing
Lecture 04 Chapter 1 - Introduction to Parallel Computing
 
Lecture 03 Part 02 - All Examples of Chapter 02 by Muhammad Haroon
Lecture 03 Part 02 - All Examples of Chapter 02 by Muhammad HaroonLecture 03 Part 02 - All Examples of Chapter 02 by Muhammad Haroon
Lecture 03 Part 02 - All Examples of Chapter 02 by Muhammad Haroon
 
Lecture 03 - Chapter 02 - Part 02 - Probability & Statistics by Muhammad Haroon
Lecture 03 - Chapter 02 - Part 02 - Probability & Statistics by Muhammad HaroonLecture 03 - Chapter 02 - Part 02 - Probability & Statistics by Muhammad Haroon
Lecture 03 - Chapter 02 - Part 02 - Probability & Statistics by Muhammad Haroon
 
Lecture 03 - Synchronous and Asynchronous Communication - Concurrency - Fault...
Lecture 03 - Synchronous and Asynchronous Communication - Concurrency - Fault...Lecture 03 - Synchronous and Asynchronous Communication - Concurrency - Fault...
Lecture 03 - Synchronous and Asynchronous Communication - Concurrency - Fault...
 
Lecture 03 - Chapter 02 - Part 01 - Probability & Statistics by Muhammad Haroon
Lecture 03 - Chapter 02 - Part 01 - Probability & Statistics by Muhammad HaroonLecture 03 - Chapter 02 - Part 01 - Probability & Statistics by Muhammad Haroon
Lecture 03 - Chapter 02 - Part 01 - Probability & Statistics by Muhammad Haroon
 
Lecture 02 - Chapter 01 - Probability & Statistics by Muhammad Haroon
Lecture 02 - Chapter 01 - Probability & Statistics by Muhammad HaroonLecture 02 - Chapter 01 - Probability & Statistics by Muhammad Haroon
Lecture 02 - Chapter 01 - Probability & Statistics by Muhammad Haroon
 
Lecture 02 - Chapter 1 (Part 02): Grid/Cloud Computing Systems, Cluster Comp...
Lecture 02 - Chapter 1 (Part 02):  Grid/Cloud Computing Systems, Cluster Comp...Lecture 02 - Chapter 1 (Part 02):  Grid/Cloud Computing Systems, Cluster Comp...
Lecture 02 - Chapter 1 (Part 02): Grid/Cloud Computing Systems, Cluster Comp...
 
Lecture 01 - Chapter 1 (Part 01): Some basic concept of Operating System (OS)...
Lecture 01 - Chapter 1 (Part 01): Some basic concept of Operating System (OS)...Lecture 01 - Chapter 1 (Part 01): Some basic concept of Operating System (OS)...
Lecture 01 - Chapter 1 (Part 01): Some basic concept of Operating System (OS)...
 
WHO director-general's opening remarks at the media briefing on covid-19 - 23...
WHO director-general's opening remarks at the media briefing on covid-19 - 23...WHO director-general's opening remarks at the media briefing on covid-19 - 23...
WHO director-general's opening remarks at the media briefing on covid-19 - 23...
 
Course outline of parallel and distributed computing
Course outline of parallel and distributed computingCourse outline of parallel and distributed computing
Course outline of parallel and distributed computing
 
Lecture 01 - Some basic terminology, History, Application of statistics - Def...
Lecture 01 - Some basic terminology, History, Application of statistics - Def...Lecture 01 - Some basic terminology, History, Application of statistics - Def...
Lecture 01 - Some basic terminology, History, Application of statistics - Def...
 

Kürzlich hochgeladen

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 

Kürzlich hochgeladen (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

Lecture 01 - Basic Concept About OOP With Python

  • 1. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Object Oriented Programming with Python (Lecture # 01) by Muhammad Haroon 1. Object Oriented Programming Object Oriented Programming (OOP) is a programming technique is which programs are written on the basis of objects. An object is a collection of data and function. Object may represent a person, thing or place in real world. In OOP, data and all possible functions on data are grouped together. Object oriented programs are easier to learn and modify. Object Oriented Programming is a powerful technique to develop software. It is used to analyze and design the applications in terms of objects. It deals with data and the procedures that process the data as a single object. Some of the object oriented languages that have developed are: ✓ Python ✓ C++ ✓ Smalltalk ✓ Eiffel ✓ CLOS ✓ Java ✓ C# 1.1 Features of Object-Oriented Programming Following are some features of object oriented programming: ✓ Objects ✓ Classes ✓ Class variable ✓ Encapsulation/ Information Hiding ✓ Polymorphism ✓ Inheritance ✓ Overloading ✓ Overriding
  • 2. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 ✓ Abstraction ✓ Real world Modeling ✓ Reusability Objects OOP provides the facility of programming based on objects. Object is an entity that consists of data and functions. Classes Classes are designs for creating objects. OOP provides the facility to design classes for creating different objects. All properties and functions of an object are specified in classes. Classes Variable A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are. Data member A class variable or instance variable that holds data associated with a class and its objects. Function overloading The assignment of more than one behavior to a particular function. The operation performed varies by the types of objects or arguments involved. Instance variable A variable that is defined inside a method and belongs only to the current instance of a class. Inheritance The transfer of the characteristics of a class to other classes that are derived from it. Instance An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle. Instantiation The creation of an instance of a class.
  • 3. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Method A special kind of function that is defined in a class definition. Operator overloading The assignment of more than one function to a particular operator. Real world Modeling OOP is based on real world modeling. As in real world, things have properties and working capabilities. Similarly, objects have data and functions. Data represents properties and functions represent working of objects. Reusability OOP provides ways of reusing the data and code. Inheritance is a technique that allows a programmer to use the code of existing program to create new programs. Information Hiding OOP allows the programmer to hide important data from the user. It is performed by encapsulation. Polymorphism Polymorphism is an ability of an object to behave in multiple ways. Objects An object represents an entity in the real world such as a person, thing or concept etc. An object is identified by its name. An object consists of the following two things: ✓ Properties: Properties are the characteristics of an object. ✓ Functions: Functions are the action that can be performed by an object. Examples Some examples of objects of different types are as follows: ✓ Physical Objects o Vehicles such as car, bus, truck etc. o Electrical Components ✓ Elements of Computer User Environment
  • 4. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 o Windows o Menus o Graphics objects o Mouse and Keyboard ✓ User-defined Data Types o Time o Angles Properties of Object The characteristics of an objects are known as its properties or attributes. Each object has its own properties. These properties can be used to describe the object. For example, the properties of an object Person can be as follows: ✓ Name ✓ Age ✓ Weight The properties of an object Car can be as follows: ✓ Color ✓ Price ✓ Model Engine ✓ Engine Power The set of values of the attributes of a particular object is called its state. It means that the state of an object can be determined by the values of its attributes. The following figure shows the values of the attributes of an object Car: Car Model Honda City Color Silver Price 1200000 Engine Power 1600CC Table 1.1: Properties of an object Car Function of Object An object can perform different tasks and actions. The actions that can be performed by an object are known as functions or methods. For example, the object Car can perform the following functions: ✓ Start ✓ Stop ✓ Accelerate ✓ Reverse
  • 5. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 The set of all functions of an object represents the behavior of the object. It means that the overall behavior of an object can be determined by the list of functions of that object. Car Start Stop Accelerate Reverse Table 2.1: Functions of an object Car Classes A collection of objects with same properties and functions is known as class. A class is used to define the characteristics of the objects. It is used as model for creating different objects of same type. For example, a class Person can be used to define the characteristics and functions of a person. It can be used to create many object of type person such as Haroon, Farhan, Jahanzaib, Usman etc. All objects of Person class will have same characteristics and functions. However, the values of each object can be different. The values are of the objects are assigned after creating an object. Each object of a class is known as an instance of its class. For example, Haroon, Farhan and Jahanzaib are three instances of a class Person. Similarly, myBook and youBook can be two instances of a class Book. Declaring a Classes A class is declared in the same way as a structure is declared. The keyword class is used to declare a class. A class declaration specifies the variables and functions that are common to all objects of that class. The variables declared in a class are known as member variable or data members. Syntax The syntax of declaring a class is as follows: class ClassName: 'Optional class documentation string' 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
  • 6. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Below is a simple Python program that creates a class with single method. class Name: # A sample method def fun(self): print("Hello Haroon") # Code obj = Name() obj.fun() The self ✓ Class methods must have an extra first parameter in method definition. We 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 have 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), this is automatically converted by Python into MyClass.method(myobject, arg1, arg2) – this is all the special self is about. 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. # A Sample class with init method 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)
  • 7. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 p = Person('Muhammad Haroon') p.say_hi() Class and Instance Variables/attributes In the Python language, instance variables are variables whose value is assigned inside a constructor or method with self. Class variables are variables whose value is assigned in class. # Python program to show that the variables with a value # assigned in class declaration, are class variables and # variables inside methods and constructors are instance # variables. # Class for Computer Science Student class CSStudent: # Class Variable stream = 'cs' # The init method or constructor def __init__(self, roll): # Instance Variable self.roll = roll # Objects of CSStudent class a = CSStudent(1) b = CSStudent(2) print(a.stream) # prints "cs"
  • 8. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 print(b.stream) # prints "cs" print(a.roll) # prints 1 # Class variables can be accessed using class # name also print(CSStudent.stream) # prints "cs" We can define instance variables inside normal methods also. # Python program to show that we can create # instance variables inside methods # Class for Computer Science Student class CSStudent: # Class Variable stream = 'cs' # The init method or constructor def __init__(self, roll): # Instance Variable self.roll = roll # Adds an instance variable def setAddress(self, address): self.address = address # Retrieves instance variable def getAddress(self):
  • 9. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 return self.address # Code a = CSStudent(101) a.setAddress("Muhammad Haroon, Multan") print(a.getAddress()) How to create an empty class? We can create an empty class using pass statement in Python. # An empty class class Test: pass End