Python Introduction - Session 2 Exercises

ISEG Executive Education - PG AAIML

Author

Diogo Silva

Tips:

Each exercise lists the course units it practises. Exercises named Investment analyst build on each other around the same problem, from session 1 to session 3.

Session 2: U4 – U7

In class: Exercise 8 after lists and loops, Exercise 17 after dictionaries.

Unit Recommended
U4 Lists and tuples 4, 5
U5 Loops 8, 12
U6 Dictionaries and comprehensions 17, 19
U7 Functions 28, 14 (extra challenge)

Exercise 3: Expense Tracker

Units: U4, U5

You’re managing your monthly expenses. Create a program that:

  1. Stores your expenses in a list: [1250.50, 89.90, 45.00, 320.75, 12.50]
  2. Calculates the total spent
  3. Finds the average expense
  4. Counts how many expenses were above 100€

Exercise 4: Training Data Splitter

Units: U4

You have a dataset of 100 samples that need to be split into training (70%), validation (20%), and test (10%) sets. Create a program that:

  1. Creates a list of sample IDs from 0 to 99
  2. Splits them into three sets according to the percentages
  3. Prints the size of each set and first 5 IDs from each

Extra challenge: randomly sample the samples for each set; hint: use the built-in random module.


Exercise 5: Product Inventory

Units: U4, U5, U6

A small shop needs to manage its inventory. Create a program that:

  1. Stores products as tuples: (product_name, quantity, price)
  2. Creates a list with these products: ("Laptop", 5, 899.99), ("Mouse", 23, 15.50), ("Keyboard", 12, 45.00)
  3. Finds products with quantity below x (x is received from user)
  4. Calculates total inventory value
  5. Creates a dictionary mapping product names to their stock quantity

Extra challenge: receive more products with input, in a loop. Stop when product name is something very specific that you define, e.g. “-1”.

Exercise 6: Sales Report Generator

Units: U4, U5

You have daily sales data for a week. Create a program that:

  1. Stores sales in a list: [1200, 1450, 980, 1100, 1650, 890, 1320]
  2. Identifies which days had sales above average
  3. Finds the best and worst sales days

Exercise 7: Batch Data Generator

Units: U4, U5

Simulate creating batches for training. You have 47 samples and need to create batches of size 8.

Create a program that:

  1. Creates sample IDs (0 to 46): range(47)
  2. Splits them into batches of 8 (you must choose the data structure for each batch, and the one that will hold all batches)
  3. Shows how many full batches and the size of the last batch

Exercise 8: Investment analyst – lists and loops

Units: U4, U5

  • Redo Exercise 1 with lists and loops.
  • Now, the user chooses the initial investment value and the number of years to simulate (use input).
  • Your forecaster colleague will give you the prediction for the returns for those years (comma seperated float values).
  • The user should be able to see the evolution of the investment over the years and if it increased or decreased compared to the previous year.
  • Show a formated table with the results, first column is the year, second column is the value in that year.

Exercise 9: Investment analyst – one simulation per party

Units: U4, U5

  • Redo Exercise 2 with lists, as in Exercise 8.
  • A country will have elections. There are K parties relevant parties (which you hardcode in, for example, a list).
  • The winning party will govern for 8 years.
  • The party’s policies will influence the annual return rate (ARR) over some type of investments.
  • A forecaster has provided you with a base ARR (configure this in the code).
  • A political analyst has provided you with how each party might influence the ARR (configure this in code)..
  • The ARR for each party is the base ARR plus the influence of the party.
  • For each party, do a simulation for the evolution of an investment of M€ over 8 years (M is received from the user).
  • Print the final investment for each party, along with the full evolution of the party with the highest investment.
  • Tips
    • The value in each year (including the initial value) must be stored.
      • How will you store this data? A list for each party? A list for each year? Somethine else?
    • You need to iterate over the years for each party, or vice versa. That’s a loop inside a loop.
    • You need to keep track of the investment for each party.

Exercise 10: Investment analyst – volatility

Units: U5

  • Based on Exercise 9, add the following features.
  • Each party may also make the ARR more or less volatile (unstable).
  • The volatility is given by a list of 8 floats (one for each party).
  • The volatility is the standard deviation of the ROI.
  • For each year, each party’s final ARR is a random number with a normal distribution with the mean given by the party’s ARR of the previous year and the standard deviation given by the volatility.
  • Recompute investment for each party.
  • Tips
    • You can generate random numbers with a normal distribution with the numpy library.
    import numpy as np
    mean = 0
    std = 1
    np.random.normal(mean, std) # random number with mean 0 and std 1
    np.random.normal(mean, std, 10)  # list of 10 random numbers with mean 0 and std 1

Exercise 11: Investment analyst – Monte Carlo simulation

Units: U5

  • Based on Exercise 10, add the following features.
  • Because we’re dealing with random events, let’s do a Monte Carlo simulation.
  • Simulate the evolution of the investment 1000 times for each party.
  • Print the average investment and its standard deviation for the parties with the highest final average and the one with lowest standard deviation.
  • For the party with the highest average, include also the 25%, 50% and 75% percentiles.
  • Tips
    import numpy as np
    l = [1,2,3,4,5]
    np.mean(l)  # -> mean
    np.std(l)  # -> standard deviation
    import numpy as np
    l = [1,2,3,4,5]
    np.percentile(l, 25)  # -> 25% percentile

Exercise 12: Income Tax Calculator

Units: U4, U5

Portuguese income tax (IRS) is progressive: income is taxed in brackets.

Write a program that receives the gross annual income and shows the total income tax and the final net income.

The brackets are defined in 3 lists:

bracket_min = [0, 7479, 11284, 15992, 20700, 26355, 38632, 50483, 78834]
bracket_max = [7479, 11284, 15992, 20700, 26355, 38632, 50483, 78834, float("inf")]
rate = [14.5, 21, 26.5, 28.5, 35, 37, 43.5, 45, 48]

Progressive tax means the same income is taxed in different brackets, e.g.

Income: 20000€
Tax in bracket 1: (7479 - 0) * 14.5%
Tax in bracket 2: (11284 - 7479) * 21%
Tax in bracket 3: (15992 - 11284) * 26.5%
Tax in bracket 4: (20000 - 15992) * 28.5%
There is no tax in brackets above 4, because the income is not high enough.

Exercise 13: Learning Rate Scheduler (ML)

Units: U5

Create a learning rate scheduler that reduces the learning rate by 50% every 10 epochs. Start with LR=0.1 and simulate 35 epochs. Track and display the learning rate at each epoch. Your only job is to produce a list with the learning rates of all epochs, i.e. the length of that list is the number of epochs.

Exercise 14: Password Validator

Units: U5

Create a simple password validator that checks if a password:

  1. Is at least 8 characters long
  2. Contains at least one number
  3. Contains both uppercase and lowercase letters

Test with: “Secure123”, “weak”, “NOLOWER1”, “noupper1”

Extra challenge (U7): put the checks in a function is_valid_password(password) that returns True or False.

Exercise 15: Confusion Matrix Calculator

Units: U4, U5

You have predictions from a binary classifier. Create a program that:

  1. Takes actual labels: [1, 0, 1, 1, 0, 1, 0, 0, 1, 0]
  2. Takes predictions: [1, 0, 1, 0, 0, 1, 1, 0, 1, 0]
  3. Calculates True Positives, True Negatives, False Positives, False Negatives
  4. Computes Accuracy, Precision, and Recall
  5. Displays the confusion matrix

Exercise 16: Dataset Preparation

Units: U5

You’re preparing a dataset for training. You have raw data with missing values represented as -999: - Features: [1.2, 3.4, -999, 5.6, 2.1, -999, 4.5, 3.2]

Create a program that:

  1. Counts how many missing values exist
  2. Replaces missing values with the mean of valid values
  3. Normalizes all values to 0-1 range (min-max scaling)
  4. Prints original vs processed data side-by-side

Exercise 17: Employee Database

Units: U6

You’re building a simple employee management system. Create a dictionary with employee IDs as keys and their information as values (this information should also be a dict):

  • Employee 101: Maria Silva, salary 3500, department “Sales”
  • Employee 102: João Santos, salary 3100, department “IT”
  • Employee 103: Ana Costa, salary 2800, department “Sales”

Then:

  1. Print all employees with salary above 2600
  2. Calculate the average salary for the Sales department
  3. Add a new employee (104: Pedro Alves, salary 3500, department “IT”)

Extra challenge: receive employees from the user with input inside a loop, only stop when employee number is a negative number.

Exercise 18: Shopping Cart

Units: U6

Simulate a shopping cart system:

  1. Create a dictionary with products and prices: {"apple": 0.50, "bread": 1.20, "milk": 0.90, "cheese": 2.50}
  2. Create an empty cart (list of tuples: product name and quantity)
  3. Add items to cart: 3 apples, 1 bread, 2 milk (or receive from user)
  4. Calculate total cost
  5. Apply a 10% discount if total is above 5€

Exercise 19: Text Analyzer

Units: U6

Create a program that analyzes a piece of text:

  1. Count total words
  2. Count how many words start with a vowel
  3. Find the longest word
  4. Create a dictionary with word lengths as keys and list of words as values

Test with: “Python programming is powerful and versatile”

Added challenge - Bag of words: count the occurence of each word in a text; test with the text in this link

Added challenge 2 - n-gram counter: instead of words, count the number of times n letters appear together, e.g. “Instituto” leads to trigrams (3-gram) “ins” “nst” “sti” “tit” “itu” “tut” “uto”, but there is no repetition here; make it so n is a parameter, i.e. you can easily change it in a variable, and the rest of the logic remains intact.

Exercise 20: Meeting Scheduler

Units: U4, U6

You’re scheduling meetings for a week. Create a program that:

  1. Stores meeting slots as tuples: (day, hour, duration_minutes, attendees_count)
  2. Create a list with: ("Monday", 10, 60, 5), ("Monday", 14, 30, 3), ("Tuesday", 9, 90, 8), ("Wednesday", 11, 45, 4)
  3. Calculate total meeting hours for the week
  4. Find the busiest day (most total meeting time)
  5. Calculate average number of attendees per meeting
  6. Convert the list to a dictionary, with days as keys and list of meetings as values (sorted chronologically).

Exercise 21: Model Performance Tracker

Units: U6

You’re training multiple models and need to track their performance. Create a system that:

  1. Stores model results as dictionaries with: model_name, accuracy, training_time (minutes), parameter_count
  2. Creates a list with results for: CNN (0.87, 356, 500000), SwinT (0.91, 1200, 1200000), FCN (0.82, 3, 50000), CNN2 (0.85, 65, 200000)
  3. Finds the best model by accuracy
  4. Calculates the average training time in hours
  5. Identifies models with accuracy > 0.85 and training time < 2 hours

Note you might want to revisit this exercise with Exercise 37.

Exercise 22: Feature Engineering Pipeline

Units: U5, U6

You have raw sensor data and need to create features. Given temperature readings: [20.5, 21.3, 22.1, 21.8, 23.2, 24.1, 23.5, 22.9]

Create a program that generates these features:

  1. Moving average (window size 3)
  2. Temperature difference from previous reading
  3. Boolean feature: is_increasing (compared to previous)
  4. Store all features in a dictionary with descriptive keys

Exercise 23: Hyperparameter Grid Generator

Units: U5, U6

You need to create all combinations of hyperparameters for grid search. Generate combinations for:

  • learning rate: [0.001, 0.01, 0.1]
  • batch size: [8, 16, 32, 64]
  • epochs: [10, 20]

Create a program that:

  1. Generates all possible combinations
  2. Counts total combinations
  3. Estimates total training time (assume each takes 5 minutes)
  4. Stores combinations in a list of dictionaries

Extra challenge - parameters with dependencies Consider that this model has some architectural modules which can be easily swapped: a feature extractor and an aggregation head. The feature extractors include resnet18, resnet50, resnet100, resnet152 and ViT. The heads include GeM, NetVLAD and MLP. For resnet100 and resnet152, batch size may not be higher than 8. For ViT, NetVLAD will not work. Redo the exercise with these 2 added parameters, and with the dependencies in mind.

Note: this kind of logic is not supported in most built-in libraries, e.g. Scikit-Learn.

Exercise 24: Label Distribution Analyzer

Units: U6

You have a dataset with class labels. Analyze if there’s class imbalance:

  • Labels: [0, 1, 0, 0, 1, 2, 0, 1, 0, 2, 0, 0, 1, 0, 2, 0, 0, 0, 1, 0]

Create a program that:

  1. Counts samples per class
  2. Calculates the percentage distribution
  3. Identifies if dataset is imbalanced (majority class > 60%)
  4. Suggests whether resampling might be needed
  5. Creates a dictionary mapping class labels to their counts

Extra challenge: sample classes so that number of samples per class is roughly the same. The output sample should be the indeces of the original samples.

Exercise 25: Product Sales Analysis

Units: U4, U6

The code snippet at the bottom, contains 2 lists: 1. the IDs of the products that were transacted in a given day 2. a list of the price of the products

Copy these lists and write code to answer the following questions.

  1. Which product was most transacted?
  2. What is the ID of the cheapest product?
  3. What is the ID of the most expensive product?
  4. What is the average product price?
  5. What is the total revenue for the day?
  6. How many times was product with ID 1 transactioned? And ID 2?
  7. Create a dictionary where the keys are IDs of the products transactioned and the values are the counts of the times that product was transactioned in the day.
  8. Change the values of the dictionary so that now we have a tuple, where the first element is the number of transactions (as before) and the second is the total revenue of that product, e.g. (1, 1013.73) -> not a the real value.
  9. The products with an ID less than 10 are for pet products. How many transactions for pet products?
  10. How much revenue for pet products?
products_ids_day1 = [33, 28, 37, 25, 18, 4, 0, 14, 42, 6, 24, 20, 44, 14, 49, 10, 4, 32, 11, 49, 48, 25, 11, 10, 43, 28, 2, 2, 26, 9, 9, 39, 20, 43, 37, 15, 26, 18, 37, 38, 40, 32, 2, 19, 48, 38, 33, 17, 47, 24, 19, 12, 43, 50, 5, 41, 30, 44, 44, 44, 13, 5, 50, 23, 36, 37, 49, 28, 44, 26, 45, 39, 30, 34, 17, 44, 47, 13, 23, 2, 10, 39, 34, 44, 28, 33, 6, 41, 31, 10, 48, 0, 31, 13, 15, 23, 30, 50, 48, 27, 29, 25, 41, 44, 3, 49, 14, 13, 4, 21, 47, 15, 28, 44, 29, 20, 14, 17, 50, 45, 17, 38, 45, 20, 34, 11, 49, 17, 1, 22, 31, 41, 31, 0, 12, 48, 49, 7, 48, 40, 38, 6, 36, 37, 27, 10, 26, 41, 21, 26, 3, 12, 31, 36, 48, 15, 41, 49, 38, 3, 32, 47, 39, 28, 35, 5, 17, 10, 14, 5, 27, 5, 18, 35, 2, 8, 40, 41, 30, 26, 2, 34, 23, 21, 30, 45, 9, 41, 27, 35, 47, 24, 37, 17, 16, 35, 14, 4, 7, 28]

# value at index 0 has the price of product with ID 1, index 1 -> ID 2, ...
ids_price = [37.73, 615.92, 789.51, 362.33, 159.15, 482.15, 332.99, 264.94, 110.41, 917.21, 3.99, 358.15, 6.37, 15.0, 495.94, 26.12, 202.62, 438.0, 672.33, 369.49, 79.68, 703.73, 93.0, 89.97, 3.34, 101.3, 151.12, 807.89, 2.45, 117.8, 426.9, 33.87, 260.01, 341.21, 74.43, 709.64, 218.8, 851.58, 100.12, 217.5, 35.91, 534.81, 114.68, 189.61, 441.05, 65.25, 172.97, 494.9, 87.64, 524.5]

Exercise 26: Investment analyst – political influence expected value

Units: U6

  • The client has given up on trying to predict which party will be in power each year.
  • Instead, they just want to know the expected value of their investments based on the probability of each party being in power.
  • They are assuming the probabilities will not change over the next N years.
  • The client will provide you with
    • the initial investment value;
    • number of years to simulate.
  • Your colleague, the forecaster, has provided you a base annual return rate.
  • Your colleague, the political analyst, has provided you with
    • the impact factors of different political parties on the annual return rate;
    • the probabilities of each party winning the upcoming election.
  • Your task is to calculate the expected investment value for each year during N years, considering the probabilities of each party being in power, and determine if the investment has increased or decreased compared to the previous year, for each year.

Exercise 27: Investment analyst – to donate or not to donate

Units: U6

  • Your client is considering making a large donation to a political party.
  • They want to understand the potential impact of this donation on their investments.
  • Your colleague, the forecaster, has provided you with the base annual return rate.
  • Your colleague, the political analyst, has provided you with
    • impact factor of each party on the annual return rate;
    • the initial probability of each party winning the upcoming election;
    • the additional probability of each party winning the election if the client makes a donation.
  • Your task is to determine if the client should make the donation based on the expected investment value over the next N years. And, if yes, to which party.
    • For each donation scenario (no donation, donation to party A, B, …), calculate the expected investment value over the next N years.
    • Compare the final value of each scenario.
    • Provide a recommendation to the client based on the comparison.

Exercise 28: Investment analyst – functions

Units: U7

  • Redo exercises from session 2, but make them more modular and flexible by using functions.
  • For example, redo the Monte Carlo simulation (Exercise 11).
  • Make a function that receives the initial investment, base ARR and standard deviation, and makes the simulation for N years.
  • Then make another function that calls the first function 1000 times and computes the average and standard deviation of the final investment.
  • Make sure to test your functions with different inputs.
  • Combine those functions to implement all features from that exercise and implement other functions you think will make the code cleaner and more reusable.