This week deepens your Python fluency:
- Decorators help you modify or extend function behavior — great for logging, security, and more.
- Abstract Data Structures like stacks and queues teach you how to manage data efficiently.
- You’ll also explore idiomatic Python, learning patterns pros use every day.
These topics build the bridge from intermediate to advanced Python, preparing you for technical interviews and large-scale systems.
A decorator is a function that takes another function and extends or modifies its behavior.
Basic Decorator
def my_decorator(func):
def wrapper():
print("Before the function")
func()
print("After the function")
return wrapper
@my_decorator
def say_hello():
print("Hello!")
say_hello()Decorator with arguments
def repeat(func):
def wrapper(*args, **kwargs):
for _ in range(3):
func(*args, **kwargs)
return wrapper
@repeat
def greet(name):
print(f"Hello, {name}!")
greet("Pythonista")Stack (LIFO)
stack = []
stack.append(1)
stack.append(2)
print(stack.pop()) # 2Queue (FIFO)
from collections import deque
queue = deque()
queue.append(1)
queue.append(2)
print(queue.popleft()) # 1LinkedList (basic custom example)
class Node:
def __init__(self, data):
self.data = data
self.next = None
n1 = Node(1)
n2 = Node(2)
n1.next = n2
print(n1.next.data) # 2- List Comprehensions
- Using
enumerate()
for i, value in enumerate(["a", "b", "c"]):
print(i, value)- Dictionary Comprehensions
squares = {x: x**2 for x in range(5)}-
What is a decorator?
-
What does LIFO stand for?
-
Which Python module supports queue structures?
-
What keyword is used to apply a decorator?
-
True or False:
enumerate()returns (index, value) pairs. -
What is the result of
[x*2 for x in range(3)]?
Instructions: Write a decorator that prints "Starting..." before and "Done." after the function call.
Learning Objective: Understand how decorators wrap and extend functions.
# TODO: Write a decorator that prints "Starting..." before and "Done." after the function call.Instructions: Write a decorator that logs the arguments a function is called with.
Learning Objective:
Use *args and **kwargs in decorators.
# TODO: Write a decorator that logs the arguments a function is called with.Instructions: Use a list to simulate a stack. Push 3 elements, then pop and print each.
Learning Objective: Reinforce stack logic (LIFO).
# TODO: Use a list to simulate a stack. Push 3 elements, then pop and print each.Instructions:
Use deque to implement a simple queue. Enqueue 3 values, then dequeue all.
Learning Objective: Reinforce queue logic (FIFO).
# TODO: Use `deque` to implement a simple queue. Enqueue 3 values, then dequeue all.Instructions:
Print the index and value of each item in a list using enumerate().
Learning Objective: Write clean, idiomatic loops.
# TODO: Print the index and value of each item in a list using `enumerate()`.Instructions: Create a dictionary where keys are numbers 1-5 and values are their cubes.
Learning Objective: Learn dictionary comprehensions.
# TODO: Create a dictionary where keys are numbers 1-5 and values are their cubes.- You wrote decorators to enhance or modify function behavior.
- You practiced custom data structures like stacks, queues, and linked lists.
- You used idiomatic Python constructs like
enumerate()and comprehensions. - You’re now comfortable writing clean, expressive, and reusable code.
These patterns will empower your final capstone project and any advanced Python work.
- https://codingnomads.com/course/python-programming-301
- Section 7) Decorators
- Section 8) Abstract Data Structures
- Section 9) Idiomatic Data Structures
Complete the following exercises -- you can, of course, do them all:
- 07_decorators
- 07_01_quote_wrap.py
- 07_03_text_decoration.py
- 07_05_decorated_logging.py
- 08_data_structures
- 08_01_stack.py
Finally, start planning and working on a capstone project for the course. Your final project should demonstrate what you've learned:
- A real-world Python app
- Uses an external API
- Stores data in a SQL database
- Interacts via command-line or simple interface
- Optional: include testing & decorators!