
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Paradigms in Python
Programming paradigm is a specific approach or style of programming that provides a framework for designing and implementing computer programs. It encompasses a set of principles, concepts, and techniques that guide the development process and the structure of the code. Different paradigms have different ways of solving problems, organizing code, and expressing computations.
The below are the various programming paradigms available in python which makes the developer work easy and more efficient.
Procedural Programming
Procedural programming focuses on dividing a program into a set of procedures or functions. In Python, we can define functions to perform specific tasks and structure our program using procedural techniques.
Example
In this example we are trying to create a function with the name greet() to perform the specific task of greeting the given name.
def greet(name): print("Hello, " + name + "!") name = "Tutorialspoint" greet(name)
Output
Hello, Tutorialspoint!
Object-Oriented Programming (OOPs)
OOPs is a paradigm that organizes code around objects, which are instances of classes. Python fully supports OOPs and provides features such as classes, objects, inheritance, and polymorphism.
Example
In this example we are creating different classes and within the class we are trying to create the functions to implement a specific task.
class Animal: def __init__(self, name): self.name = name def speak(self): raise NotImplementedError("Subclass must implement this method.") class Dog(Animal): def speak(self): return "Woof!" class Cat(Animal): def speak(self): return "Meow!" dog = Dog("Buddy") print(dog.name) print(dog.speak())
Output
Buddy Woof!
Imperative Programming
Imperative programming involves writing code that specifies detailed steps for the computer to follow. Python, as a general-purpose language, supports imperative programming by default.
Example
In this example we are trying to create a program for performing the addition of all the elements in a given list.
numbers = [1, 2, 3, 4, 5] add = 0 for num in numbers: add += num print("Sum:", add)
Output
Sum: 15
Event-Driven Programming
Event-driven programming is commonly used for graphical user interfaces (GUIs) and network programming. Python provides libraries like Tkinter and asyncio that enable event-driven programming.
Example
Here in this example we are creating a GUI button using the tkinter library.
from tkinter import Tk, Button def button_click(): print("Button clicked!") root = Tk() button = Button(root, text="Click me", command=button_click) button.pack() root.mainloop() print("Button created")
Output
Button created
Functional Programming
Functional programming emphasizes immutability and the use of pure functions. Python supports functional programming concepts such as higher-order functions, lambda functions, and list comprehensions.
Example
In this example we are creating a functional program using map, lambda and filter to find the square of a given number in the list and to print the even numbers of the given list.
numbers = [1, 2, 3, 4, 5] squares = list(map(lambda x: x ** 2, numbers)) print("The square of the numbers in the list",squares) even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print("The even numbers from the given list",even_numbers)
Output
The square of the numbers in the list [1, 4, 9, 16, 25] The even numbers from the given list [2, 4]