Programming Paradigms in Python


Introduction of Programming Paradigms

Paradigm: A paradigm is a set of ideas or methods or organization principle that help you solve problems. It is also called a model. Programming Paradigm : A programming paradigm/model is a style of building the structure and elements of computer programs. It is a way of thinking about and creating computer programs using certain principles and techniques. There are different programming paradigms like procedural, object-oriented, functional, logic, and event-driven, each with its own way of solving problems. Choosing the right paradigm can make a big difference in how well a program works. Python supports below Programming Paradigms 1. Object-Oriented Programming (OOP) Paradigm. 2. Procedural Programming Paradigm. 3. Functional Programming Paradigm. 4. Event-driven programming model 1. Object-Oriented Programming (OOP) Paradigm Description:Object-oriented programming (OOP) is a way of writing computer programs. It helps you organize your code by structuring it to match real-world scenarios. OOP combines the data (representing an entity’s properties) and the actions (functions) that work on that data into a single unit called a class. A class is like a blueprint, and the actual instance you create from it is called an object. This method makes the code easier to organize, reuse, and understand. Programs designed using OOP are simpler to build and maintain. # Class and objects # Class with arguments class Contacts: contact_list = [] def __init__(self, name, contact_number): self.name = name self.contact_number = contact_number def show_contact_number(self): print(f"Contact number of {self.name}: {self.contact_number}") def add_contact(self): self.contact_list.append({"name":self.name, "contact_number": self.contact_number}) def show_contact_list(self): for cnt in self.contact_list: print(cnt) print(f"{cnt.get("name")}: {cnt.get("contact_number")}") contact_object = Contacts("Alice", "9876543210") contact_object.add_contact() contact_object.show_contact_number() contact_object = Contacts("Bob", "9866543321") contact_object.add_contact() contact_object.show_contact_number() contact_object.show_contact_list() 2. Procedural Programming Paradigm Description: We have been thinking in terms of variables and functions. This is what is called the procedural programming paradigm. It focuses on functions and follows a top-down approach by breaking down problems into smaller functions. The functions are then called in a manner such that the problem is solved. An important characteristic of procedural programming is that it treats data and functions as separate entities. # 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)) 3. Functional Programming Paradigm Description: Functional programming paradigms is a paradigm in which everything is bound in pure mathematical functions style. It is known as the declarative paradigm because it uses declarations overstatements. It uses the mathematical function and treats every statement as a functional expression as an expression is executed to produce a value. Lambda functions or Recursion is a basic approach 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 makes the code more readable and understandable. 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. # Using a lambda (anonymous function) numbers = [1, 2, 3, 4, 5] squared = list(map(lambda x: x**2, numbers)) print(squared) # Output: [1, 4, 9, 16, 25] def factorial(n): if n == 0 or n==1: return 1 else: return n*factorial(n-1) factorial(5) 4. Event-driven programming model Description: This model is popularly used for programming GUI applications that contain elements like windows, check boxes, buttons, combo boxes, scroll bars, menus, etc.
Benefits of Using Object-Oriented Programming (OOP)
Modularity: OOP promotes modularity by allowing developers to break down complex systems into smaller, manageable components known as objects. Each object encapsulates data and functionality, making it easier to understand, develop, and maintain the overall system. This separation of concerns enhances collaboration among developers, as they can work on individual modules independently. Reusability: One of the key advantages of OOP is its ability to promote code reusability through inheritance. Developers can create new classes based on existing ones, reducing code duplication and saving time. This means that once a class is created, it can be reused across different parts of a program or even in different projects, significantly lowering development costs and efforts. Scalability: OOP systems are inherently scalable due to their modular nature. As applications grow in complexity, new features can be added without affecting existing code. This flexibility allows developers to extend functionality easily and adapt to changing requirements over time. Maintainability: OOP enhances maintainability by organizing code into classes and objects that model real-world entities. This structure makes the code more readable and easier to debug. Changes can be made to specific classes without impacting the entire system, simplifying both updates and troubleshooting efforts
Pillars of OOP in Python
Encapsulation: Encapsulation is the practice of bundling data (attributes) and methods (functions) that operate on that data into a single unit or class. It restricts direct access to some of the object's components, which helps protect the integrity of the data. This principle provides security by preventing unauthorized access and modification. Inheritance: Inheritance allows a new class (subclass) to inherit properties and methods from an existing class (superclass). This mechanism promotes code reuse and establishes a hierarchical relationship between classes. It simplifies code management by allowing developers to extend existing functionality without rewriting code from scratch. Polymorphism: Polymorphism enables objects of different classes to be treated as objects of a common superclass. This means that a single interface can represent different underlying forms (data types). Polymorphism enhances flexibility in programming, allowing for methods to use objects of various types seamlessly. Abstraction: Abstraction involves hiding complex implementation details while exposing only the necessary parts of an object to the user. This simplification helps developers focus on interactions at a higher level without getting bogged down by intricate details. Abstraction is crucial for managing complexity in large systems.