A Complete Overview of Python Main Function with Examples:
Python File Handling was explained in detail in our previous tutorial in the series of Free Python tutorials.
This tutorial will explain to you all about the main function in Python with hands-on examples.
What is the Main Function in Python?
There is a special function in Python that helps us to invoke the functions automatically by operating the system during run-time or when the program is executed, and this is what we call as the main function.
Even though it is not mandatory to use this function in Python, it is a good practice to use this function as it improves the logical structure of the code.
Let’s see everything in more details.
Table of Contents:
What is a Function?
A function is a block of code which is used to perform some action, and it is also called as reusable code. A function provides higher modularity and code re-usability.
What is the Main Function?
If you observe or if you would have worked in other programming languages like C, C++, C#, Java etc. all these programming languages require the main function to execute the program and without it, we can’t execute a program.
But it is not mandatory or necessary in python language, we can execute a python program with or without the use of the main function.
Python Main Function
As Python is an interpreted language, it follows a top-down approach. Just because python is interpreted there is no static entry point to the program and the source code is executed sequentially and it doesn’t call any methods unless you manually call it.
The most important factor in any programming language is the ‘modules’. The module is a program that can be included or imported to the other programs so that it can be reused in the future without writing the same module again.
However, there is a special function in Python that helps us to invoke the functions automatically by operating the system during run-time or when the program is executed, and this is what we call as the main function.
Even though it is not mandatory to use this function in Python, it is a good practice to use this function as it improves the logical structure of the code.
Let’s see an example without the main function.
Example 1:
print(“Good Morning”) def main(): print(“Hello Python”) print(“Good Evening”)
Output:
Good Morning
Good Evening
If we observe the above program, it has printed only ‘Good Morning’ and ‘Good Evening’ and it did not print the term ‘Hello Python’ which is because we didn’t call it manually or we didn’t use the python’s main function here.
Output:
Now let’s see the program with function call if __name__ == “__main__”.
Example 2:
print(“Good Morning”) def main(): print(“Hello Python”) print(“Good Evening”) if __name__ == “__main__”: main()
Output:
Good Morning
Good Evening
Hello Python
Output:
If you observe the above program you may get a question – why Hello Python is printed? That’s because we are calling the main function at the end of the code, hence it prints ‘Good Morning’ first, ‘Good Evening’ next and ‘Hello Python’ at the end.
If you observe the below program you will get a still more clear picture.
Example 3:
print(“Good Morning”) def main(): print(“Hello Python”) if __name__ == “__main__”: main() print(“Good Evening”)
Output:
Good Morning
Hello Python
Good Evening
Output:
What is if __name__ == “__main__” ?
As discussed earlier, Python is an interpreted programming language and the interpreter starts executing the code as soon as the program is executed.
During this time, the interpreter sets so many implicit variables, and one of them is __name__ and __main__ is the value which is set to the variable. Remember, that we have to define a function for python main function and by using if __name__ == “__main__” we can execute the function.
When the interpreter reads the line if __name__ == “__main__”, then it encounters if statement as if is a conditional statement and it checked the condition whether implicit variable __name__ is equal to the value __main__.
If you consider any other programming languages like C, C++, Java, etc. we have to write the main function as main itself as it is a common standard. But Python is very flexible and it allows to keep any name for the main function, however, it is a good practice to keep the name as the main() function.
Let’s see an example of it!!
Example:
print(“Apple”) def my_main(): print(“Mango”) if __name__ == “__main__”: my_main() print(“Orange”)
Output:
Apple
Mango
Orange
Output:
The above program is executed as expected, but it is a good practice to use the my_main() function as a main() function so that it would be very easy to understand.
Note: When you include this statement if __name__ == “__main__” in the program, it tells the interpreter that it should always be executed as a standalone program only, and you cannot execute this program if it is imported as a module.
Example:
#name of the file main_function.py
print(“Good Morning”) print(“Value of implicit variable __name__ is: ”, __name__) def main(): print(“Hello Python”) print(“Good Evening”) if __name__ == “__main__”: main()
Output:
Good Morning
Value of implicit variable __name__ is: __main__
Good Evening
Hello Python
Output:
Importing Python Main function
Calling a function from another program
Before we get into the concept of importing the main function as a module, let us first understand how to use the functions present inside one program into another program.
Example 1:
#name the file as test.py
def my_fun(a, b): c = a+b print(“Sum of a and b is: ”, c)
#name the file as test1.py
import test test.my_fun(2, 3) print(“Done”)
Run the file test1.py
Output:
Sum of a and b is: 5
Done
Output:
We can also import the main function present in one program into another program as a module.
If you observe in the above code, it prints the value of __name__ as “__main__”, but if we import a module from another program it will not be __main__. Let us see it in the below program.
Example 2:
#name of the file python_module.py
import test print(“Hello World”)
Output:
Good Morning
Value of implicit variable __name__ is: test
Good Evening
Hello World
Output:
If we observe the output of the above program the first 3 lines are coming from the test module. If you notice, it didn’t execute the main method of test.py as the value of __name__ is different.
Let’s create 2 python files i.e. test1.py and test2.py
#I will name the file as test1.py
def my_fun(): print(“Apple”) print(“I am in test1 file”) if __name__ == “__main__”: print(“test1.py will run as standalone”) else: print(“test1.py will run only when imported”)
#I will name the file as test2.py
import test1 print(“I am in test2 file”) test1.my_fun() if __name__ == “__main__”: print(“test2.py will run as standalone”) else: print(“test2.py will run only when imported”)
Output:
#now run the test1.py
I am in test1 file
test1.py will run as standalone
#now run the test2.py
I am in test1 file
test1.py will run only when imported
I am in test2 file
Apple
test2.py will run as standalone
Output:
Conclusion
Hope this tutorial has briefed you all about main function in Python.
The main function is mandatory in programs like C, Java, etc, but it is not necessary for python to use the main function, however it is a good practice to use it.
If your program has if __name__ == “__main__” statement then the program is executed as a standalone program.
Check out our upcoming tutorial to know more about the most commonly asked Python Interview questions!!