SlideShare a Scribd company logo
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

More Related Content

What's hot

Oop concepts in python
Oop concepts in pythonOop concepts in python
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
11 Curso de POO en java - métodos constructores y toString()
11 Curso de POO en java - métodos constructores y toString()11 Curso de POO en java - métodos constructores y toString()
11 Curso de POO en java - métodos constructores y toString()
Clara Patricia Avella Ibañez
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
farwa waqar
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
Md.Al-imran Roton
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
Binay Kumar Ray
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
Srinivas Narasegouda
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
Sujith Kumar
 
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
Sujith Kumar
 
Java Collections
Java  Collections Java  Collections
08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS
koolkampus
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
 
Object Oriented Design
Object Oriented DesignObject Oriented Design
Object Oriented Design
Sudarsun Santhiappan
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
BG Java EE Course
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
ammarbrohi
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Python
PythonPython
Python
대갑 김
 

What's hot (20)

Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
11 Curso de POO en java - métodos constructores y toString()
11 Curso de POO en java - métodos constructores y toString()11 Curso de POO en java - métodos constructores y toString()
11 Curso de POO en java - métodos constructores y toString()
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
 
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
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Object Oriented Design
Object Oriented DesignObject Oriented Design
Object Oriented Design
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Inheritance and polymorphism
 
Python
PythonPython
Python
 

Similar to Lecture 01 - Basic Concept About OOP With Python

Python-Classes.pptx
Python-Classes.pptxPython-Classes.pptx
Python-Classes.pptx
Karudaiyar Ganapathy
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
SanmatiRM
 
Regex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overlodingRegex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overloding
sangumanikesh
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptx
KunalYadav65140
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptx
RaazIndia
 
OOP Presentation.pptx
OOP Presentation.pptxOOP Presentation.pptx
OOP Presentation.pptx
DurgaPrasadVasantati
 
OOP Presentation.pptx
OOP Presentation.pptxOOP Presentation.pptx
OOP Presentation.pptx
DurgaPrasadVasantati
 
Object oriented software engineering concepts
Object oriented software engineering conceptsObject oriented software engineering concepts
Object oriented software engineering concepts
Komal Singh
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
AnmolVerma363503
 
Oops concepts
Oops conceptsOops concepts
Oops concepts
ACCESS Health Digital
 
OOP_1_TEG
OOP_1_TEGOOP_1_TEG
OOP_1_TEG
STMIK Surabaya
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
nitamhaske
 
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
National College of Business Administration & Economics ( NCBA&E)
 
oopusingc.pptx
oopusingc.pptxoopusingc.pptx
oopusingc.pptx
MohammedAlobaidy16
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
AshutoshTrivedi30
 
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
Madishetty Prathibha
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
Geophery sanga
 
BIS08 Application Development - II
BIS08 Application Development - IIBIS08 Application Development - II
BIS08 Application Development - II
Prithwis Mukerjee
 
Ooad notes
Ooad notesOoad notes
Ooad notes
NancyJP
 
Lecture01
Lecture01Lecture01
Lecture01
artgreen
 

Similar to 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-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptx
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.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
 

More from National College of Business Administration & Economics ( NCBA&E)

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
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 05 - Chapter 03 - Examples
Lecture 05 - Chapter 03 - ExamplesLecture 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
National College of Business Administration & Economics ( NCBA&E)
 
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
National College of Business Administration & Economics ( NCBA&E)
 
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...
National College of Business Administration & Economics ( NCBA&E)
 
Lecture02 - Fundamental Programming with Python Language
Lecture02 - Fundamental Programming with Python LanguageLecture02 - Fundamental Programming with Python Language
Lecture02 - Fundamental Programming with Python Language
National College of Business Administration & Economics ( NCBA&E)
 
Lecture01 - Fundamental Programming with Python Language
Lecture01 - Fundamental Programming with Python LanguageLecture01 - Fundamental Programming with Python Language
Lecture01 - Fundamental Programming with Python Language
National College of Business Administration & Economics ( NCBA&E)
 
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
National College of Business Administration & Economics ( NCBA&E)
 
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
National College of Business Administration & Economics ( NCBA&E)
 
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
National College of Business Administration & Economics ( NCBA&E)
 
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
National College of Business Administration & Economics ( NCBA&E)
 
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
National College of Business Administration & Economics ( NCBA&E)
 
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...
National College of Business Administration & Economics ( NCBA&E)
 
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
National College of Business Administration & Economics ( NCBA&E)
 
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
National College of Business Administration & Economics ( NCBA&E)
 
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...
National College of Business Administration & Economics ( NCBA&E)
 
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)...
National College of Business Administration & Economics ( NCBA&E)
 
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...
National College of Business Administration & Economics ( NCBA&E)
 
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
National College of Business Administration & Economics ( NCBA&E)
 
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...
National College of Business Administration & Economics ( NCBA&E)
 

More from 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...
 

Recently uploaded

S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 

Recently uploaded (20)

S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 

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