Python notes
Python notes
In procedural programming, the program is divided into In object-oriented programming, the program is divided
small parts called functions. into small parts called objects.
Adding new data and functions is not easy. Adding new data and function is easy.
Procedural programming does not have any proper way of Object-oriented programming provides data hiding so it
hiding data so it is less secure. is more secure.
In procedural programming, the function is more In object-oriented programming, data is more important
important than the data. than function.
Procedural programming is used for designing medium- Object-oriented programming is used for designing large
sized programs. and complex programs.
Code reusability absent in procedural programming, Code reusability present in object-oriented programming.
Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.
1. Object Oriented Programming Concepts
1. What is Object Oriented Programming
Data Abstraction
It hides unnecessary code details from the user. Also, when we do not want to give out
sensitive parts of our code implementation and this is where data abstraction came.
Data Abstraction in Python can be achieved by creating abstract classes.
2. Encapsulation
Encapsulation
Encapsulation refers to the bundling of attributes and methods inside a single class.
It prevents outer classes from accessing and changing attributes and methods of a class.
This also helps to achieve data hiding.
A class is an example of encapsulation as it encapsulates all the data that is member
functions, variables, etc.
2. Encapsulation
Consider a real-life example of encapsulation, in a company, there are different sections like the
accounts section, finance section, sales section etc.
The finance section handles all the financial transactions and keeps records of all the data related
to finance.
Similarly, the sales section handles all the sales-related activities and keeps records of all the sales.
Now there may arise a situation when due to some reason an official from the finance section
needs all the data about sales in a particular month.
In this case, he is not allowed to directly access the data of the sales section.
He will first have to contact some other officer in the sales section and then request him to give
the particular data.
This is what encapsulation is. Here the data of the sales section and the employees that can
manipulate them are wrapped under a single name “sales section”.
Using encapsulation also hides the data. In this example, the data of the sections like sales,
finance, or accounts are hidden from any other section.
2. Encapsulation
# Main program
# Define the Person class # Create Person objects (instances)
class Person: person1 = Person("Alice", 25)
person2 = Person("Bob", 30)
# Constructor to initialize object
attributes # Access object attributes using methods
def __init__(self, name, age): print(person1.get_name(),"is",
self.name = name person1.get_age()," years old.")
self.age = age print(person2.get_name()," is",
person2.get_age()," years old.")
# Method to get the person's name
def get_name(self): # Update age
return self.name person1.update_age(26)
# Method to get the person's age # Check the updated age
def get_age(self): print(person1.get_name(),"is
return self.age now",person1.get_age())
# Method to update the person's age OUTPUT
def update_age(self, new_age): Alice is 25 years old.
self.age = new_age Bob is 30 years old.
Alice is now 26
2. Encapsulation
class Rectangle:
def __init__(self,l,b):
self.l=l
self.b=b
def area(self):
return self.l*self.b
r1=Rectangle(2,3)
print('Area of first Rectangle',r1.area())
r2 = Rectangle(4,5)
print('Area of second rectangle',r2.area())
OUTPUT
Area of first Rectangle 6
Area of second rectangle 20
3. Polymorphism
3. 1 Constructor Overloading
CONSTRUCTOR OVERLOADING
In Python, constructor overloading refers to the ability to define multiple
constructors (initializers) for a class with different sets of parameters.
It allows you to create objects of a class in various ways, depending on the
parameters provided.
Unlike some other programming languages, Python does not support
explicit constructor overloading like method overloading (where you
can define multiple methods with the same name but different parameter
lists).
Instead, Python uses a different approach to achieve constructor
overloading by utilizing default values and optional arguments.
3. 1 Constructor Overloading
class Const:
def __init__(self,a=0,b=0,c=0):
self.a=a
self.b=b
self.c=c
def sum(self):
return self.a+self.b+self.c
s1 = Const(3,4)
sum2 = s1.sum()
print('sum of 2 numbers',sum2)
s2 = Const(2,3,4)
sum3 = s2.sum()
print('sum of 3 numbers',sum3)
s3 = Const()
sum0 = s3.sum()
print("sum of 0 numbers is ",sum0)
OUTPUT
sum of 2 numbers 7
sum of 3 numbers 9
sum of 0 numbers is 0
3.2 Method Overloading
METHOD OVERLOADING
In Python, method overloading refers to the ability to define multiple
methods in a class with the same name but different parameter lists.
Each method performs a different operation based on the number or type of
arguments passed to it. .
3.2 Method Overloading
class Meth:
def sum(self,x=0,y=0,z=0):
return x+y+z
s1=Meth()
so2=s1.sum(2,3)
so3=s1.sum(1,2,3)
print("sum of 2 numbers is ",so2)
print("sum of 3 numbers is ",so3)
OUTPUT
sum of 2 numbers is 5
sum of 3 numbers is 6
3.2 Method Overloading
class methOverloading:
def add(self, a, b):
return a + b
class ChildClass(ParentClass):
pass
4.1 Types of Inheritance
4) Hierarchical Inheritance:
Hierarchical inheritance involves multiple classes inheriting from the same base class. It creates a
hierarchy of classes derived from a common base.
class ParentClass:
pass
class ChildClass1(ParentClass):
pass
class ChildClass2(ParentClass):
pass
5) Hybrid Inheritance:
Hybrid Inheritance (Mixing Multiple and Multilevel Inheritance): Hybrid inheritance is a combination of
multiple inheritance and multilevel inheritance.
class GrandparentClass:
pass
class ParentClass1(GrandparentClass):
pass
class ParentClass2(GrandparentClass):
pass
def make_sound(self):
pass
# Derived class inheriting from the Animal class
class Dog(Animal):
def __init__(self, name, breed):
# Call the constructor of the base class using the
super() function
super().__init__(name)
self.breed = breed
def make_sound(self):
return "Woof!"
# Create instances of the derived classes
dog = Dog("Buddy", "Golden Retriever")
# Accessing attributes and methods of the base class through the
derived classes
print(dog.name ," is a", dog.breed ," and says ",
dog.make_sound())
def make_sound(self):
pass
def move(self):
print(f"{self.name} is moving.")
class Dog(Animal):
def make_sound(self):
print("Woof!") # Creating objects of the derived classes
dog = Dog("Buddy")
class Cat(Animal): cat = Cat("Whiskers")
def make_sound(self):
print("Meow!") # Calling the methods
dog.make_sound() # Output: Woof!
dog.move() # Output: Buddy is
moving.
The version of a method that is executed will be determined by the object that is used to invoke it.
If an object of a parent class is used to invoke the method, then the version in the parent class will
be executed, but if an object of the subclass is used to invoke the method, then the version in the
child class will be executed
4.4 Method Overriding
method overriding allows you to change or extend the behavior of the
inherited method to suit the needs of the subclass.
Method Overriding
class Animal:
def makeSound(self):
print("Animal makes a sound");
class Dog(Animal):
#Override
def makeSound(self):
print("Dog barks");
d = Dog()
d.makeSound()
Output
Dog barks
5 Object Oriented Design using UML
Object-oriented design (OOD) is a process of designing software systems using the
principles of object-oriented programming.
Unified Modeling Language (UML) is a standard visual representation used to
model and visualize the various aspects of object-oriented systems.
UML provides a set of diagrams to describe different perspectives of a software system,
aiding in the understanding, communication, and design of complex systems.
There are several types of UML diagrams used in object-oriented design. Here are
some of the most commonly used ones:
1. Class Diagram:
Represents the static structure of the system.
Shows classes, their attributes, methods, and relationships between classes.
It is used to visualize the class hierarchy and associations between classes.
2. Object Diagram:
Represents a snapshot of instances of classes and their relationships at a
particular time.
Shows objects and links between objects with their current data values.
5 Object Oriented Design using UML
Activity Diagram to illustrate the activities between user and the system
5 Object Oriented Design using UML
Deployment Diagram