SlideShare a Scribd company logo
Introduction to Object Oriented
Programming in Python
Aleksander Fabijan
Recap
integer = 9
string = "Some text"
dictionary = {
"name": "Aleksander",
"last_name": "Fabijan"
}
array = ['First Element', "Second YYY", dictionary]
# loop
for element in array:
if element == "Second YYY":
print("I found the second element: ", element)
Why OOP?
• 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 Recipie:
• (1) define classes (these are descriptions)
• (2) make object instances out of classes.
Class Car Car instances
OOP with a Taxi Example
• To learn OOP, we will use an example of a Taxi.
Example Object - Taxi
ATTRIBUTES
• DriverName
• OnDuty
• NumPassenger
• Cities
BEHAVIOR
• PickUpPassenger
• DropOffPassenger
• SetDriverName
• GetDriverName
Every object has two main components:
Attributes (the data 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__)
class Taxi(object):
’''This is a blueprint for a taxi! '’’
pass
CLASS NAME
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
Exercise: 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
Takeaways
• Today, we created our first class - Taxi. It describes a common object from
our life. We added to this class a number of methods, and one class
variable.
• A class is a blueprint for objects, and it contains attributes and behavior.
• A new object is created from a class through the __init__ method.
• Most variables are object specific (color, size, etc.). Some can be common
to all of the objects- class variables (for example, number of taxis).
For Next time
• Encapsulation
• Inheritance
• Polymorphism

More Related Content

What's hot

PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
Maulik Borsaniya
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
Arslan Arshad
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Function
FunctionFunction
Function
jayesh30sikchi
 
Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
Raghunath A
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
M Vishnuvardhan Reddy
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
nitamhaske
 
Lists
ListsLists
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Kamal Acharya
 
Python list
Python listPython list
Python list
Mohammed Sikander
 
What is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | EdurekaWhat is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | Edureka
Edureka!
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)
Fatima Kate Tanay
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Arrays
ArraysArrays
L11 array list
L11 array listL11 array list
L11 array list
teach4uin
 
Python strings
Python stringsPython strings
Python strings
Mohammed Sikander
 
python Function
python Function python Function
python Function
Ronak Rathi
 
Introduction to OOP in Python
Introduction to OOP in PythonIntroduction to OOP in Python
Introduction to OOP in Python
Aleksander Fabijan
 

What's hot (20)

PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Function
FunctionFunction
Function
 
Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Lists
ListsLists
Lists
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Python list
Python listPython list
Python list
 
What is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | EdurekaWhat is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | Edureka
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 
Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Arrays
ArraysArrays
Arrays
 
L11 array list
L11 array listL11 array list
L11 array list
 
Python strings
Python stringsPython strings
Python strings
 
python Function
python Function python Function
python Function
 
Introduction to OOP in Python
Introduction to OOP in PythonIntroduction to OOP in Python
Introduction to OOP in Python
 

Similar to Python oop class 1

Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
agorolabs
 
Class methods
Class methodsClass methods
Class methods
NainaKhan29
 
creating objects and Class methods
creating objects and Class methodscreating objects and Class methods
creating objects and Class methods
NainaKhan28
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 
Advanced oops concept using asp
Advanced oops concept using aspAdvanced oops concept using asp
Advanced oops concept using asp
shenbagavallijanarth
 
CPP13 - Object Orientation
CPP13 - Object OrientationCPP13 - Object Orientation
CPP13 - Object Orientation
Michael Heron
 
lecture_for programming and computing basics
lecture_for programming and computing basicslecture_for programming and computing basics
lecture_for programming and computing basics
JavedKhan524377
 
Lecture2.pdf
Lecture2.pdfLecture2.pdf
Lecture2.pdf
SakhilejasonMsibi
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
Introduction to java and oop
Introduction to java and oopIntroduction to java and oop
Intro to JavaScript - Week 4: Object and Array
Intro to JavaScript - Week 4: Object and ArrayIntro to JavaScript - Week 4: Object and Array
Intro to JavaScript - Week 4: Object and Array
Jeongbae Oh
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
Vu Tran Lam
 
Android App code starter
Android App code starterAndroid App code starter
Android App code starter
FatimaYousif11
 
Creating class, self variables in Python
Creating class, self variables in PythonCreating class, self variables in Python
Creating class, self variables in Python
AditiPawaskar5
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
 
COMP111-Week-1_138439.pptx
COMP111-Week-1_138439.pptxCOMP111-Week-1_138439.pptx
COMP111-Week-1_138439.pptx
FarooqTariq8
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
msneha
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
Abdullah Jan
 
Classes_python.pptx
Classes_python.pptxClasses_python.pptx
Classes_python.pptx
RabiyaZhexembayeva
 
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
 

Similar to Python oop class 1 (20)

Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 
Class methods
Class methodsClass methods
Class methods
 
creating objects and Class methods
creating objects and Class methodscreating objects and Class methods
creating objects and Class methods
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Advanced oops concept using asp
Advanced oops concept using aspAdvanced oops concept using asp
Advanced oops concept using asp
 
CPP13 - Object Orientation
CPP13 - Object OrientationCPP13 - Object Orientation
CPP13 - Object Orientation
 
lecture_for programming and computing basics
lecture_for programming and computing basicslecture_for programming and computing basics
lecture_for programming and computing basics
 
Lecture2.pdf
Lecture2.pdfLecture2.pdf
Lecture2.pdf
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
Introduction to java and oop
Introduction to java and oopIntroduction to java and oop
Introduction to java and oop
 
Intro to JavaScript - Week 4: Object and Array
Intro to JavaScript - Week 4: Object and ArrayIntro to JavaScript - Week 4: Object and Array
Intro to JavaScript - Week 4: Object and Array
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
 
Android App code starter
Android App code starterAndroid App code starter
Android App code starter
 
Creating class, self variables in Python
Creating class, self variables in PythonCreating class, self variables in Python
Creating class, self variables in Python
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
COMP111-Week-1_138439.pptx
COMP111-Week-1_138439.pptxCOMP111-Week-1_138439.pptx
COMP111-Week-1_138439.pptx
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
 
Classes_python.pptx
Classes_python.pptxClasses_python.pptx
Classes_python.pptx
 
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
 

More from Aleksander Fabijan

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
 
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
 

More from Aleksander Fabijan (8)

Retrospective 1
Retrospective 1Retrospective 1
Retrospective 1
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
 
Introduction to OOP in python inheritance
Introduction to OOP in python   inheritanceIntroduction to OOP in python   inheritance
Introduction to OOP in python inheritance
 
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: ...
 
Introduction to data visualisation with D3
Introduction to data visualisation with D3Introduction to data visualisation with D3
Introduction to data visualisation with D3
 
JavaScript development methodology
JavaScript development methodologyJavaScript development methodology
JavaScript development methodology
 
Introduction to js (cnt.)
Introduction to js (cnt.)Introduction to js (cnt.)
Introduction to js (cnt.)
 
Javascript intro for MAH
Javascript intro for MAHJavascript intro for MAH
Javascript intro for MAH
 

Recently uploaded

How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 

Recently uploaded (20)

How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 

Python oop class 1

  • 1. Introduction to Object Oriented Programming in Python Aleksander Fabijan
  • 2. Recap integer = 9 string = "Some text" dictionary = { "name": "Aleksander", "last_name": "Fabijan" } array = ['First Element', "Second YYY", dictionary] # loop for element in array: if element == "Second YYY": print("I found the second element: ", element)
  • 3. Why OOP? • 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
  • 4. 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 Recipie: • (1) define classes (these are descriptions) • (2) make object instances out of classes. Class Car Car instances
  • 5.
  • 6. OOP with a Taxi Example • To learn OOP, we will use an example of a Taxi.
  • 7. Example Object - Taxi ATTRIBUTES • DriverName • OnDuty • NumPassenger • Cities BEHAVIOR • PickUpPassenger • DropOffPassenger • SetDriverName • GetDriverName Every object has two main components: Attributes (the data about it) Behavior (the methods)
  • 8. • DriverName: string • OnDuty: Boolean • NumPassenger: int • Cities:list Taxi • PickUpPassenger():int • DropOffPassenger(): int • SetDriverName(string) • GetDriverName:string
  • 9. 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. class Taxi(object): ’''This is a blueprint for a taxi! '’’ pass CLASS NAME
  • 12. 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” …
  • 13. Example Object - Taxi DATA • DriverName • OnDuty • NumPassenger • Cities
  • 14. Creating an object from the class • From one class, you make objects (instances).
  • 15. Object Creation Flow • To create a new object, use the class name
  • 16. 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.
  • 17. Object Creation Flow • The __init__method Is called
  • 18. Example Object - Taxi DATA • DriverName • OnDuty • NumPassenger • Cities BEHAVIOR • PickUpPassenger • DropOffPassenger • SetDriverName • GetDriverName
  • 19. 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.
  • 20. Example Object Method This method changes the name of the taxi driver for the passed object (self).
  • 21. Another example of an object method
  • 22. 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
  • 23. 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
  • 24. Accessing class variables • To access a class variable within a method, we use the @classmethod decorator, and pass the class to the method.
  • 25. Example use of class variable
  • 26. Exercise: 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
  • 27. Takeaways • Today, we created our first class - Taxi. It describes a common object from our life. We added to this class a number of methods, and one class variable. • A class is a blueprint for objects, and it contains attributes and behavior. • A new object is created from a class through the __init__ method. • Most variables are object specific (color, size, etc.). Some can be common to all of the objects- class variables (for example, number of taxis).
  • 28. For Next time • Encapsulation • Inheritance • Polymorphism

Editor's Notes

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