Week 1 -Foundational Python

Python Setup

Data Types and Variables

Control Flow

Functions and Modules

Type Hinting

CLI Habits

Errors and Debugging

Logging in Python

File Operations

[Cloud] Azure Setup

Gotchas & Pitfalls

Practice

Assignment

Back to Track

🛠️ Practice

Exercise 1: The Temperature Logger

Concepts: Variables, Functions, Type Hinting, Logging.

You are building a small weather station script. Your task is to write a function that converts Celsius to Fahrenheit, but it must be “production-ready”.

Instructions:

  1. Import the logging module and configure it to level INFO.
  2. Write a function called convert_c_to_f.
  3. Type Hinting: The function should accept a float and return a float.
  4. Inside the function, calculate the result ((celsius * 9/5) + 32).
  5. Logging: Before returning the value, log an info message: "Converting {celsius}°C to {fahrenheit}°F".
  6. Call the function with three different values (e.g., 0, 25, 100).

Exercise 2: The Data Cleaner

Concepts: Lists, Loops, Conditionals, Debugging.

You have received a list of user ages from a database, but the data is dirty. Some values are strings, some are numbers, and some are negative (impossible!).

The Buggy Code: Copy this into exercise_2.py. It currently crashes.

ages = [25, 30, "40", "not_available", 20, -5]

def calculate_average_age(age_list):
    total = 0
    count = 0
    for age in age_list:
        total += age
        count += 1

    return total / count

print(calculate_average_age(ages))

Instructions:

  1. Run the code and read the Traceback. What kind of error is it?
  2. Use the VS Code Debugger to step through the loop. Find which value causes the crash.
  3. Modify the loop to fix the code:
  1. Print the final correct average.

Exercise 3: The Precision Trap

Concepts: Floating Point Math, Debugging, Logic.

You are processing payments for a transaction system. You have a wallet with 0.1 bitcoin, and you receive 0.2 bitcoin. You want to check if you now have exactly 0.3 bitcoin to execute a trade.

The Buggy Code: This code prints “Transaction failed?” even though 0.1 + 0.2 should equal 0.3.

wallet = 0.1
wallet += 0.2

print(f"Wallet balance:{wallet}")

if wallet == 0.3:
    print("Transaction Success!")
else:
    print("Transaction failed?")

Instructions:

  1. Run the code. Notice that the print statement says Wallet balance: 0.3, yet the if check fails. This is confusing!
  2. Set a breakpoint on the if wallet == 0.3: line.
  3. Start the debugger. Hover your mouse over the wallet variable (or look in the Variables pane).
  4. What is the actual value of wallet?
  5. Computers struggle with exact decimal math. Change the code to use the round() function to fix the comparison (e.g., round to 1 decimal place).

Exercise 4: The File Ingestor

Concepts: File I/O, Strings, Context Managers (with).

A big part of Data Engineering is reading data from files. Let’s practice reading a raw text file and processing it.

Instructions:

  1. Create a text file named raw_data.txt in your folder. Add these lines:
amsterdam
rotterdam
the hague
utrecht

  1. Create a python script assignment_6.py.
  2. Use the with open(...) pattern to read the raw_data.txt file.
  3. Loop through the lines and capitalize each city name (e.g., “Amsterdam”).
  4. Store the cleaned names in a list.
  5. Use with open(...) again to write the cleaned names into a new file called processed_data.txt.
  6. Check your folder to see if the new file was created!

Exercise 5: Grade Processor

Concepts: Dictionaries, Type Hinting, Logging, Complex Logic.

Combine everything! You need to process student grades.

Instructions:

  1. Create a dictionary representing a student:
student = {"name": "Alice", "grades": [85, 90, 78]}

  1. Write a function process_student(student_data: dict) -> None.
  2. Configure logging to show timestamps.
  3. Inside the function:
  1. Call the function with Alice’s data.

CC BY-NC-SA 4.0 Icons

*https://hackyourfuture.net/*

Found a mistake or have a suggestion? Let us know in the feedback form.