{"id":4381,"date":"2020-08-14T17:17:38","date_gmt":"2020-08-14T11:47:38","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4381"},"modified":"2020-08-14T17:18:47","modified_gmt":"2020-08-14T11:48:47","slug":"pythons-main-function-with-examples","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/pythons-main-function-with-examples\/","title":{"rendered":"Python&#8217;s main() Function with Examples"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is the main function?<\/h2>\n\n\n\n<p>The first function which is called when you run a program in any programming language is the main function. <a href=\"https:\/\/www.h2kinfosys.com\/courses\/python-online-training\">Python <\/a>specifically does not have the Python&#8217;s main function as it is an <a href=\"https:\/\/www.geeksforgeeks.org\/python-compiled-or-interpreted\/\" rel=\"nofollow noopener\" target=\"_blank\">interpreter language<\/a> but we can create the main function and run it when the python file is run using special variable __name__. Let us first see what is __name__<\/p>\n\n\n\n<p>__name__ is a special variable<\/p>\n\n\n\n<p>Python automatically makes __name__ = \u201c__main__\u201d as you can see below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"434\" height=\"310\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-3.png\" alt=\"main function\" class=\"wp-image-4383\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-3.png 434w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-3-300x214.png 300w\" sizes=\"(max-width: 434px) 100vw, 434px\" \/><\/figure>\n\n\n\n<p>So we will write a condition to check whether __name__ == __main__ then call the Python&#8217;s main function. This checking will make sure that this hello.py is running this script. We will take a deep look into this when we will discuss importing other files than you will be able to see the importance of this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def main():\n\tprint(\"I am main function\")\n\n\nif __name__ == \"__main__\":\n\tmain()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"534\" height=\"386\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-4.png\" alt=\"\" class=\"wp-image-4384\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-4.png 534w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-4-300x217.png 300w\" sizes=\"(max-width: 534px) 100vw, 534px\" \/><\/figure>\n\n\n\n<p>We can call different functions from the main functions. Let\u2019s have a look at the example.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"534\" height=\"386\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-5.png\" alt=\"main functions example\" class=\"wp-image-4385\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-5.png 534w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-5-300x217.png 300w\" sizes=\"(max-width: 534px) 100vw, 534px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding __name__ == \u201c__main__\u201d<\/h2>\n\n\n\n<p>Take a look at the screenshot below. When we run the articles.py file __main__ is set to \u201c__name__\u201d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def function_one():\n\tprint(\"I am first function\")\n\n\nif __name__ == \"__main__\":\n\tfunction_one()\nprint(\"Value in built variable name is:  \", __name__)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"277\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-6.png\" alt=\"understanding main\" class=\"wp-image-4386\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-6.png 624w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-6-300x133.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p>But when we import the articles.py file in secondFile __main__ is set to \u201carticles\u201d<\/p>\n\n\n\n<p>This __main__ function gives us control over when to run the main function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import articles\nprint(\"Hello\")<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"621\" height=\"272\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-7.png\" alt=\"\" class=\"wp-image-4387\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-7.png 621w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/08\/image-7-300x131.png 300w\" sizes=\"(max-width: 621px) 100vw, 621px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the main function? The first function which is called when you run a program in any programming language is the main function. Python specifically does not have the Python&#8217;s main function as it is an interpreter language but we can create the main function and run it when the python file is run [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4394,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[483,1221,1222],"class_list":["post-4381","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-examples","tag-pythons-main-function","tag-what-is-the-main-function"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4381"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4381\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/4394"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}