Legends ofPythos
Claim your name
Writing Real Programs

Putting a program together

Lesson 15 of 16

Watch the lesson1:09 · with Sigrid

Putting It Together

Think of your program like a kitchen station. The functions are specialized tools: one chops, one sautés, one plates. main() is the chef who orchestrates them in order. You do not want to chop onions every time someone looks at the knife; you only use it when preparing dinner.
In real projects—like processing daily sales logs or generating report cards—the structure keeps logic testable and reusable. If your code is all jumbled together, changing one price calculation might break the receipt printer. Separation prevents that chaos.

The Bottom-Up Structure

def get_inventory():
    return ["apples", "bananas"]

def display_items(items):
    for item in items:
        print(item)

def main():
    stock = get_inventory()
    display_items(stock)

if __name__ == '__main__':
    main()
Logic lives above; execution happens at the very bottom
Notice that get_inventory only returns data and display_items only prints it, and neither does anything until main() calls it. Keeping each job in its own function makes them predictable: you can call get_inventory in a test without anything being printed.

The Entry Point Guard

Python has a built-in variable named __name__. When you run a file directly, Python sets it to '__main__'. If another script imports your module, __name__ becomes the module's name: the filename without .py. This distinction lets you control when code executes.
def calculate_total(prices):
    return sum(prices)

if __name__ == '__main__':
    total = calculate_total([10, 20])
    print(total)  # Prints: 30
else:
    pass  # Nothing happens when imported
Code under the guard runs only on direct execution

Why This Matters

This pattern scales from a single-file script to a large application package. When your team grows, someone will import your utility functions into tests or other modules. The entry-point guard ensures they get clean, reusable tools—not a program that starts running in the background.
# utils.py
def filter_negatives(numbers):
    return [n for n in numbers if n >= 0]

if __name__ == '__main__':
    print(filter_negatives([1, -2, 3]))  # Prints: [1, 3]
Safe to import; safe to run directly
In your upcoming exercises, you will complete main() functions that orchestrate multiple helpers. Keep the pattern consistent: define pure logic in named functions, then call them from a single entry point at the bottom of the file.

Your turn

0 of 2 solved

Exercise 1

+45 XP
Complete main() so it calls get_inventory, passes the result to display_items, and returns the total count as a string in the form 'Total items: 8'. The data is in INVENTORY.
INVENTORY = {'apples': 5, 'oranges': 3}


def get_inventory():
    return INVENTORY


def display_items(inv):
    for item in inv:
        print(f"{item}: {inv[item]}")


def main():
    pass

Run your code to check it against the tests.

Exercise 2

+45 XP
Finish the program: filter_negatives should drop negative numbers, and main should print Sum = followed by the total of what is left. The __main__ guard at the bottom calls main for you.
DATA = [10, -5, 3, -2, 8]




def filter_negatives(numbers):
    pass




def main():
    pass




if __name__ == "__main__":
    main()

Run your code to check it against the tests.