Learn practical skills, build real-world projects, and advance your career

Programming - Procedural vs. Object-Oriented


Procedural Approach

  • Procedural programming is where a program is divided into elements called functions
  • Procedural programming uses a top-bottom approach
# Example of a function
def add(a, b):
    # Add the two numbers
    total = a + b 
    return total

# Call the function
add(1, 2)

Object-Oriented Approach

  • Object Oriented programming is where a program is divided into elements called objects
  • Object Oriented programming follows a bottom-top approach

Objects

  • Objects can be termed as an entity that can store data as we store in variables
  • Objects are entities that have certain characteristics and they can perform certain actions

Objects in general use

  • We have already been using objects
# Using the type() function
x = 42 # Object of class int
print(f'x belongs to {type(x)}')
y = 'Python Community' # Object of class str
print(f'y belongs to {type(y)}')
z = ['a', 'b', 'c'] # Object of class list
print(f'z belongs to {type(z)}')
# We can know about the functions of a class object using the dir() function
print(f'The list class has {len(dir(z))} functions. Below mentioned are a few functions: ')
print(dir(z)[-17:-7])