Procedural Vs. Object Oriented Programming

Procedural programming and Object-Oriented Programming (OOP) are two different programming paradigms that can be used to write programs in Python.

Procedural programming is a traditional programming paradigm that focuses on breaking down a problem into smaller subroutines called functions. In procedural programming, the focus is on procedures that operate on data, and the data is often stored in global variables that can be accessed by any part of the program. Procedural programming is often used to write simple programs or scripts that perform a specific task.

On the other hand, OOP is a programming paradigm that uses objects to model real-world entities. It is based on the concept of encapsulation, inheritance, and polymorphism. In OOP, data and functions that operate on that data are encapsulated into objects. The objects are instances of classes, which are defined by the programmer. Classes define the attributes and methods of the objects, and objects can interact with each other to perform specific tasks.

In Python, the difference between procedural and OOP programming is mainly in the way the code is structured. Procedural programs consist of a series of functions that operate on global data, while OOP programs consist of objects that encapsulate data and methods.

Here is a simple example that demonstrates the difference between procedural and object-oriented programming in Python:

Procedural Programming:

# Function to calculate the area of a rectangle
def calculate_area(length, width):
    area = length * width
    return area

# Function to calculate the perimeter of a rectangle
def calculate_perimeter(length, width):
    perimeter = 2 * (length + width)
    return perimeter

# Main program
length = 5
width = 3

area = calculate_area(length, width)
perimeter = calculate_perimeter(length, width)

print(f"Area of the rectangle: {area}")
print(f"Perimeter of the rectangle: {perimeter}")

Output:

Area of the rectangle: 15
Perimeter of the rectangle: 16

Object-Oriented Programming:

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width
    
    def calculate_area(self):
        area = self.length * self.width
        return area
    
    def calculate_perimeter(self):
        perimeter = 2 * (self.length + self.width)
        return perimeter

# Main program
length = 5
width = 3

rect = Rectangle(length, width)

area = rect.calculate_area()
perimeter = rect.calculate_perimeter()

print(f"Area of the rectangle: {area}")
print(f"Perimeter of the rectangle: {perimeter}")

Output:

Area of the rectangle: 15
Perimeter of the rectangle: 16
Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial