that emphasizes the use of procedures, or functions, to break down a program into
smaller, more manageable parts. In procedural programming, the focus is on the
steps that need to be taken to solve a problem, rather than on the data being
manipulated.
Here's an example of procedural programming in Python:
Let's say we want to calculate the area of a rectangle. We could write a procedural
program in Python as follows:
# Function to calculate the area of a rectangle
def rectangle_area(length, width):
return length * width
# Get the length and width of the rectangle from the user
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
# Calculate the area of the rectangle
area = rectangle_area(length, width)
# Print the area of the rectangle
print("The area of the rectangle is:", area)
In this example, we define a function rectangle_area that takes two arguments,
length and width, and returns the product of those arguments, which is the area of
the rectangle. We then get the length and width of the rectangle from the user,
calculate the area, and print the result.
Object-oriented programming, on the other hand, is a programming paradigm that
focuses on the use of objects, which are instances of classes, to represent real-
world entities and their interactions. In object-oriented programming, the focus is
on the data being manipulated, rather than on the steps taken to manipulate it.
Here's an example of object-oriented programming in Python:
Let's say we want to model a bank account. We could write an object-oriented
program in Python as follows:
# Class to represent a bank account
class BankAccount:
def __init__(self, balance=0.0):
self.balance = balance
# Method to deposit money into the account
def deposit(self, amount):
self.balance += amount
return self.balance
# Method to withdraw money from the account
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient funds!")
return None
else:
self.balance -= amount
return self.balance
# Create a bank account with a balance of $1000
account = BankAccount(1000.0)
smaller, more manageable parts. In procedural programming, the focus is on the
steps that need to be taken to solve a problem, rather than on the data being
manipulated.
Here's an example of procedural programming in Python:
Let's say we want to calculate the area of a rectangle. We could write a procedural
program in Python as follows:
# Function to calculate the area of a rectangle
def rectangle_area(length, width):
return length * width
# Get the length and width of the rectangle from the user
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
# Calculate the area of the rectangle
area = rectangle_area(length, width)
# Print the area of the rectangle
print("The area of the rectangle is:", area)
In this example, we define a function rectangle_area that takes two arguments,
length and width, and returns the product of those arguments, which is the area of
the rectangle. We then get the length and width of the rectangle from the user,
calculate the area, and print the result.
Object-oriented programming, on the other hand, is a programming paradigm that
focuses on the use of objects, which are instances of classes, to represent real-
world entities and their interactions. In object-oriented programming, the focus is
on the data being manipulated, rather than on the steps taken to manipulate it.
Here's an example of object-oriented programming in Python:
Let's say we want to model a bank account. We could write an object-oriented
program in Python as follows:
# Class to represent a bank account
class BankAccount:
def __init__(self, balance=0.0):
self.balance = balance
# Method to deposit money into the account
def deposit(self, amount):
self.balance += amount
return self.balance
# Method to withdraw money from the account
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient funds!")
return None
else:
self.balance -= amount
return self.balance
# Create a bank account with a balance of $1000
account = BankAccount(1000.0)