Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
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
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
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
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.
Practice
(1/5)
1. Which statement best describes the main difference between procedural and object-oriented programming in Python?
easy
A. Procedural programming is faster than object-oriented programming in all cases.
B. Procedural programming is only for small programs; object-oriented programming is for large programs.
C. Procedural programming cannot use variables; object-oriented programming can.
D. Procedural programming uses functions and step-by-step instructions; object-oriented programming uses classes and objects.
Solution
Step 1: Understand procedural programming basics
Procedural programming organizes code as functions and instructions executed in order.
2. Which of the following is the correct way to define a class in Python?
easy
A. def MyClass(): pass
B. class MyClass(): pass
C. function MyClass() {}
D. class MyClass[]: pass
Solution
Step 1: Recall Python class syntax
In Python, classes are defined using the keyword class followed by the class name and parentheses.
Step 2: Check each option
class MyClass(): pass uses correct Python syntax. def MyClass(): pass uses def which defines a function, not a class. function MyClass() {} uses JavaScript syntax. class MyClass[]: pass uses invalid brackets.
Final Answer:
class MyClass(): pass -> Option B
Quick Check:
Python classes start with 'class' keyword [OK]
Hint: Classes start with 'class' keyword in Python [OK]
Common Mistakes:
Using def instead of class
Using wrong brackets [] instead of ()
Confusing Python with other languages syntax
3. What will be the output of this Python code?
def greet(name):
return f"Hello, {name}!"
class Person:
def __init__(self, name):
self.name = name
def greet(self):
return greet(self.name)
p = Person("Anna")
print(p.greet())
medium
A. TypeError
B. Hello, name!
C. Hello, Anna!
D. AttributeError
Solution
Step 1: Understand the procedural function greet
The function greet(name) returns the string "Hello, {name}!" with the given name.
Step 2: Understand the Person class and method call
The Person class stores the name and its greet method calls the procedural greet function with self.name. Creating p with name "Anna" and calling p.greet() returns "Hello, Anna!".
Final Answer:
Hello, Anna! -> Option C
Quick Check:
Class method calls procedural function correctly [OK]
Hint: Class method calls function with self.name [OK]
Common Mistakes:
Confusing variable name with string 'name'
Expecting error due to mixing styles
Forgetting to use self.name
4. Identify the error in this code that mixes procedural and object-oriented styles:
class Calculator:
def add(self, a, b):
return a + b
result = Calculator.add(3, 4)
print(result)
medium
A. Missing self argument when calling add method
B. Class Calculator is not defined
C. add method should not return a value
D. print statement syntax error
Solution
Step 1: Understand method call on class vs instance
The add method is an instance method requiring a self parameter. Calling Calculator.add(3, 4) misses the self argument.
Step 2: Correct usage
To fix, create an instance: calc = Calculator() then call calc.add(3, 4). This passes self automatically.
Final Answer:
Missing self argument when calling add method -> Option A
Quick Check:
Instance methods need self, call via instance [OK]
Hint: Call instance methods on object, not class [OK]
Common Mistakes:
Calling instance method directly on class
Ignoring self parameter
Assuming methods are static by default
5. You want to convert this procedural code into an object-oriented style. Which class design correctly encapsulates the data and behavior?
# Procedural code
def area_rectangle(width, height):
return width * height
w = 5
h = 3
print(area_rectangle(w, h))
hard
A. class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
B. class Rectangle:
def area(width, height):
return width * height
C. class Rectangle:
def __init__(self):
pass
def area(self):
return width * height
D. class Rectangle:
def __init__(self, width, height):
return width * height
Solution
Step 1: Identify data and behavior to encapsulate
The procedural code uses width and height as data and area_rectangle as behavior. In OOP, these should be inside a class.
Step 2: Check class options for correct encapsulation
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height stores width and height as instance variables and defines area() method using them. Other options either miss self, lack data storage, or misuse return in constructor.
Final Answer:
class Rectangle with __init__ storing width and height, and area method using them -> Option A
Quick Check:
OOP encapsulates data and behavior in class [OK]
Hint: Store data in __init__, use methods for behavior [OK]