- main
- def
The main function - called
In this example I added 3 lines to the previous file. The line main() calls the main function. We sometimes also say "runs the function" or "invokes the function".
In this context they mean the same.
The two print-statements are not necessary to call the function, I only added them so it will be easy to see the order of the operations that you can observe by looking at the output.
examples/basics/hello_world.py
def main(): print("Hello") print("World") print("before") main() print("after")
before Hello World after
- Use a main function to avoid globals and better structure your code.
- Python uses indentation for blocks instead of curly braces, and it uses the colon : to start a block.