×

python · level 3 · unit 6 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

marks your program's front door.

Every Python file is a module, and every module has a variable called __name__. Run the file directly and __name__ is "__main__". Import it from another file and __name__ is the module's own name, like "prices".

So code under if __name__ == "__main__": runs only when the file is the program itself. Other files can import its functions to reuse or test them, without the program starting up.

Professionals put the work in a main() function and call it from that block. That one call is the : the single, obvious place where the program starts.

def add_vat(pence):
    return round(pence * 1.2)

def main():
    for price in [999, 2450]:
        print(f"{price} -> {add_vat(price)}")

if __name__ == "__main__":
    main()
print("__name__ is", __name__)

In Gwecode your code is always the file being run, so __name__ is always "__main__" here.