
In my first exercise to learn Python, I start with displaying a message.
✅ Step-by-step plan (pseudocode)
- Define a main function.
- Inside main, print
"Hello World". - Add the standard
if __name__ == "__main__":entry point. - Call
main().
1, 2, and 4 are standard across most languages, aside from possibly a main function. Remembering my class on Haskell, specifically. That language is a completely different mindset.
Step 3 feels unusual. Why is it done this way? It turns out this is a side effect of Python being used for scripts/libraries for other programs as well as direct programs. For this simple example, it separates this program from being used as a library. While the consequences of a simple print line running out-of-place may be small, it seems a good practice, going forward.
✅ Finally, the complete Python code
# file: hello_world.py
def main():
print("Hello World... anticlimactic, right?") # why: main entry prints greeting
if __name__ == "__main__":
main() # why: ensures script runs only when executed directly
It’s such a small program, but that is typical for this, and why this is often a 1st step in learning a new language.
Next step: modularization