Exploring Programming Paradigms: Python in Action
Learning objective
Exploring Programming Paradigms: Python in Action
Learning objective
Learning Objective
At the end of the lesson, you'll understand different programming paradigms with Python examples and real-life analogies.
What do you already know about programming paradigms?
What is a Programming Paradigm?
A programming paradigm is a style or way of programming. It influences how you think about and write code.
Introduction to Procedural Programming
Procedural programming focuses on procedures or routines. It's about writing sequences of instructions.
Python Example: Procedural
def greet(name): print('Hello', name) greet('Alice')
Real-Life Example: Procedural
Cooking a recipe: Follow step-by-step instructions to prepare a dish.
Introduction to Object-Oriented Programming
OOP is based on objects that combine data and functionality. It emphasizes reusability and organization.
Python Example: OOP
class Dog: def bark(self): print('Woof!') buddy = Dog() buddy.bark()
Real-Life Example: OOP
A car: It has properties like color and methods like drive.
Introduction to Functional Programming
Functional programming treats computation as the evaluation of mathematical functions, avoiding changing states.
Python Example: Functional
def square(x): return x * x print(square(5))
Real-Life Example: Functional
Mathematical formulas: Like f(x) = x^2, they return results without side effects.
Introduction to Declarative Programming
Declarative programming expresses logic without describing control flow. It's about what, not how.
Python Example: Declarative
print(sorted([3, 1, 2]))
Real-Life Example: Declarative
Ordering at a restaurant: You state your order, not how it's prepared.
Comparison of Paradigms
Different paradigms suit different problems. Choosing the right one is key.
Advantages of Each Paradigm
Each paradigm offers unique benefits, from code organization to simplicity.
Coding Challenge: Procedural
Write a Python function that calculates the factorial of a number procedurally.
Coding Challenge: OOP
Create a Python class for a Book with title and author attributes.
Coding Challenge: Functional
Use a lambda function to double a list of numbers.
Recap and Questions
Review paradigms and examples. Any questions?
Write down 3 things you learned in this lesson.
Write down 2 things you want to know more about.
Ask 1 question about something you haven't quite understood yet.