What does if __name__ == "__main__" do, and what is __name__ set to in each case?
The guard is a comparison against the literal string __main__, and __name__ is a module attribute whose value depends on how the module was loaded.
When you run a file directly, Python executes it as the main module and sets __name__ to the string __main__. When the same file is imported by another module, __name__ is set to the module's own name — greet, for example — so the comparison is False and the guarded block is skipped.
That difference is the entire purpose of the guard. It lets a file contain both a set of definitions that other modules can import and a set of statements that should only run when the file is executed as a program. Without it, importing the module would run the top-level code too, which is why scripts used to print things or start servers the moment they were imported.
The idiomatic shape puts the executable work in a function called main and calls it from inside the guard, so the module stays importable and side-effect free, and there is one obvious place to look for the entry point.
One trap worth mentioning for interviews. If a module in your project is imported under two different names — for example once as package.module and once as module — it will be executed twice, because sys.modules keys on the name. The main guard does not prevent that, because in both cases the module is being imported rather than run. Keeping consistent import paths is what actually solves it.