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

PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PPT
Ip address and subnetting
IGZ Software house
 
PPTX
Network protocols
Abiud Orina
 
PDF
Electron JS | Build cross-platform desktop applications with web technologies
Bethmi Gunasekara
 
PDF
Image segmentation with deep learning
Antonio Rueda-Toicen
 
PDF
Python exception handling
Mohammed Sikander
 
PDF
A comprehensive guide to Agentic AI Systems
Debmalya Biswas
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Ip address and subnetting
IGZ Software house
 
Network protocols
Abiud Orina
 
Electron JS | Build cross-platform desktop applications with web technologies
Bethmi Gunasekara
 
Image segmentation with deep learning
Antonio Rueda-Toicen
 
Python exception handling
Mohammed Sikander
 
A comprehensive guide to Agentic AI Systems
Debmalya Biswas
 

What's hot (20)

ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
Python-Classes.pptx
Karudaiyar Ganapathy
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PPTX
Multithreading in java
Monika Mishra
 
PPTX
Python Data Structures and Algorithms.pptx
ShreyasLawand
 
PPTX
Python OOPs
Binay Kumar Ray
 
PPTX
Functions in Python
Kamal Acharya
 
PDF
Python libraries
Prof. Dr. K. Adisesha
 
PPTX
Applets in java
Wani Zahoor
 
PPTX
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Data Structures in Python
Devashish Kumar
 
PPTX
Python-Encapsulation.pptx
Karudaiyar Ganapathy
 
PPTX
Vectors in Java
Abhilash Nair
 
PPTX
Event Handling in java
Google
 
PDF
Set methods in python
deepalishinkar1
 
PPT
Java Servlets
BG Java EE Course
 
PPTX
Looping statement in python
RaginiJain21
 
PPTX
Introduction to Object Oriented Programming
Moutaz Haddara
 
PPT
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
Python Modules
Nitin Reddy Katkam
 
Python-Classes.pptx
Karudaiyar Ganapathy
 
Modules and packages in python
TMARAGATHAM
 
Multithreading in java
Monika Mishra
 
Python Data Structures and Algorithms.pptx
ShreyasLawand
 
Python OOPs
Binay Kumar Ray
 
Functions in Python
Kamal Acharya
 
Python libraries
Prof. Dr. K. Adisesha
 
Applets in java
Wani Zahoor
 
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
Data Structures in Python
Devashish Kumar
 
Python-Encapsulation.pptx
Karudaiyar Ganapathy
 
Vectors in Java
Abhilash Nair
 
Event Handling in java
Google
 
Set methods in python
deepalishinkar1
 
Java Servlets
BG Java EE Course
 
Looping statement in python
RaginiJain21
 
Introduction to Object Oriented Programming
Moutaz Haddara
 
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
Ad

Similar to Introduction to OOP in Python (20)

PPTX
Python oop class 1
Aleksander Fabijan
 
PPTX
Object Oriented Programming Class and Objects
rubini8582
 
PPTX
Lec-21-Classes and Object Orientation.pptx
resoj26651
 
PPTX
Java 102 intro to object-oriented programming in java
agorolabs
 
PPTX
lecture_for programming and computing basics
JavedKhan524377
 
PPTX
CPP13 - Object Orientation
Michael Heron
 
PPTX
OOP Concepts Python with code refrences.pptx
SofiMusic
 
PPT
07slide.ppt
NuurAxmed2
 
PPTX
object oriented porgramming using Java programming
afsheenfaiq2
 
PPTX
object oriented porgramming using Java programming
afsheenfaiq2
 
PPTX
Python oop - class 2 (inheritance)
Aleksander Fabijan
 
PPTX
Creating class, self variables in Python
AditiPawaskar5
 
PPTX
Object oriented programming
msneha
 
PPTX
Introduction to Object Oriented Programming in Python.pptx
eduardocehenmu
 
PPTX
Object Oriented Programming in Python.pptx
grpvasundhara1993
 
PPTX
2classes in c#
Sireesh K
 
PPTX
OOP02-27022023-090456am.pptx
uzairrrfr
 
PPTX
CPP15 - Inheritance
Michael Heron
 
PPTX
Object Oriented Programming (OOP) Introduction
SamuelAnsong6
 
PPTX
Classes_python.pptx
RabiyaZhexembayeva
 
Python oop class 1
Aleksander Fabijan
 
Object Oriented Programming Class and Objects
rubini8582
 
Lec-21-Classes and Object Orientation.pptx
resoj26651
 
Java 102 intro to object-oriented programming in java
agorolabs
 
lecture_for programming and computing basics
JavedKhan524377
 
CPP13 - Object Orientation
Michael Heron
 
OOP Concepts Python with code refrences.pptx
SofiMusic
 
07slide.ppt
NuurAxmed2
 
object oriented porgramming using Java programming
afsheenfaiq2
 
object oriented porgramming using Java programming
afsheenfaiq2
 
Python oop - class 2 (inheritance)
Aleksander Fabijan
 
Creating class, self variables in Python
AditiPawaskar5
 
Object oriented programming
msneha
 
Introduction to Object Oriented Programming in Python.pptx
eduardocehenmu
 
Object Oriented Programming in Python.pptx
grpvasundhara1993
 
2classes in c#
Sireesh K
 
OOP02-27022023-090456am.pptx
uzairrrfr
 
CPP15 - Inheritance
Michael Heron
 
Object Oriented Programming (OOP) Introduction
SamuelAnsong6
 
Classes_python.pptx
RabiyaZhexembayeva
 
Ad

More from Aleksander Fabijan (8)

PPTX
Retrospective 1
Aleksander Fabijan
 
PPTX
Python oop third class
Aleksander Fabijan
 
PDF
Introduction to OOP in python inheritance
Aleksander Fabijan
 
PDF
The evolution of continuous experimentation in software product development: ...
Aleksander Fabijan
 
PDF
Introduction to data visualisation with D3
Aleksander Fabijan
 
PDF
JavaScript development methodology
Aleksander Fabijan
 
PDF
Introduction to js (cnt.)
Aleksander Fabijan
 
PDF
Javascript intro for MAH
Aleksander Fabijan
 
Retrospective 1
Aleksander Fabijan
 
Python oop third class
Aleksander Fabijan
 
Introduction to OOP in python inheritance
Aleksander Fabijan
 
The evolution of continuous experimentation in software product development: ...
Aleksander Fabijan
 
Introduction to data visualisation with D3
Aleksander Fabijan
 
JavaScript development methodology
Aleksander Fabijan
 
Introduction to js (cnt.)
Aleksander Fabijan
 
Javascript intro for MAH
Aleksander Fabijan
 

Recently uploaded (20)

PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PDF
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PDF
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
TestNG for Java Testing and Automation testing
ssuser0213cb
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
Presentation about variables and constant.pptx
kr2589474
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
oapresentation.pptx
mehatdhavalrajubhai
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
TestNG for Java Testing and Automation testing
ssuser0213cb
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Become an Agentblazer Champion Challenge
Dele Amefo
 

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.