How to use the main function in Python Programming Language

In this article, I have explained about how to use the main function in the python programming language.

main function

There’s a function named main which is considered the entry point of a program. It means, no matter how many functions we…


This content originally appeared on DEV Community and was authored by MakendranG

In this article, I have explained about how to use the main function in the python programming language.

main function

There’s a function named main which is considered the entry point of a program. It means, no matter how many functions we have defined in our program it will find the main function and executes it at the very beginning, and later on other programs will be executed serially that are inside the main function.

As python is an interpreted language, it does not have any main function of any entry points. It simply executes the program from the top to bottom approach. Python provides a special built-in variable named __name__ and in this article, we will explore how we can use the main function as the entry point of a python program and executes it at the very beginning.

Let’s see the below example first where we do not add any functionality and simply define a python function and named it main()

def main():
     print ("Welcome to KCD Chennai!")

print ("Welcome")


# output: Welcome

In the above program, we can see that it only prints the Welcome in the output and does not execute the main function. But we want to execute the main function in our program. To perform this action we need to use the python’s built-in __name__ variable and set the variable value as the main function. See the below code example:

def main():
     print ("Welcome to KCD Chennai!")

if __name__ == "__main__":
    main()

print ("Welcome")


# Output: 
# Welcome to KCD Chennai!
# Welcome

This time the main function has been executed first and then the rest of the code. The main reason behind is that we have used python’s built-in variable and when the if block of code executes it has found the main function and simply returned true as a result, the main function has been executed.

Thanks for reading my article till end. I hope you learned something special today. If you enjoyed this article then please share to your friends and if you know any other ways to use the main function in Python Programming Language or thoughts to share with me then please write in the comment box.


This content originally appeared on DEV Community and was authored by MakendranG


Print Share Comment Cite Upload Translate Updates
APA

MakendranG | Sciencx (2022-05-12T17:58:19+00:00) How to use the main function in Python Programming Language. Retrieved from https://www.scien.cx/2022/05/12/how-to-use-the-main-function-in-python-programming-language/

MLA
" » How to use the main function in Python Programming Language." MakendranG | Sciencx - Thursday May 12, 2022, https://www.scien.cx/2022/05/12/how-to-use-the-main-function-in-python-programming-language/
HARVARD
MakendranG | Sciencx Thursday May 12, 2022 » How to use the main function in Python Programming Language., viewed ,<https://www.scien.cx/2022/05/12/how-to-use-the-main-function-in-python-programming-language/>
VANCOUVER
MakendranG | Sciencx - » How to use the main function in Python Programming Language. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/05/12/how-to-use-the-main-function-in-python-programming-language/
CHICAGO
" » How to use the main function in Python Programming Language." MakendranG | Sciencx - Accessed . https://www.scien.cx/2022/05/12/how-to-use-the-main-function-in-python-programming-language/
IEEE
" » How to use the main function in Python Programming Language." MakendranG | Sciencx [Online]. Available: https://www.scien.cx/2022/05/12/how-to-use-the-main-function-in-python-programming-language/. [Accessed: ]
rf:citation
» How to use the main function in Python Programming Language | MakendranG | Sciencx | https://www.scien.cx/2022/05/12/how-to-use-the-main-function-in-python-programming-language/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.