University Setif 1 - Ferhat Abbas Master Degree “M1”
Faculty of Technology Semester 1
Department of Process Engineering 2025/2026
Advanced Programming in Python
Practical Work N° 4
Structured Procedural Programming
Recap
In Python, a function is a reusable block of code designed to perform a specific task. Functions
improve readability, reduce duplication, and make programs easier to maintain.
• Functions allow code to be organized into smaller, logical units.
• A function is declared using the def keyword;
• Functions can take parameters that allow them to work with different inputs;
• A function may return a single value, multiple values, or nothing at all using the return keyword.
def add(a, b):
return a + b
• Python allows functions defined in one file (a module) to be imported and used in another script.
import my_module
my_module.my_function()
Exercise 1. Building a File Handling Module
Create a Python module named file_handler.py that contains a collection of reusable functions to
simplify common operations on text files.
Then create a second script, [Link], which imports and tests each function.
Module Requirements: file_handler.py
Implement the following functions:
1. read_file(filename) 📁 Project Structure
Returns the full content of the file as a string. PW4/
Returns None if the file does not exist. │── file_handler.py
└── [Link]
2. write_file(filename, content)
Writes the provided content to the file, overwriting existing content.
3. append_to_file(filename, content)
Appends content to the end of the file.
4. count_lines(filename)
Returns the number of lines in the file.
5. count_words(filename)
Returns the number of words contained in the file.
6. search_in_file(filename, keyword)
Returns a list of lines containing the keyword (case-insensitive).
7. file_exists(filename)
Returns True if the file exists, otherwise False.
8. delete_file(filename)
Deletes the file from the file system.
PW N 4 1 Structured Procedural Programming
Exercise 2. Plotting with matplotlib
Create a dashboard with four different plots using matplotlib. The goal is to visualize sales data
and other business metrics using various types of plots:
• Line Plot: Shows the sales trends over the months.
• Bar Chart: Displays the sales for each product category.
• Histogram: Shows the age distribution of your customers.
• Scatter Plot: Shows the relationship between marketing spend and sales.
1. Add titles to each plot.
2. Label axes appropriately (X and Y).
3. Add legends to indicate what each line/bar/scatter point represents.
4. Choose colors and adjust the grid settings for clarity and style.
5. Combine all plots into a single figure with multiple subplots. Use the matplotlib subplot
functionality to organize the plots in a 2x2 grid (2 rows, 2 columns).
Use the following synthetic data:
• months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
• sales = [200, 210, 240, 260, 300, 280, 320, 350, 330, 300, 290, 310]
• categories = ['Electronics', 'Furniture', 'Clothing', 'Food', 'Toys']
• category_sales = [1500, 1200, 950, 1800, 700]
• ages = use a random function to generate 100 random ages between 18 and 70
• marketing_spend = [50, 60, 80, 90, 100, 120, 150, 180, 210, 250, 300, 350]
• sales_prediction = [110.64, 247.23, 334.35, 172.40, 240.04, 320.80, 435.53, 585.09, 581.88,
781.99, 843.81, 1059.65]
PW N 4 2 Structured Procedural Programming