Mastering Programming Paradigms in Python
3 min readFeb 12, 2023
Paradigm can also be termed as a method to solve some problems or do some tasks. A programming paradigm is an approach to solve the problem using some programming language or also we can say it is a method to solve a problem using tools and techniques that are available to us following some approach. There are lots of programming languages that are known but all of them need to follow some strategy when they are implemented and this methodology/strategy is paradigms. Apart from varieties of programming languages, there are lots of paradigms to fulfill each and every demand. Python is a multi-paradigm programming language. Object-oriented programming and structured programming are also fully supported, and many of their features support functional programming and aspect-oriented programming.
Procedural Programming
Is a programming paradigm, derived from structured programming, based on the concept of procedure calls. Procedural programming, also known as routines, subroutines, or functions, simply contains a set of computational steps that must be performed. The first major procedural programming languages appeared around 1957–1964, including Fortran, ALGOL, COBOL, PL/I and BASIC. Pascal and C were published around 1970–1972. Wikipedia
Procedural programming is inline programming using the concept of a top-down approach. In other words, PP can be interpreted by giving instructions in the form of stages of activity to complete an activity depending on related procedures and routines. It’s a shame that this programming model tends to be difficult for people other than the programmers to understand.
The objectives of the PP include:
- Facilitate understanding of the program
- Simplify programs
- As a program maintainer (Maintenance)
- Increase program productivity
- Improve the quality and reliability of the program
# Procedural way of finding sum
# of a list
mylist = [10, 20, 30, 40]
# modularization is done by
# functional approach
def sum_the_list(mylist):
res = 0
for val in mylist:
res += val
return res
print(sum_the_list(mylist))
Object-Oriented Programming
Is a programming paradigm based on the concept of “objects”, which can contain data, in the form of fields, often known as attributes, and code, in the form of procedures, are often known as methods. ” OOP programming languages include C++, C#, Python, PHP, Java JavaScript, Ruby, Perl, Object Pascal, Objective-C, Dar, Scala, Swift, Common Lisp, MATLAB, and Smalltalk. Wikipedia
In OOP everything is an object. For example, an object could be a person. That person will have a name (a property of the object), and will know how to invoke the method. Methods in OOP can be considered as procedures in PP, but here methods belong to certain objects. Another important aspect of OOP is Classes. Classes can be thought of as blueprints for an object.
# class Emp has been defined here
class Emp:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print("Hello, % s. You are % s old." % (self.name, self.age))
# Objects of class Emp has been
# made here
Emps = [Emp("John", 43),
Emp("Hilbert", 16),
Emp("Alice", 30)]
# Objects of class Emp has been
# used here
for emp in Emps:
emp.info()
Fuctional Programming
Is a programming paradigm — a style of building structures and elements in computer programs — that presents computation as evaluating mathematical functions and avoiding state-changing and mutable data. Wikipedia
Functional programming (FP) is about how to pass data from one function to another to get the desired result. In FP, functions are treated as data, meaning you can take them as parameters, return them, build functions from other functions, and build custom functions.
# Procedural way of finding sum
# of a list
mylist = [10, 20, 30, 40]
# modularization is done by
# functional approach
def sum_the_list(mylist):
res = 0
for val in mylist:
res += val
return res
print(sum_the_list(mylist))
Thank you for reading my article, and wait for my next article soon. you can contact me at
Gmail: fikrimuzadi@gmail.com