Week 1 - Python Foundations

Overview

Python Setup

Data Types and Variables

Control Flow: Logic and Loops

Functions and Modules

Type Hints for Clearer Code

Command-Line Interface Habits

🐛 Errors and Debugging

📝 Logging in Python

File Operations

Azure Setup and Account Access

🛠️ Practice

🎒 Assignment

⚠️ Gotchas & Pitfalls

🗓️ Lesson Plan

Functions and Modules

Organizing code into functions and modules is essential for building maintainable data pipelines.

Functions Review

You learned functions in Core. Here's what's important for data engineering:

def clean_value(value: str, default: str = "") -> str:
    """Clean and normalize a string value.

    Args:
        value: The string to clean
        default: Value to return if input is empty

    Returns:
        Cleaned string, lowercase and stripped
    """
    if not value:
        return default
    return value.strip().lower()

<aside> 💡 Always write docstrings. They help your future self and teammates.

</aside>

Modules

A module is simply a .py file. You can import functions from it.

# `utils.py`
def clean_value(value):
    return value.strip().lower()

# `main.py`
from utils import clean_value
print(clean_value("  HELLO  "))

The __name__ == "__main__" Pattern

This pattern lets a file work both as a module AND as a script:

# `utils.py`
def clean_value(value):
    return value.strip().lower()

if __name__ == "__main__":
    # Only runs when executed directly
    print(clean_value("  TEST  "))

<aside> ⌨️ Hands-on: Create utils.py with a function, import it in main.py.

</aside>

🧠 Knowledge Check

  1. What is the purpose of a docstring?
  2. What does the if __name__ == "__main__": block allow a script to do?
  3. If you have a file named utils.py with a function clean(), how do you import and use it in main.py?

Extra reading

Articles

Videos


Next lesson: Type Hints


The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0

CC BY-NC-SA 4.0 Icons

*https://hackyourfuture.net/*

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