SlideShare a Scribd company logo
Introduction to Object Oriented
Programming in Python
Aleksander Fabijan
Why OOP (from last time)?
• Often we describe real-life objects in code (which is easier with OOP),
• Code get’s more manageable,
• With OPP, it is easy to reuse previously written code,
• …
• Every bigger real-life project will be written in OOP.
Object oriented programming
• Object in Python is a representation of a person, a place, a bank
account, a car, or any item that the program should handle.
• Object Oriented Programming Recipe:
• (1) define classes (these are descriptions)
• (2) make object instances out of classes.
Class Car Car instances
Introduction to OOP in Python
OOP with a Taxi Example
• To learn OOP, we will use an example of a Taxi.
Example Object - Taxi
DATA
• DriverName
• OnDuty
• NumPassenger
• Cities
BEHAVIOR
• PickUpPassenger
• DropOffPassenger
• SetDriverName
• GetDriverName
Every object has two main components:
Data (the attributes about it)
Behavior (the methods)
• DriverName: string
• OnDuty: Boolean
• NumPassenger: int
• Cities:list
Taxi
• PickUpPassenger():int
• DropOffPassenger(): int
• SetDriverName(string)
• GetDriverName:string
Creating a simple class in Python
• In a class, we describe (1) how the object will look like, and (2) what
behavior it will have.
• Classes are the blueprints for a new data type in your program!
• A class should have at minimum*:
• A name (e.g. Class Taxi)
• A constructor method (that describes how to create a new object, __init__)
Example Class
CLASS NAME
ObjectVariables
The __init__ method
• __init__ is a special method in Python classes,
• The __init__ method is the constructor method for a class
• __init__ is called when ever an object of the class is constructed.
Programmer
makes a new
object
__init__
method is
called
Behaviour within the
__init__ method
executes
myTaxi = Taxi(“Aleks”) …
Self.taxiDriver = “Aleks”
…
Example Object - Taxi
DATA
• DriverName
• OnDuty
• NumPassenger
• Cities
Creating an object from the class
• From one class, you make objects (instances).
Object Creation Flow
• To create a new object, use the class name
Object Creation Flow
• To create a new object, use the class name
• When you create a new object, the __init__ method from the class is
called with the parameters that were passed.
Object Creation Flow
• The __init__method Is called
Example Object - Taxi
DATA
• DriverName
• OnDuty
• NumPassenger
• Cities
BEHAVIOR
• PickUpPassenger
• DropOffPassenger
• SetDriverName
• GetDriverName
Behavior of a class
• classes/objects can have methods just like functions except that they
have an extra self variable at the beginning.
• An object method takes as the first parameter the object (self) and
can accept any number of other parameters.
Example Object Method
This method changes the name of the taxi driver for the passed object (self).
Another example of an object method
Exercise: Write a class that describes a Bus.
• A bus is created with a number of seats, a color, and is driven by a bus
driver that has a name. New passengers can get in the bus, and
existing bus passengers can leave their seat and get of the bus.
Number of buss passengers can’t be smaller than 0.
Instructions
• Start by drawing a class diagram
• Create a class for the bus in python
• Create two objects of your class
Object vs. Class Variables
• Most variables are object specific (for example, the variable number
of passengers in a taxi is different for every taxi that we create).
• Some variables are common to all objects (for example, if we want to
count the number of taxis that we have in our company)
T1 T2 T7
Class Taxi -> number_of_taxi_vehicles = 7
Accessing class variables
• To access a class variable within a method, we use the @classmethod
decorator, and pass the class to the method.
Example use of class variable
Exercise2: Update your class Bus.
• As an owner of the bus company, I wish to keep track of the number
of busses that people buy (create).
Instructions
• Create a class variable
• Increment a class variable on __init__
• Create a @classmethod to print its value
Exercise 3: Hippo ZOO
• A local ZOO keeper wants to model his collection of Hippos in the Zoo.
• Every hippo has a name and size (in kg). In the morning, a zoo keeper
feeds the hypo so his weight increases. During the day the Hippo
exercises, therefore his weight goes down.
• A zoo keeper also needs a way to keep track how many Hippos he has in
the ZOO.
• Model this problem (1) with a Class diagram & (2) in Python OOP code.
Example Solution
Takeaways
• Today, we learned how to create simple classes and objects.
• A class is a blueprint for objects, and it contains attributes and behavior.
• We added to our classes a number of:
• instance methods & instance variables (for example, color, size, etc.).
• class methods & class variables (for exmaple, a number of taxis).
• We learned that new objects are created from a class through the
__init__ method.
For Next time
In our next lecture, we will learn how to reuse the code that we have
written today while describing other objects. We will learn about:
• Inheritance. (TransportVehicle – Taxi)
• Encapsulation (e.g. private functions)
• Polymorphism (defining the same function for different types of
objects)

More Related Content

What's hot (20)

4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
Jalpesh Vasa
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
Forziatech
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
Binay Kumar Ray
 
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | EdurekaWhat is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
Edureka!
 
What Are Python Modules? Edureka
What Are Python Modules? EdurekaWhat Are Python Modules? Edureka
What Are Python Modules? Edureka
Edureka!
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
nitamhaske
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Edureka!
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Kamal Acharya
 
Python-Classes.pptx
Python-Classes.pptxPython-Classes.pptx
Python-Classes.pptx
Karudaiyar Ganapathy
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.
Questpond
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
Raghuveer Guthikonda
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Ramish Suleman
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
Forziatech
 
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | EdurekaWhat is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
Edureka!
 
What Are Python Modules? Edureka
What Are Python Modules? EdurekaWhat Are Python Modules? Edureka
What Are Python Modules? Edureka
Edureka!
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
nitamhaske
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Edureka!
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.
Questpond
 

Similar to Introduction to OOP in Python (20)

Python oop class 1
Python oop   class 1Python oop   class 1
Python oop class 1
Aleksander Fabijan
 
Lecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With PythonLecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With Python
National College of Business Administration & Economics ( NCBA&E)
 
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 Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
object oriented porgramming using Java programming
object oriented porgramming using Java programmingobject oriented porgramming using Java programming
object oriented porgramming using Java programming
afsheenfaiq2
 
OOP Concepts Python with code refrences.pptx
OOP Concepts Python with code refrences.pptxOOP Concepts Python with code refrences.pptx
OOP Concepts Python with code refrences.pptx
SofiMusic
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptxIPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
Learnbay Datascience
 
Class_and_Object_with_Example_Python.pptx janbsbznnsbxghzbbshvxnxhnwn
Class_and_Object_with_Example_Python.pptx janbsbznnsbxghzbbshvxnxhnwnClass_and_Object_with_Example_Python.pptx janbsbznnsbxghzbbshvxnxhnwn
Class_and_Object_with_Example_Python.pptx janbsbznnsbxghzbbshvxnxhnwn
bandiranvitha
 
Python advance
Python advancePython advance
Python advance
Mukul Kirti Verma
 
Creating class, self variables in Python
Creating class, self variables in PythonCreating class, self variables in Python
Creating class, self variables in Python
AditiPawaskar5
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
KALAISELVI P
 
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
thuy27042005thieu
 
software construction and development week 3 Python lists, tuples, dictionari...
software construction and development week 3 Python lists, tuples, dictionari...software construction and development week 3 Python lists, tuples, dictionari...
software construction and development week 3 Python lists, tuples, dictionari...
MuhammadBilalAjmal2
 
Problem solving with python programming OOP's Concept
Problem solving with python programming OOP's ConceptProblem solving with python programming OOP's Concept
Problem solving with python programming OOP's Concept
rohitsharma24121
 
Python3
Python3Python3
Python3
Ruchika Sinha
 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMINGOOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
Unit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptxUnit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
 
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 Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
object oriented porgramming using Java programming
object oriented porgramming using Java programmingobject oriented porgramming using Java programming
object oriented porgramming using Java programming
afsheenfaiq2
 
OOP Concepts Python with code refrences.pptx
OOP Concepts Python with code refrences.pptxOOP Concepts Python with code refrences.pptx
OOP Concepts Python with code refrences.pptx
SofiMusic
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptxIPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
Class_and_Object_with_Example_Python.pptx janbsbznnsbxghzbbshvxnxhnwn
Class_and_Object_with_Example_Python.pptx janbsbznnsbxghzbbshvxnxhnwnClass_and_Object_with_Example_Python.pptx janbsbznnsbxghzbbshvxnxhnwn
Class_and_Object_with_Example_Python.pptx janbsbznnsbxghzbbshvxnxhnwn
bandiranvitha
 
Creating class, self variables in Python
Creating class, self variables in PythonCreating class, self variables in Python
Creating class, self variables in Python
AditiPawaskar5
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
KALAISELVI P
 
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
thuy27042005thieu
 
software construction and development week 3 Python lists, tuples, dictionari...
software construction and development week 3 Python lists, tuples, dictionari...software construction and development week 3 Python lists, tuples, dictionari...
software construction and development week 3 Python lists, tuples, dictionari...
MuhammadBilalAjmal2
 
Problem solving with python programming OOP's Concept
Problem solving with python programming OOP's ConceptProblem solving with python programming OOP's Concept
Problem solving with python programming OOP's Concept
rohitsharma24121
 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMINGOOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
Unit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptxUnit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
 
Ad

More from Aleksander Fabijan (9)

Retrospective 1
Retrospective 1Retrospective 1
Retrospective 1
Aleksander Fabijan
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
Aleksander Fabijan
 
Python oop - class 2 (inheritance)
Python   oop - class 2 (inheritance)Python   oop - class 2 (inheritance)
Python oop - class 2 (inheritance)
Aleksander Fabijan
 
Introduction to OOP in python inheritance
Introduction to OOP in python   inheritanceIntroduction to OOP in python   inheritance
Introduction to OOP in python inheritance
Aleksander Fabijan
 
The evolution of continuous experimentation in software product development: ...
The evolution of continuous experimentation in software product development: ...The evolution of continuous experimentation in software product development: ...
The evolution of continuous experimentation in software product development: ...
Aleksander Fabijan
 
Introduction to data visualisation with D3
Introduction to data visualisation with D3Introduction to data visualisation with D3
Introduction to data visualisation with D3
Aleksander Fabijan
 
JavaScript development methodology
JavaScript development methodologyJavaScript development methodology
JavaScript development methodology
Aleksander Fabijan
 
Introduction to js (cnt.)
Introduction to js (cnt.)Introduction to js (cnt.)
Introduction to js (cnt.)
Aleksander Fabijan
 
Javascript intro for MAH
Javascript intro for MAHJavascript intro for MAH
Javascript intro for MAH
Aleksander Fabijan
 
Python oop - class 2 (inheritance)
Python   oop - class 2 (inheritance)Python   oop - class 2 (inheritance)
Python oop - class 2 (inheritance)
Aleksander Fabijan
 
Introduction to OOP in python inheritance
Introduction to OOP in python   inheritanceIntroduction to OOP in python   inheritance
Introduction to OOP in python inheritance
Aleksander Fabijan
 
The evolution of continuous experimentation in software product development: ...
The evolution of continuous experimentation in software product development: ...The evolution of continuous experimentation in software product development: ...
The evolution of continuous experimentation in software product development: ...
Aleksander Fabijan
 
Introduction to data visualisation with D3
Introduction to data visualisation with D3Introduction to data visualisation with D3
Introduction to data visualisation with D3
Aleksander Fabijan
 
JavaScript development methodology
JavaScript development methodologyJavaScript development methodology
JavaScript development methodology
Aleksander Fabijan
 
Ad

Recently uploaded (20)

Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The SequelMarketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
BradBedford3
 
FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025
Safe Software
 
How AI Can Improve Media Quality Testing Across Platforms (1).pptx
How AI Can Improve Media Quality Testing Across Platforms (1).pptxHow AI Can Improve Media Quality Testing Across Platforms (1).pptx
How AI Can Improve Media Quality Testing Across Platforms (1).pptx
kalichargn70th171
 
Generative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its ApplicationsGenerative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its Applications
SandeepKS52
 
Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...
Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...
Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...
SheenBrisals
 
Design by Contract - Building Robust Software with Contract-First Development
Design by Contract - Building Robust Software with Contract-First DevelopmentDesign by Contract - Building Robust Software with Contract-First Development
Design by Contract - Building Robust Software with Contract-First Development
Par-Tec S.p.A.
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlowDevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
The rise of e-commerce has redefined how retailers operate—and reconciliation...
The rise of e-commerce has redefined how retailers operate—and reconciliation...The rise of e-commerce has redefined how retailers operate—and reconciliation...
The rise of e-commerce has redefined how retailers operate—and reconciliation...
Prachi Desai
 
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdfThe Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
Varsha Nayak
 
IBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - IntroductionIBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - Introduction
Gaurav Sharma
 
Agile Software Engineering Methodologies
Agile Software Engineering MethodologiesAgile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdfHow to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
QuickBooks Training
 
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Micro-Metrics Every Performance Engineer Should Validate Before Sign-OffMicro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Tier1 app
 
Plooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your wayPlooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your way
Plooma
 
Artificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across IndustriesArtificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across Industries
SandeepKS52
 
COBOL Programming with VSCode - IBM Certificate
COBOL Programming with VSCode - IBM CertificateCOBOL Programming with VSCode - IBM Certificate
COBOL Programming with VSCode - IBM Certificate
VICTOR MAESTRE RAMIREZ
 
How to purchase, license and subscribe to Microsoft Azure_PDF.pdf
How to purchase, license and subscribe to Microsoft Azure_PDF.pdfHow to purchase, license and subscribe to Microsoft Azure_PDF.pdf
How to purchase, license and subscribe to Microsoft Azure_PDF.pdf
victordsane
 
Online Queue Management System for Public Service Offices [Focused on Municip...
Online Queue Management System for Public Service Offices [Focused on Municip...Online Queue Management System for Public Service Offices [Focused on Municip...
Online Queue Management System for Public Service Offices [Focused on Municip...
Rishab Acharya
 
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
BradBedford3
 
Revolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management SoftwareRevolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management Software
Insurance Tech Services
 
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The SequelMarketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
BradBedford3
 
FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025
Safe Software
 
How AI Can Improve Media Quality Testing Across Platforms (1).pptx
How AI Can Improve Media Quality Testing Across Platforms (1).pptxHow AI Can Improve Media Quality Testing Across Platforms (1).pptx
How AI Can Improve Media Quality Testing Across Platforms (1).pptx
kalichargn70th171
 
Generative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its ApplicationsGenerative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its Applications
SandeepKS52
 
Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...
Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...
Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...
SheenBrisals
 
Design by Contract - Building Robust Software with Contract-First Development
Design by Contract - Building Robust Software with Contract-First DevelopmentDesign by Contract - Building Robust Software with Contract-First Development
Design by Contract - Building Robust Software with Contract-First Development
Par-Tec S.p.A.
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlowDevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
The rise of e-commerce has redefined how retailers operate—and reconciliation...
The rise of e-commerce has redefined how retailers operate—and reconciliation...The rise of e-commerce has redefined how retailers operate—and reconciliation...
The rise of e-commerce has redefined how retailers operate—and reconciliation...
Prachi Desai
 
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdfThe Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
Varsha Nayak
 
IBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - IntroductionIBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - Introduction
Gaurav Sharma
 
Agile Software Engineering Methodologies
Agile Software Engineering MethodologiesAgile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdfHow to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
QuickBooks Training
 
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Micro-Metrics Every Performance Engineer Should Validate Before Sign-OffMicro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Tier1 app
 
Plooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your wayPlooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your way
Plooma
 
Artificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across IndustriesArtificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across Industries
SandeepKS52
 
COBOL Programming with VSCode - IBM Certificate
COBOL Programming with VSCode - IBM CertificateCOBOL Programming with VSCode - IBM Certificate
COBOL Programming with VSCode - IBM Certificate
VICTOR MAESTRE RAMIREZ
 
How to purchase, license and subscribe to Microsoft Azure_PDF.pdf
How to purchase, license and subscribe to Microsoft Azure_PDF.pdfHow to purchase, license and subscribe to Microsoft Azure_PDF.pdf
How to purchase, license and subscribe to Microsoft Azure_PDF.pdf
victordsane
 
Online Queue Management System for Public Service Offices [Focused on Municip...
Online Queue Management System for Public Service Offices [Focused on Municip...Online Queue Management System for Public Service Offices [Focused on Municip...
Online Queue Management System for Public Service Offices [Focused on Municip...
Rishab Acharya
 
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
BradBedford3
 
Revolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management SoftwareRevolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management Software
Insurance Tech Services
 

Introduction to OOP in Python

  • 1. Introduction to Object Oriented Programming in Python Aleksander Fabijan
  • 2. Why OOP (from last time)? • Often we describe real-life objects in code (which is easier with OOP), • Code get’s more manageable, • With OPP, it is easy to reuse previously written code, • … • Every bigger real-life project will be written in OOP.
  • 3. Object oriented programming • Object in Python is a representation of a person, a place, a bank account, a car, or any item that the program should handle. • Object Oriented Programming Recipe: • (1) define classes (these are descriptions) • (2) make object instances out of classes. Class Car Car instances
  • 5. OOP with a Taxi Example • To learn OOP, we will use an example of a Taxi.
  • 6. Example Object - Taxi DATA • DriverName • OnDuty • NumPassenger • Cities BEHAVIOR • PickUpPassenger • DropOffPassenger • SetDriverName • GetDriverName Every object has two main components: Data (the attributes about it) Behavior (the methods)
  • 7. • DriverName: string • OnDuty: Boolean • NumPassenger: int • Cities:list Taxi • PickUpPassenger():int • DropOffPassenger(): int • SetDriverName(string) • GetDriverName:string
  • 8. Creating a simple class in Python • In a class, we describe (1) how the object will look like, and (2) what behavior it will have. • Classes are the blueprints for a new data type in your program! • A class should have at minimum*: • A name (e.g. Class Taxi) • A constructor method (that describes how to create a new object, __init__)
  • 10. The __init__ method • __init__ is a special method in Python classes, • The __init__ method is the constructor method for a class • __init__ is called when ever an object of the class is constructed. Programmer makes a new object __init__ method is called Behaviour within the __init__ method executes myTaxi = Taxi(“Aleks”) … Self.taxiDriver = “Aleks” …
  • 11. Example Object - Taxi DATA • DriverName • OnDuty • NumPassenger • Cities
  • 12. Creating an object from the class • From one class, you make objects (instances).
  • 13. Object Creation Flow • To create a new object, use the class name
  • 14. Object Creation Flow • To create a new object, use the class name • When you create a new object, the __init__ method from the class is called with the parameters that were passed.
  • 15. Object Creation Flow • The __init__method Is called
  • 16. Example Object - Taxi DATA • DriverName • OnDuty • NumPassenger • Cities BEHAVIOR • PickUpPassenger • DropOffPassenger • SetDriverName • GetDriverName
  • 17. Behavior of a class • classes/objects can have methods just like functions except that they have an extra self variable at the beginning. • An object method takes as the first parameter the object (self) and can accept any number of other parameters.
  • 18. Example Object Method This method changes the name of the taxi driver for the passed object (self).
  • 19. Another example of an object method
  • 20. Exercise: Write a class that describes a Bus. • A bus is created with a number of seats, a color, and is driven by a bus driver that has a name. New passengers can get in the bus, and existing bus passengers can leave their seat and get of the bus. Number of buss passengers can’t be smaller than 0. Instructions • Start by drawing a class diagram • Create a class for the bus in python • Create two objects of your class
  • 21. Object vs. Class Variables • Most variables are object specific (for example, the variable number of passengers in a taxi is different for every taxi that we create). • Some variables are common to all objects (for example, if we want to count the number of taxis that we have in our company) T1 T2 T7 Class Taxi -> number_of_taxi_vehicles = 7
  • 22. Accessing class variables • To access a class variable within a method, we use the @classmethod decorator, and pass the class to the method.
  • 23. Example use of class variable
  • 24. Exercise2: Update your class Bus. • As an owner of the bus company, I wish to keep track of the number of busses that people buy (create). Instructions • Create a class variable • Increment a class variable on __init__ • Create a @classmethod to print its value
  • 25. Exercise 3: Hippo ZOO • A local ZOO keeper wants to model his collection of Hippos in the Zoo. • Every hippo has a name and size (in kg). In the morning, a zoo keeper feeds the hypo so his weight increases. During the day the Hippo exercises, therefore his weight goes down. • A zoo keeper also needs a way to keep track how many Hippos he has in the ZOO. • Model this problem (1) with a Class diagram & (2) in Python OOP code.
  • 27. Takeaways • Today, we learned how to create simple classes and objects. • A class is a blueprint for objects, and it contains attributes and behavior. • We added to our classes a number of: • instance methods & instance variables (for example, color, size, etc.). • class methods & class variables (for exmaple, a number of taxis). • We learned that new objects are created from a class through the __init__ method.
  • 28. For Next time In our next lecture, we will learn how to reuse the code that we have written today while describing other objects. We will learn about: • Inheritance. (TransportVehicle – Taxi) • Encapsulation (e.g. private functions) • Polymorphism (defining the same function for different types of objects)

Editor's Notes

  • #6: A taxi has a driver Can pick up people Can drop off people And can travel only between certain cities
  • #9: You can even skip the __init__ however, you typically would have it.