Python Main Function with Examples: Understand __main__

Like C, Python uses == for comparison while = for assignment. Python interpreter uses the main function in two ways

  • import: __name__= module’s filename

    if statement==false, and the script in __main__ will not be executed

  • direct run:__name__=__main__

    if statement == True, and the script in _main_will be executed

  • So when the code is executed, it will check for module name with “if.”

It is important that after defining the main function, you call the code by if__name__== “__main__” and then run the code, only then you will get the output “hello world!” in the programming console as shown below.

top
X
Glad to have you at Our Website
Welcome to WPBot
wpChatIcon