Programming Paradigms

Learn about the different programming paradigms supported in Python.

A paradigm is defined as an organization principle. It is also known as a model. A programming paradigm/model is a style of building the structure and elements of computer programs. Many programming models exist, such as functional, procedural, object-oriented, and event-driven.

Many languages facilitate programming in one or more paradigms. For example, Python supports functional, procedural, object-oriented, and event-driven programming models. There are situations when functional programming is the obvious choice and other situations where procedural programming is the better choice. Paradigms are not meant to be mutually exclusive. A single program can use multiple paradigms.

Functional programming model

Functional programming decomposes a problem into a set of functions. These functions provide the main source of logic in the program.

Functions take input parameters and produce outputs. Python provides functional programming techniques such as lambda, map, reduce, and filter. These techniques will be discussed later in the course.

In this model, computation is treated as an evaluation of mathematical functions. For example, to get the factorial value of a number, or the nnth Fibonacci number, we can use the following functions:

factorial(n)={1if n = 0nfactorial(n1)if n > 0factorial(n) = \begin{cases}1 & \text{\textbf{if} \textit{n} = 0} \\ n * factorial(n - 1) & \text{\textbf{if} \textit{n} > 0}\end{cases}

fibo(n)={0if n = 01if n = 1fibo(n2)+fibo(n1)if n > 1fibo(n) = \begin{cases}0 & \text{\textbf{if} \textit{n} = 0} \\ 1 & \text{\textbf{if} \textit{n} = 1} \\ fibo(n - 2) + fibo(n - 1) & \text{\textbf{if} \textit{n} > 1}\end{cases}

The output value of a function depends only on its arguments, so calling a function with the same value for an argument always produces the same result. As a result, it is a good fit for parallel execution.

No function can affect other variables (the state remains unaltered).

The functional programming model is often called a declarative programming paradigm, as programming is done with expressions or declarations instead of statements.

Procedural programming model

Procedural programming solves the problem by implementing one statement (a procedure) at a time. Thus, it contains explicit steps that are executed in a specific order.

It also uses functions, but these are not mathematical functions like the ones used in functional programming. Functional programming focuses on expressions, whereas procedural programming focuses on statements.

The statements don’t have values. Instead, they modify the state of some conceptual machine.

The same language expression can result in different values at different times, depending on the global state of the executing program. Also, the functions can change a program’s state.

The procedural programming model is often called imperative programming because it changes state with an explicit sequence of statements.

Object-oriented programming model

This model mimics the real world by creating a mini-world of objects inside the computer.

For example, in a university system, objects can be VC, professors, nonteaching staff, students, courses, semesters, examinations, etc.

Each object has a state (values) and behavior (interface/methods). Objects get state and behavior based on the class they have been created from.

Objects interact with one another by sending messages to each other, i.e., by calling each other’s interface methods.

Event-driven programming model

This model is popularly used for programming GUI applications that contain elements like windows, check boxes, buttons, combo boxes, scroll bars, menus, etc.

When we interact with these elements (such as when clicking a button, moving the scrollbar, or selecting a menu item), events occur, and these elements emit messages. There are listener methods registered with the GUI elements that react to these events.

Since there is no guaranteed sequence in which events may occur (based on how we interact with GUI elements), the listeners should be able to handle them in an asynchronous manner.