Week 1 - Python Foundations

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

Week 1 Gotchas & Pitfalls

Week 1 Assignment: The Data Cleaning Pipeline

Week 1 Glossary

Going Further: Optional Deep Dives

Week 1 Kickoff Slides

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>

<aside> ๐Ÿš€ Try it in the widget: https://lasse.be/simple-hyf-teach-widget/?week=1&chapter=functions_and_modules&exercise=modules_demo&lang=python

</aside>

๐Ÿง  Knowledge Check

Extra reading

Articles

Videos


Next lesson: Type Hints


The HackYourFuture curriculum is licensed underย CC BY-NC-SA 4.0 *https://hackyourfuture.net/*

CC BY-NC-SA 4.0 Icons

Built with โค๏ธ by the HackYourFuture community ยท Thank you, contributors

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