Complete worked example
python
def greeting(name):
return f"Hello, {name}!"
if __name__ == "__main__":
print(greeting("Ada"))- Reasoning
- The function is reusable and only direct execution prints the demonstration.
Typorithm · easy
Step 1 of 4 · intro card
Separate defining reusable behavior from running a script, and identify the interpreter you actually use.
A Python file can define reusable functions and also provide a script entry point. The worked example defines greeting and prints one demonstration only when the file is run directly. Importing it should make the function available without printing a message or starting unrelated work.
Begin by checking python --version and the executable path in your own environment. A virtual environment selects project dependencies; it does not turn one installed interpreter into another Python release. The companion lab uses only the standard library, so no package installation is needed for its exercises.
Python executes a module body when it is first imported.
Python executes a module body when it is first imported. A function definition creates a callable without running its body immediately. The __name__ guard distinguishes direct script execution from ordinary import, allowing one file to support both paths deliberately.
For direct execution the demonstration prints Hello, Ada followed by a newline. Under import, the output must be empty and greeting remains callable. These are two separate acceptance checks: a correct return value alone would not expose an accidental import-time print.
python
def greeting(name):
return f"Hello, {name}!"
if __name__ == "__main__":
print(greeting("Ada"))The counterexample leaves the print outside the guard.
The counterexample leaves the print outside the guard. Its function still returns correct greetings, so a narrow function-only test can pass while imports produce unwanted output. In larger projects, the same pattern can accidentally start a network request or command when a helper is imported.
Do not solve this by hiding every exception or moving business logic into a long guarded block. Keep a small entry point and reusable functions. Exact project packaging conventions can grow later; this lesson establishes predictable module behavior before introducing extra tools.
python
def greeting(name):
return f"Hello, {name}!"
print(greeting("Ada"))Change the greeting function to return Welcome, followed by the supplied name and a full stop.
Change the greeting function to return Welcome, followed by the supplied name and a full stop. Keep the same direct-execution demonstration for Ada and preserve silent import. The task changes one small output contract rather than asking you to create a project from scratch.
Predict the exact return strings for Ada, Bo and an empty input name. This teaching function simply formats a string; it does not claim to validate a real person's name. Validation rules are introduced later as explicit contracts rather than hidden assumptions.
Step 1: Change the function's returned message. Step 2: Keep the guarded Ada demonstration and its newline.
python
def greeting(name):
# Return the requested Welcome message.
raise NotImplementedError
if __name__ == "__main__":
print(greeting("Ada"))The solution keeps the function and entry-point boundary intact and changes only the requested punctuation and message.
The solution keeps the function and entry-point boundary intact and changes only the requested punctuation and message. Its direct script output is Welcome, Ada followed by a full stop and newline, while importing the file remains silent. The two behaviors are tested independently.
Use a plain script invocation for the demonstration and the function tests for reuse. If a tool uses a different interpreter, record that executable explicitly. A green test on one runtime is evidence for that runtime, not a universal guarantee about every Python installation.
python
def greeting(name):
return f"Welcome, {name}."
if __name__ == "__main__":
print(greeting("Ada"))Explain what runs at import time, what runs only on a function call and what the main guard controls.
Explain what runs at import time, what runs only on a function call and what the main guard controls. This is useful in interviews, command-line tools and test suites. It also prepares you to recognize why a module can have side effects even before any of its functions are called.
The course uses portable Python 3.13+ syntax and reviews current Python 3.15 documentation. New language features are not prerequisites for the first tasks. Next, examine object types and the difference between rebinding a name and modifying a referenced object.
Optional stretch — after the core task works: Extract a small main function while retaining silent import and the same direct-script output.
Computer practice: https://applydjinn.com/typorithm/python-lab-20261105.zip. Run verify.py with your selected Python interpreter, then complete an exercise and check it with --practice followed by the lesson id. The lab uses only the standard library and disposable local fixtures; it never connects to the production database or company feeds.
Separate defining reusable behavior from running a script, and identify the interpreter you actually use.