SlideShare a Scribd company logo
1 of 28
Download to read offline
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
OBJECT ORIENTED PROGRAMMING
WITH PYTHON
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
LET’S DISCUSS
 Why OOPs
 Classes & Objects
 Inheritance
 Revisiting Types & Exceptions
 Polymorphism
 Operator Overloading
 Method Overriding
 Quick Quiz
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
THE CURRENT SCENARIO
Let’s store employee data
Why OOPs
# Ask for the name of the employee
name = input("Enter the name of the employee: ")
# Ask for the age of the employee
age = input("Enter the age of the employee: ")
# Print the collected information
print(f"Employee Name: {name}")
print(f"Employee Age: {age}")
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
LET’S IMPROVE IT
# Function to input employee data (name and age)
def input_employee_data():
name = input("Enter the name of the employee: ")
age = input("Enter the age of the employee: ")
return name, age
# Function to print employee information
def print_employee_info(name, age):
print(f"Employee Name: {name}")
print(f"Employee Age: {age}")
name, age = input_employee_data()
print_employee_info(name, age)
# Ask for the name of the employee
name = input("Enter the name of the employee: ")
# Ask for the age of the employee
age = input("Enter the age of the employee: ")
# Print the collected information
print(f"Employee Name: {name}")
print(f"Employee Age: {age}")
Why OOPs
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
IS THAT GOOD ENOUGH?
# Function to input employee data (name and age)
def input_employee_data():
name = input("Enter the name of the employee: ")
age = input("Enter the age of the employee: ")
return name, age
# Function to print employee information
def print_employee_info(name, age):
print(f"Employee Name: {name}")
print(f"Employee Age: {age}")
name, age = input_employee_data()
print_employee_info(name, age)
Why OOPs
class Employee:
def input_employee_data(self):
self.name = input("Enter the name of the employee: ")
self.age = input("Enter the age of the employee: ")
def print_employee_info(self):
print(f"Employee Name: {self.name}")
print(f"Employee Age: {self.age}")
# Create an instance of the Employee class
employee = Employee()
# Input employee data using the class method
employee.input_employee_data()
# Print employee information using the class method
employee.print_employee_info()
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
PARADIGMS OF PROGRAMMING
# Ask for the name of the employee
name = input("Enter the name : ")
# Ask for the age of the employee
age = input("Enter the age of the employee: ")
# Print the collected information
print(f"Employee Name: {name}")
print(f"Employee Age: {age}")
# Function to input employee data (name and age)
def input_employee_data():
name = input("Enter the name of the employee: ")
age = input("Enter the age of the employee: ")
return name, age
# Function to print employee information
def print_employee_info(name, age):
print(f"Employee Name: {name}")
print(f"Employee Age: {age}")
name, age = input_employee_data()
print_employee_info(name, age)
class Employee:
def input_employee_data(self):
self.name = input("Enter the name of the employee: ")
self.age = input("Enter the age of the employee: ")
def print_employee_info(self):
print(f"Employee Name: {self.name}")
print(f"Employee Age: {self.age}")
# Create an instance of the Employee class
employee = Employee()
# Input employee data using the class method
employee.input_employee_data()
# Print employee information using the class method
employee.print_employee_info()
Procedural Functional Object Oriented
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
CLASSES
Classes are used to create our
own type of data and give them
names.
Classes are “Blueprints”
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
OBJECTS
Any time you
create a class and
you utilize that
“blueprint” to
create “object”.
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
CLASSES AND OBJECTS IN ACTION
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
CLASSES AND OBJECTS IN ACTION
class Employee:
def input_employee_data(self):
self.name = input("Enter the name of the employee: ")
self.age = input("Enter the age of the employee: ")
def print_employee_info(self):
print(f"Employee Name: {self.name}")
print(f"Employee Age: {self.age}")
# Create an instance of the Employee class
employee1 = Employee()
# Create another instance of the Employee
class
employee2 = Employee()
# Yet another instance of the Employee class
employee3 = Employee()
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
ANATOMY OF A CLASS
class Employee:
...
Class is a
Blueprint
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
METHODS IN CLASS
# Create an instance of the Employee class
employee = Employee()
# Input employee data using the class method
employee.input_employee_data()
class Employee:
def input_employee_data(self):
self.name = input("Enter the name of the employee: ")
self.age = input("Enter the age of the employee: ")
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
WHAT IS “self”
class Employee:
def input_employee_data(self):
self.name = input("Enter the name of the employee: ")
self.age = input("Enter the age of the employee: ")
self refers to the current object that was just created.
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
ATTRIBUTES
class Employee:
def input_employee_data(self):
self.name = input("Enter the name of the employee: ")
self.age = input("Enter the age of the employee: ")
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
SET ATTRIBUTES WHILE CREATING THE OBJECT
class Employee:
def input_employee_data(self):
self.name = input("Enter the name of the employee: ")
self.age = input("Enter the age of the employee: ")
# Driver Code
# Create an instance of the Employee class
employee = Employee()
# Input employee data using the class method
employee.input_employee_data()
class Employee:
def __init__(self, name, age):
self.name = name
self.age = age
# Driver Code
name = input("Enter the name of the employee: ")
age = input("Enter the age of the employee: ")
# Create an instance of the Employee class
employee = Employee(name, age)
# Print the employee data
print("Employee Name:", employee.name)
print("Employee Age:", employee.age)
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
LET’S VISUALISE __init__()
class Employee:
def __init__(self, name, age):
self.name = name
self.age = age
self.name
self.age
name
age
# Create an instance
employee = Employee(name, age)
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
WHERE ARE THESE EMPLOYEE WORKING?
class Employee:
org = 'CompanyName'
def input_employee_data(self):
self.name = input("Enter the name of the employee: ")
self.age = input("Enter the age of the employee: ")
def print_employee_info(self):
print(f"Employee Name: {self.name}")
print(f"Employee Age: {self.age}")
emp = Employee()
print(emp.org)
Class Variables
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
WHAT ABOUT CODE REUSABILITY?
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
INHERITENCE
class Human:
...
class Employee:
...
class Human:
...
class Employee(Human):
...
Inheritance enables us
to create a class that
“inherits” methods,
variables, and attributes
from another class.
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
INHERITENCE IN ACTION
class Human:
def __init__(self, height):
self.height = height
class Employee(Human):
def __init__(self, height, name, age):
super().__init__(height)
self.name = name
self.age = age
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
YOU ARE ALREADY USING CLASSES
https://docs.python.org/3/library/stdtypes.html#str
surprise
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
EXCEPTIONS
BaseException
+-- KeyboardInterrupt
+-- Exception
+-- ArithmeticError
| +-- ZeroDivisionError
+-- AssertionError
+-- AttributeError
+-- EOFError
+-- ImportError
| +-- ModuleNotFoundError
+-- LookupError
| +-- KeyError
+-- NameError
+-- SyntaxError
| +-- IndentationError
+-- ValueError
...
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
POLYMORPHISM
 Poly – many
 Morph - shapes
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
OPERATOR OVERLOADING
 Some operators such as
+ and - can be
“overloaded” such that
they can have more
abilities beyond simple
arithmetic.
class MyNumber:
def __init__(self, num=0):
self.num = num
def __add__(self, other):
return self.num * other.num
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
HOW IT WORKED?
 We override the __add__ method
 https://docs.python.org/3/reference/datamod
el.html?highlight=__add__#object.__add__
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
METHOD OVERRIDING
class Human:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"Hello, I am {self.name} and I am {self.age} years old.")
class Employee(Human):
def __init__(self, name, age, employee_id):
super().__init__(name, age)
self.employee_id = employee_id
# Method overriding
def introduce(self):
print(f"Hello, I am {self.name}, an employee with ID {self.employee_id}.")
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
QUIZ TIME
BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only.
THANKS

More Related Content

Similar to Object Oriented Programming in Python

Having issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdfHaving issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdfrajkumarm401
 
Final Presentation V1.8
Final Presentation V1.8Final Presentation V1.8
Final Presentation V1.8Chad Koziel
 
HOW TO CREATE A MODULE IN ODOO
HOW TO CREATE A MODULE IN ODOOHOW TO CREATE A MODULE IN ODOO
HOW TO CREATE A MODULE IN ODOOCeline George
 
structure and union
structure and unionstructure and union
structure and unionstudent
 
Programming Primer Encapsulation CS
Programming Primer Encapsulation CSProgramming Primer Encapsulation CS
Programming Primer Encapsulation CSsunmitraeducation
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiMuhammed Thanveer M
 
CIS 247C iLab 4 of 7: Composition and Class Interfaces
CIS 247C iLab 4 of 7: Composition and Class Interfaces  CIS 247C iLab 4 of 7: Composition and Class Interfaces
CIS 247C iLab 4 of 7: Composition and Class Interfaces HomeWork-Fox
 
2. Create a Java class called EmployeeMain within the same project Pr.docx
 2. Create a Java class called EmployeeMain within the same project Pr.docx 2. Create a Java class called EmployeeMain within the same project Pr.docx
2. Create a Java class called EmployeeMain within the same project Pr.docxajoy21
 
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdfI am trying to change this code from STRUCTS to CLASSES, the members.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdfpetercoiffeur18
 
Written by Dr. Preiser 1 CIS 3100 MS Access Assignment .docx
Written by Dr. Preiser 1 CIS 3100 MS Access Assignment  .docxWritten by Dr. Preiser 1 CIS 3100 MS Access Assignment  .docx
Written by Dr. Preiser 1 CIS 3100 MS Access Assignment .docxtroutmanboris
 
Sencha Touch - Introduction
Sencha Touch - IntroductionSencha Touch - Introduction
Sencha Touch - IntroductionABC-GROEP.BE
 
Form demoinplaywithmysql
Form demoinplaywithmysqlForm demoinplaywithmysql
Form demoinplaywithmysqlKnoldus Inc.
 
Django Admin: Widgetry & Witchery
Django Admin: Widgetry & WitcheryDjango Admin: Widgetry & Witchery
Django Admin: Widgetry & WitcheryPamela Fox
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAMaulik Borsaniya
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
HELP IN JAVACreate a main method and use these input files to tes.pdf
HELP IN JAVACreate a main method and use these input files to tes.pdfHELP IN JAVACreate a main method and use these input files to tes.pdf
HELP IN JAVACreate a main method and use these input files to tes.pdffatoryoutlets
 
Mysql to mongo
Mysql to mongoMysql to mongo
Mysql to mongoAlex Sharp
 
ADBMS ASSIGNMENT
ADBMS ASSIGNMENTADBMS ASSIGNMENT
ADBMS ASSIGNMENTLori Moore
 

Similar to Object Oriented Programming in Python (20)

Having issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdfHaving issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdf
 
Final Presentation V1.8
Final Presentation V1.8Final Presentation V1.8
Final Presentation V1.8
 
HOW TO CREATE A MODULE IN ODOO
HOW TO CREATE A MODULE IN ODOOHOW TO CREATE A MODULE IN ODOO
HOW TO CREATE A MODULE IN ODOO
 
structure and union
structure and unionstructure and union
structure and union
 
Programming Primer Encapsulation CS
Programming Primer Encapsulation CSProgramming Primer Encapsulation CS
Programming Primer Encapsulation CS
 
Structure and union
Structure and unionStructure and union
Structure and union
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
CIS 247C iLab 4 of 7: Composition and Class Interfaces
CIS 247C iLab 4 of 7: Composition and Class Interfaces  CIS 247C iLab 4 of 7: Composition and Class Interfaces
CIS 247C iLab 4 of 7: Composition and Class Interfaces
 
python.pdf
python.pdfpython.pdf
python.pdf
 
2. Create a Java class called EmployeeMain within the same project Pr.docx
 2. Create a Java class called EmployeeMain within the same project Pr.docx 2. Create a Java class called EmployeeMain within the same project Pr.docx
2. Create a Java class called EmployeeMain within the same project Pr.docx
 
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdfI am trying to change this code from STRUCTS to CLASSES, the members.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
 
Written by Dr. Preiser 1 CIS 3100 MS Access Assignment .docx
Written by Dr. Preiser 1 CIS 3100 MS Access Assignment  .docxWritten by Dr. Preiser 1 CIS 3100 MS Access Assignment  .docx
Written by Dr. Preiser 1 CIS 3100 MS Access Assignment .docx
 
Sencha Touch - Introduction
Sencha Touch - IntroductionSencha Touch - Introduction
Sencha Touch - Introduction
 
Form demoinplaywithmysql
Form demoinplaywithmysqlForm demoinplaywithmysql
Form demoinplaywithmysql
 
Django Admin: Widgetry & Witchery
Django Admin: Widgetry & WitcheryDjango Admin: Widgetry & Witchery
Django Admin: Widgetry & Witchery
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
HELP IN JAVACreate a main method and use these input files to tes.pdf
HELP IN JAVACreate a main method and use these input files to tes.pdfHELP IN JAVACreate a main method and use these input files to tes.pdf
HELP IN JAVACreate a main method and use these input files to tes.pdf
 
Mysql to mongo
Mysql to mongoMysql to mongo
Mysql to mongo
 
ADBMS ASSIGNMENT
ADBMS ASSIGNMENTADBMS ASSIGNMENT
ADBMS ASSIGNMENT
 

Recently uploaded

WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfryanfarris8
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 

Recently uploaded (20)

WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid Environments
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 

Object Oriented Programming in Python

  • 1. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. OBJECT ORIENTED PROGRAMMING WITH PYTHON
  • 2. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. LET’S DISCUSS  Why OOPs  Classes & Objects  Inheritance  Revisiting Types & Exceptions  Polymorphism  Operator Overloading  Method Overriding  Quick Quiz
  • 3. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. THE CURRENT SCENARIO Let’s store employee data Why OOPs # Ask for the name of the employee name = input("Enter the name of the employee: ") # Ask for the age of the employee age = input("Enter the age of the employee: ") # Print the collected information print(f"Employee Name: {name}") print(f"Employee Age: {age}")
  • 4. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. LET’S IMPROVE IT # Function to input employee data (name and age) def input_employee_data(): name = input("Enter the name of the employee: ") age = input("Enter the age of the employee: ") return name, age # Function to print employee information def print_employee_info(name, age): print(f"Employee Name: {name}") print(f"Employee Age: {age}") name, age = input_employee_data() print_employee_info(name, age) # Ask for the name of the employee name = input("Enter the name of the employee: ") # Ask for the age of the employee age = input("Enter the age of the employee: ") # Print the collected information print(f"Employee Name: {name}") print(f"Employee Age: {age}") Why OOPs
  • 5. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. IS THAT GOOD ENOUGH? # Function to input employee data (name and age) def input_employee_data(): name = input("Enter the name of the employee: ") age = input("Enter the age of the employee: ") return name, age # Function to print employee information def print_employee_info(name, age): print(f"Employee Name: {name}") print(f"Employee Age: {age}") name, age = input_employee_data() print_employee_info(name, age) Why OOPs class Employee: def input_employee_data(self): self.name = input("Enter the name of the employee: ") self.age = input("Enter the age of the employee: ") def print_employee_info(self): print(f"Employee Name: {self.name}") print(f"Employee Age: {self.age}") # Create an instance of the Employee class employee = Employee() # Input employee data using the class method employee.input_employee_data() # Print employee information using the class method employee.print_employee_info()
  • 6. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. PARADIGMS OF PROGRAMMING # Ask for the name of the employee name = input("Enter the name : ") # Ask for the age of the employee age = input("Enter the age of the employee: ") # Print the collected information print(f"Employee Name: {name}") print(f"Employee Age: {age}") # Function to input employee data (name and age) def input_employee_data(): name = input("Enter the name of the employee: ") age = input("Enter the age of the employee: ") return name, age # Function to print employee information def print_employee_info(name, age): print(f"Employee Name: {name}") print(f"Employee Age: {age}") name, age = input_employee_data() print_employee_info(name, age) class Employee: def input_employee_data(self): self.name = input("Enter the name of the employee: ") self.age = input("Enter the age of the employee: ") def print_employee_info(self): print(f"Employee Name: {self.name}") print(f"Employee Age: {self.age}") # Create an instance of the Employee class employee = Employee() # Input employee data using the class method employee.input_employee_data() # Print employee information using the class method employee.print_employee_info() Procedural Functional Object Oriented
  • 7. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. CLASSES Classes are used to create our own type of data and give them names. Classes are “Blueprints”
  • 8. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. OBJECTS Any time you create a class and you utilize that “blueprint” to create “object”.
  • 9. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. CLASSES AND OBJECTS IN ACTION
  • 10. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. CLASSES AND OBJECTS IN ACTION class Employee: def input_employee_data(self): self.name = input("Enter the name of the employee: ") self.age = input("Enter the age of the employee: ") def print_employee_info(self): print(f"Employee Name: {self.name}") print(f"Employee Age: {self.age}") # Create an instance of the Employee class employee1 = Employee() # Create another instance of the Employee class employee2 = Employee() # Yet another instance of the Employee class employee3 = Employee()
  • 11. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. ANATOMY OF A CLASS class Employee: ... Class is a Blueprint
  • 12. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. METHODS IN CLASS # Create an instance of the Employee class employee = Employee() # Input employee data using the class method employee.input_employee_data() class Employee: def input_employee_data(self): self.name = input("Enter the name of the employee: ") self.age = input("Enter the age of the employee: ")
  • 13. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. WHAT IS “self” class Employee: def input_employee_data(self): self.name = input("Enter the name of the employee: ") self.age = input("Enter the age of the employee: ") self refers to the current object that was just created.
  • 14. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. ATTRIBUTES class Employee: def input_employee_data(self): self.name = input("Enter the name of the employee: ") self.age = input("Enter the age of the employee: ")
  • 15. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. SET ATTRIBUTES WHILE CREATING THE OBJECT class Employee: def input_employee_data(self): self.name = input("Enter the name of the employee: ") self.age = input("Enter the age of the employee: ") # Driver Code # Create an instance of the Employee class employee = Employee() # Input employee data using the class method employee.input_employee_data() class Employee: def __init__(self, name, age): self.name = name self.age = age # Driver Code name = input("Enter the name of the employee: ") age = input("Enter the age of the employee: ") # Create an instance of the Employee class employee = Employee(name, age) # Print the employee data print("Employee Name:", employee.name) print("Employee Age:", employee.age)
  • 16. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. LET’S VISUALISE __init__() class Employee: def __init__(self, name, age): self.name = name self.age = age self.name self.age name age # Create an instance employee = Employee(name, age)
  • 17. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. WHERE ARE THESE EMPLOYEE WORKING? class Employee: org = 'CompanyName' def input_employee_data(self): self.name = input("Enter the name of the employee: ") self.age = input("Enter the age of the employee: ") def print_employee_info(self): print(f"Employee Name: {self.name}") print(f"Employee Age: {self.age}") emp = Employee() print(emp.org) Class Variables
  • 18. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. WHAT ABOUT CODE REUSABILITY?
  • 19. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. INHERITENCE class Human: ... class Employee: ... class Human: ... class Employee(Human): ... Inheritance enables us to create a class that “inherits” methods, variables, and attributes from another class.
  • 20. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. INHERITENCE IN ACTION class Human: def __init__(self, height): self.height = height class Employee(Human): def __init__(self, height, name, age): super().__init__(height) self.name = name self.age = age
  • 21. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. YOU ARE ALREADY USING CLASSES https://docs.python.org/3/library/stdtypes.html#str surprise
  • 22. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. EXCEPTIONS BaseException +-- KeyboardInterrupt +-- Exception +-- ArithmeticError | +-- ZeroDivisionError +-- AssertionError +-- AttributeError +-- EOFError +-- ImportError | +-- ModuleNotFoundError +-- LookupError | +-- KeyError +-- NameError +-- SyntaxError | +-- IndentationError +-- ValueError ...
  • 23. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. POLYMORPHISM  Poly – many  Morph - shapes
  • 24. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. OPERATOR OVERLOADING  Some operators such as + and - can be “overloaded” such that they can have more abilities beyond simple arithmetic. class MyNumber: def __init__(self, num=0): self.num = num def __add__(self, other): return self.num * other.num
  • 25. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. HOW IT WORKED?  We override the __add__ method  https://docs.python.org/3/reference/datamod el.html?highlight=__add__#object.__add__
  • 26. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. METHOD OVERRIDING class Human: def __init__(self, name, age): self.name = name self.age = age def introduce(self): print(f"Hello, I am {self.name} and I am {self.age} years old.") class Employee(Human): def __init__(self, name, age, employee_id): super().__init__(name, age) self.employee_id = employee_id # Method overriding def introduce(self): print(f"Hello, I am {self.name}, an employee with ID {self.employee_id}.")
  • 27. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. QUIZ TIME
  • 28. BUSINESS DOCUMENT This document is intended for business use and should be distributed to intended recipients only. THANKS