0
0
Pythonprogramming~30 mins

Procedural vs object-oriented approach in Python - Hands-On Comparison

Choose your learning style9 modes available
Procedural vs Object-Oriented Approach in Python
📖 Scenario: You are building a simple program to manage a list of books in a library. Each book has a title and an author. You will first create the program using a procedural approach, then improve it using an object-oriented approach.
🎯 Goal: Build a program that stores book information and prints each book's details. First, use a procedural style with dictionaries and functions. Then, use an object-oriented style with a class and methods.
📋 What You'll Learn
Create a list of dictionaries to store books with exact titles and authors
Create a variable to count the number of books
Use a for loop to print each book's title and author
Define a Book class with title and author attributes
Create Book objects and store them in a list
Use a for loop to print each Book object's details using a method
💡 Why This Matters
🌍 Real World
Managing collections of items like books, movies, or products is common in software. Procedural code works for simple tasks, but object-oriented code helps organize complex data and behavior.
💼 Career
Understanding both procedural and object-oriented programming is important for software development jobs. Object-oriented programming is widely used in real-world applications for better code organization and reuse.
Progress0 / 4 steps
1
Create a list of books using dictionaries
Create a list called books with these exact dictionaries: {'title': '1984', 'author': 'George Orwell'}, {'title': 'To Kill a Mockingbird', 'author': 'Harper Lee'}, and {'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald'}.
Python
Need a hint?

Use square brackets [] to create a list. Each book is a dictionary with keys 'title' and 'author'.

2
Create a variable to count books
Create a variable called book_count and set it to the length of the books list using the len() function.
Python
Need a hint?

Use len(books) to get how many books are in the list.

3
Print each book's details using a for loop
Use a for loop with variable book to iterate over books. Inside the loop, print the book's title and author in this exact format: Title: 1984, Author: George Orwell.
Python
Need a hint?

Use for book in books: to loop. Use an f-string to print the title and author.

4
Create a Book class and print book details using objects
Define a class called Book with an __init__ method that takes title and author parameters and stores them as attributes. Add a method called display that prints the book's title and author in the format: Title: 1984, Author: George Orwell. Create a list called book_objects with three Book objects using the same titles and authors as before. Use a for loop with variable book to call the display method on each object.
Python
Need a hint?

Define a class with __init__ to set attributes. Add a method display to print details. Create objects and store in a list. Loop over the list and call display.