Module 3 Lesson 11: Hands-on Projects
Build a professional Utility Library. Apply everything you've learned about functions, modules, and refactoring to create a suite of reusable tools.
Lesson 11: Module 3 Hands-on Projects
In this final lesson for Module 3, we will combine all our knowledge of functions and modularity to build a Custom Utility Library. This library will contain reusable tools for file management, math, and string manipulation.
Project: Building the "PyBuddy" Utility Library
We will split our library into three files:
helpers.py: Contains our core logic functions.constants.py: Stores our configuration and fixed values.app.py: The user interface that imports and uses our library.
Part 1: constants.py
VERSION = "1.0.0"
AUTHOR = "Python Beginner"
TAX_RATE = 0.08
Part 2: helpers.py (The Logic)
import math
def format_title(text):
"""Capitalizes the first letter of every word."""
return text.title()
def calculate_total(price, quantity=1, discount=0):
"""Calculates price with tax and optional discount."""
subtotal = price * quantity
subtotal -= (subtotal * (discount / 100))
return subtotal * 1.08 # Uses our tax rate logic
def get_circle_info(radius):
"""Returns both area and circumference as a tuple."""
area = math.pi * (radius ** 2)
circ = 2 * math.pi * radius
return (round(area, 2), round(circ, 2))
Part 3: app.py (The Execution)
import helpers
import constants
print(f"Welcome to PyBuddy v{constants.VERSION}")
# 1. Using String Helper
raw_name = "welcome to python programming"
print(f"Formatted: {helpers.format_title(raw_name)}")
# 2. Using Math Helper
area, circ = helpers.get_circle_info(5)
print(f"Radius 5: Area={area}, Circumference={circ}")
# 3. Using Sales Helper
total = helpers.calculate_total(100, quantity=2, discount=10)
print(f"Final Total: ${total}")
Module 3 Recap: Exercises and Quiz
Exercise 1: The Scope Test
Predict the output of this code without running it:
x = 10
def change():
x = 20
change()
print(x)
Exercise 2: Refactor Me
Break this long script into two clean functions: get_input() and process_data().
data = input("Enter numbers separated by space: ").split()
nums = [int(n) for n in data]
avg = sum(nums) / len(nums)
print(f"The average is {avg}")
Module 3 Quiz
1. Which keyword is used to return data from a function?
A) give
B) exit
C) return
D) break
2. What happens to a local variable when the function ends? A) It becomes a global variable. B) It is deleted from memory. C) It remains accessible until the program ends. D) It is saved into a hidden file.
3. What is the correct way to import a specific function from a module?
A) import function from module
B) from module import function
C) get function in module
D) use module.function
4. True or False: Default parameters must come before positional parameters in a definition.
5. What is the main purpose of a docstring? A) To make the code run faster. B) To explain what the function does for other programmers. C) To encrypt the function logic.
Quiz Answers
- C | 2. B | 3. B | 4. False (They must come after!) | 5. B
Conclusion
Congratulations on completing Module 3: Functions and Modular Programming! You no longer write simple scripts—you build systems.
Next up, we move into the single most important concept in modern programming: Module 4: Object-Oriented Programming (OOP). Get ready to think in Objects!