Programming paradigms in Python
In this tutorial, we learn about different programming paradigms that are supported by Python.
There are three programming paradigms that are supported by Python:
- Procedural programming
- Object-Oriented Programming
- Functional programming
Procedural programming
Procedural programming simply contains a series of computational steps, these steps instruct the computer on how to solve a task in logical steps. Procedural programming is best for simple programs but is better is use other programming paradigms for solving complex programs.
Advantages:-
- Best for general-purpose programming.
- Reusability of the code.
- It is easy to trace the flow of the program.
Disadvantages:-
- The data is exposed (security issues).
- Difficult to solve real-world problems.
Example:-
def Sum(sample_list): total = 0 for x in sample_list: total += x return total list1 = [10, 200, 50, 70] list2 = [3, 26, 33, 13] print(Sum(list1)) print(Sum(list2))
Output:-
330 75
Object-Oriented Programming
Object-Oriented Programming is one of the most popular approaches to solve a programming problem, it is done by creating objects.
An object has two characteristics:-
- attributes
- behaviors
For example, an object could represent an employee with attributes such as name, title, experience, etc., with behaviors like working, on-leave, underperformed, etc., The four principles of object-oriented:
- encapsulation
- abstraction
- polymorphism
- inheritance
Advantages:-
- It can relate to real-world entities.
- Code Reusability
- Data hiding
Disadvantages:-
- Complex Design
- Large size
- Slow Speed
Examples:-
class animal: species = "bird" def __init__(self, name, age): self.name = name self.age = age parrot = animal("parrot", 10) sparrow = animal("sparrow", 15) print("parrot is a {}".format(parrot.__class__.species)) print("sparrow is also a {}".format(sparrow.__class__.species)) print("{} is {} years old".format( parrot.name, parrot.age)) print("{} is {} years old".format( sparrow.name, sparrow.age))
Output:-
parrot is a bird sparrow is also a bird parrot is 10 years old sparrow is 15 years old
Functional programming
Functional programming is a programming paradigm in which we use functions as the main building blocks of our program. This paradigm uses the approach of “what to solve” instead of “how to solve”. The main advantage of this approach is that it is easy to use parallel processing because there is no state to consider. However, it is possible is to maintain state in python hence python is also known as an impure functional language.
Advantages:-
- implementing concurrency becomes efficient
- simple to understand
- It uses immutable values, making debugging, and testing easier
Disadvantages:-
- Writing programs is an overwhelming task
- reduce the readability of the code
- reduce performance
Example:-
#Recursive Function to find sum of a list def Sum(list, i, Len, sum): if Len <= i: return sum sum += list[i] sum = Sum(list, i + 1, Len, sum) return sum sample_list = [10, 20, 30, 40] sum = 0 Len = len(sample_list) print(Sum(sample_list, 0, Len, sum))
Output:-
100
Conclusion
There is another question that which paradigm is best? There isn’t any clear answer to that question, if the program just uses a concrete set of steps then it is recommended to use procedural, if the program has real-world analogy then the answer is OOP. So basically it depends on the program and accordingly you have to select the best approach.
Thank you for reading this tutorial. I hope it helps you.
- Also, you may visit:
Types of Python Inheritance
Leave a Reply