Skip to content
    Hack The FutureHack The FuturePython Notes

    UNIT 03 · TOPIC 09

    Local and Global Variables

    A local variable is created inside a function; global variables live at module level and should be changed sparingly.

    The idea

    Local and Global Variables, simply explained

    A local variable is created inside a function; global variables live at module level and should be changed sparingly.

    01

    See the big picture

    How the idea flows

    Input or goalLocal and Global Va…Python result
    Understand the problemApply Local and Global VariablesRun the codeCheck the output

    Read the diagram as a simple path: begin with a problem, use the Python idea, then verify the result.

    02

    Everyday connection

    Real-life example

    ↗

    A cashier's temporary subtotal is local, while the store's tax rate is shared configuration.

    03

    Learn by doing

    Python code example

    PYTHONExample
    tax_rate = 0.05
    def final_price(price):
        subtotal = price * (1 + tax_rate)
        return subtotal
    print(final_price(200))
    Expected result210.0
    Try it: Change one value in the code, predict what will happen, then run it and compare your answer.
    04

    Keep this in mind

    Key takeaways

    • A local variable is created inside a function; global variables live at module level and should be changed sparingly.
    • Think of a real problem where local and global variables could save time or organize information.
    • Test small changes and read the result carefully before moving to a bigger program.

    Quick self-check

    Can you explain it in your own words?

    What is the main idea of Local and Global Variables?

    A local variable is created inside a function; global variables live at module level and should be changed sparingly.

    Where could you see this idea in everyday life?

    A cashier's temporary subtotal is local, while the store's tax rate is shared configuration.

    Keep learning · Hack The Future