Programming Paradigms in Python
Last Updated :
21 Apr, 2020
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 supports three types of Programming paradigms
-
Object Oriented programming paradigms
-
Procedure Oriented programming paradigms
-
Functional programming paradigms
Object Oriented programming paradigms
In the object-oriented programming paradigm, objects are the key element of paradigms. Objects can simply be defined as the instance of a class that contains both data members and the method functions. Moreover, the object-oriented style relates data members and methods functions that support
encapsulation and with the help of the concept of an
inheritance, the code can be easily reusable but the major disadvantage of object-oriented programming paradigm is that if the code is not written properly then the program becomes a monster.
Advantages
- Relation with Real world entities
- Code reusability
- Abstraction or data hiding
Disadvantages
- Data protection
- Not suitable for all types of problems
- Slow Speed
Example:
Python3
# 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()
Output:
Hello, John. You are 43 old.
Hello, Hilbert. You are 16 old.
Hello, Alice. You are 30 old.
Note: For more information, refer to
Object Oriented Programming in Python
Procedural programming paradigms
In Procedure Oriented programming paradigms, series of computational steps are divided modules which means that the code is grouped in functions and the code is serially executed step by step so basically, it combines the serial code to instruct a computer with each step to perform a certain task. This paradigm helps in the modularity of code and modularization is usually done by the functional implementation. This programming paradigm helps in an easy organization related items without difficulty and so each file acts as a container.
Advantages
- General-purpose programming
- Code reusability
- Portable source code
Disadvantages
- Data protection
- Not suitable for real-world objects
- Harder to write
Example:
Python3
# 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))
Output:
100
Functional programming paradigms
Functional programming paradigms is a paradigm in which everything is bind in pure mathematical functions style. It is known as declarative paradigms because it uses declarations overstatements. It uses the mathematical function and treats every statement as functional expression as an expression is executed to produce a value. Lambda functions or Recursion are basic approaches used for its implementation. The paradigms mainly focus on "what to solve" rather than "how to solve". The ability to treat functions as values and pass them as an argument make the code more readable and understandable.
Advantages
- Simple to understand
- Making debugging and testing easier
- Enhances the comprehension and readability of the code
Disadvantages
- Low performance
- Writing programs is a daunting task
- Low readability of the code
Example:
Python3
# Functional way of finding sum of a list
import functools
mylist = [11, 22, 33, 44]
# Recursive Functional approach
def sum_the_list(mylist):
if len(mylist) == 1:
return mylist[0]
else:
return mylist[0] + sum_the_list(mylist[1:])
# lambda function is used
print(functools.reduce(lambda x, y: x + y, mylist))
Output:
110
Note: For more information, refer to
Functional Programming in Python
Similar Reads
Last Minute Notes (LMNs) - Python Programming Python is a widely-used programming language, celebrated for its simplicity, comprehensive features, and extensive library support. This "Last Minute Notes" article aims to offer a quick, concise overview of essential Python topics, including data types, operators, control flow statements, functions
15+ min read
Getting Started with Python Programming Python is a versatile, interpreted programming language celebrated for its simplicity and readability. This guide will walk us through installing Python, running first program and exploring interactive codingâall essential steps for beginners.Install PythonBefore starting this Python course first, y
3 min read
Python Input Methods for Competitive Programming Python is an amazingly user-friendly language with the only flaw of being slow. In comparison to C, C++, and Java, it is quite slower. In online coding platforms, if the C/C++ limit provided is x. Usually, in Java time provided is 2x, and in Python, it's 5x. To improve the speed of code execution fo
6 min read
8 Tips For Object-Oriented Programming in Python OOP or Object-Oriented Programming is a programming paradigm that organizes software design around data or objects and relies on the concept of classes and objects, rather than functions and logic. Object-oriented programming ensures code reusability and prevents Redundancy, and hence has become ver
6 min read
How to Create a Programming Language using Python? In this article, we are going to learn how to create your own programming language using SLY(Sly Lex Yacc) and Python. Before we dig deeper into this topic, it is to be noted that this is not a beginner's tutorial and you need to have some knowledge of the prerequisites given below. PrerequisitesRou
7 min read
Python Naming Conventions Python, known for its simplicity and readability, places a strong emphasis on writing clean and maintainable code. One of the key aspects contributing to this readability is adhering to Python Naming Conventions. In this article, we'll delve into the specifics of Python Naming Conventions, covering
4 min read
Python OOPs Coding Practice Problems Object-Oriented Programming (OOP) is a fundamental concept in Python that helps structure code for better readability and reusability. This collection of Python OOPs coding practice problems covers everything from defining classes and objects to solving advanced challenges like implementing design p
2 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
API Changes For 3.8.0 in Python In this article, we will explore the changes in the API after the launch of Python 3.8.0. Python, a dynamic and widely-used programming language, continuously evolves with new releases, introducing enhancements and changes to improve developer productivity and code quality. Python 3.8.0, released in
6 min read
Getting Started with Competitive Programming in Python Python is a great option for programming in Competitive Programming. First off, its easy-to-understand and concise grammar enables quicker development and simpler debugging. The huge standard library of Python offers a wide range of modules and functions that can be used to effectively address progr
11 min read