A Python module is a file that can provide definitions for another program. When it is first imported in an ordinary interpreter session, Python executes its top-level statements. Defining a function makes that function available; it does not by itself run the function’s body.
Top-level demonstration code is different. A print statement or file operation there can run during import, surprising code that only wanted a function. Keeping definitions separate from demonstration or startup actions makes modules easier to reuse and reason about.