0 votes
1 view
in Python by (47.8k points)

I've seen some code samples and tutorials that use

def main(): # my code here 

if __name__ == "__main__":


But why? Is there any reason not to define your functions at the top of the file, then just write code under it? ie

def my_function() # my code here def my_function_two() # my code here # some code # call function # print(something)

I just wonder if there is any rhyme to the main?

1 Answer

0 votes
by (107k points)
edited by

Yes, you can execute your code without defining any function at the top but for that, you need to import the script as a module.

You just need to write the following mentioned code at the top of your code:-

import module; 


To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 18 in Python by ashely (44.3k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !