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