{"id":19310,"date":"2020-12-02T17:54:29","date_gmt":"2020-12-03T01:54:29","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=19310"},"modified":"2023-12-01T04:05:11","modified_gmt":"2023-12-01T12:05:11","slug":"python-main","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-main\/","title":{"rendered":"Python Main Functions: A How-To Guide"},"content":{"rendered":"\n<p><em>The Python main() method contains the methods and code you want to execute in a main program. It is not necessary because Python runs code line-by-line but the main() method helps make code more readable.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Use Python Main Functions<\/h2>\n\n\n\n<p>\u201cI\u2019ve seen a few tutorials that use <em>main()<\/em> in Python. What does it mean?\u201d That\u2019s a great question. The <em>main()<\/em> function may seem trivial when it\u2019s not actually necessary.\n\n<\/p>\n\n\n\n<p>Coding is all about being concise; you\u2019re probably curious why developers want to add in more lines of code into a program. The <em>main()<\/em> function may add an extra few lines of code into your Python program, but it does serve an important purpose.\n\n<\/p>\n\n\n\n<p>In this guide, we\u2019re going to talk about what the <em>main()<\/em> function is and why it is useful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is the Python main() Function?<\/h2>\n\n\n\n<p>The Python main() function lets you introduce logic into your Python programs. A main() function is executed as soon as a program is run. It defines the order in which particular functions should be executed.<\/p>\n\n\n\n<p>The syntax for the main() function is:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def main():\n\tyourmaincode...<\/pre><\/div>\n\n\n\n<p>The main() function appears at the bottom of a program, after all modules have been imported and functions have been declared.You can call a <a href=\"https:\/\/careerkarma.com\/blog\/python-functions\/\">Python function<\/a> at the bottom of your program and it will run. But, it can be quite confusing that the code at the bottom of a program is the main program. In other programming languages like C++ and Java, all the main code for a program is contained within a main function.<\/p>\n\n\n\n<p>A main() function a good way of breaking down all the code in your programs. People who are new to Python but who know other programming languages like C++ will thank you for adding in a <em>main()<\/em> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Main Function Python: Walkthrough<\/h2>\n\n\n\n<p>There are two parts of a Python main function. The first part is the actual <em>main()<\/em> function which stores the code for our main program. Let\u2019s call our <em>cookies()<\/em> function and print out the statement: \u201cI like cookies.\u201d from within a <em>main()<\/em> function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def cookies():\n\tprint(&quot;Cookies are delicious!&quot;)\n\ndef main():\n\tprint(&quot;I like cookies.&quot;)\n\tcookies()<\/pre><\/div>\n\n\n\n<p>Finally, let&#8217;s call our function at the bottom of our program:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>main()<\/pre><\/div>\n\n\n\n<p>We can now run our code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Cookies are delicious!\nI like cookies!<\/pre><\/div>\n\n\n\n<p>It is clear exactly what is happening in our program: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>Cookies()<\/em> is a function that prints \u201cCookies are delicious.\u201d<\/li><li>When our program is run, our two functions, <em>cookies()<\/em> and <em>main()<\/em>, are defined.<\/li><li>Then we call the <em>main()<\/em> function.<\/li><li>\u201cI like cookies.\u201d is printed to the console.<\/li><li>The <em>cookies()<\/em> function is then called, printing \u201cCookies are delicious!\u201d to the console.<\/li><\/ul>\n\n\n\n<p>Our code is not only cleaner, but it\u2019s also more logical. If you come from another language like Java, you\u2019ll know how useful this is.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Python __name__?<\/h2>\n\n\n\n<p>The Python __name__ statement refers to the name of a script. If you run a program as a module, it will take on the name of the module. Otherwise, __name__ will have the value __main__, which refers to the program you are running.<\/p>\n\n\n\n<p>Before we talk about the __name__ and __main__ statements that are often used together with a main function, we\u2019ve got to talk about __name__. __name__ stores the name of a program.\n\n<\/p>\n\n\n\n<p>When you run a file directly, __name__ is equal to <em>__main__<\/em>. Consider this file called print_name.py:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>print(__name__)<\/pre><\/div>\n\n\n\n<p>We can run our code like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>python print_name.py<\/pre><\/div>\n\n\n\n<p>Our code returns: <em>__main__<\/em>.<\/p>\n\n\n\n<p>Suppose we were to import this as a module into a file called main.py. Let\u2019s create a file called main.py and import the print_name module:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import print_name<\/pre><\/div>\n\n\n\n<p>Let\u2019s run name.py:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>python name.py<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>print_name<\/pre><\/div>\n\n\n\n<p>The code within our print_name.py file is executed because we imported it as a module in our main program. The print_name.py file prints __name__ to the console. Because we imported print_name as a module, the value of __name__ is print_name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is if __name__ == __main__?<\/h2>\n\n\n\n<p>The Python if __name__ == __main__ statements execute the code they contain if you run a file directly. This statement prevents some code from executing if a program is called as a module.<\/p>\n\n\n\n<p>You\u2019ve probably seen something like this in a Python program with a main function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>if __name__ == &quot;__main__&quot;:<\/pre><\/div>\n\n\n\n<p>What does this mean? That\u2019s another good question. In Python, any variable that starts and ends with <em>__<\/em> is a special variable. These are reserved values that have specific purposes in a Python program.<\/p>\n\n\n\n<p><em>__main__<\/em> refers to the scope where your code will execute. When you run a Python file directly, the value of \u201c__name__\u201d is set to \u201c__main__\u201d. However, when you run a file as a module, the value of \u201c__name__\u201d is not \u201c__main__\u201d; it is set to the name of the module.\n\n<\/p>\n\n\n\n<p>This means that the line of code above will evaluate to True when our program is run. But, this line will only evaluate to True if we run our program directly from the command line instead of from a <a href=\"https:\/\/careerkarma.com\/blog\/python-modules\/\">Python module<\/a>. <\/p>\n\n\n\n<p>If we reference our file as a module, the contents of the <a href=\"https:\/\/careerkarma.com\/blog\/python-if-else\/\">Python \u201cif\u201d conditional statement<\/a> will not be executed. Let\u2019s demonstrate how this works with an example.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python if __name__ == __main__ Example<\/h2>\n\n\n\n<p>Let\u2019s create a new Python script called username.py. In this file, we\u2019re going to ask a user to select a username and check if it is more than five characters long. If the username is not longer than five characters, the user will be asked to choose a new username.\n<\/p>\n\n\n\n<p>We\u2019ll start by defining a global variable to store our user\u2019s username:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>username = &quot;&quot;<\/pre><\/div>\n\n\n\n<p>We\u2019re going to define two functions. The first function will ask a user to select a username and check if it is more than five characters long. The second function will print out that username to the Python shell:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def choose_username():\n\tglobal username\n\tusername = input(&quot;Choose a username: &quot;)\n\tif len(username) &gt; 5:\n\t\tprint(&quot;Your username has been selected.&quot;)\n\telse:\n\t\tprint(&quot;Please choose a username greater than five characters long.&quot;)\n\t\tchoose_username()\n\ndef print_username():\n\tprint(username)<\/pre><\/div>\n\n\n\n<p>We\u2019ve used the <a href=\"https:\/\/careerkarma.com\/blog\/python-global-local-variables\/\">Python \u201cglobal\u201d keyword<\/a> so that the contents assigned to the \u201cusername\u201d variable inside the <em>choose_username()<\/em> method will be accessible globally. Now that we\u2019ve defined our functions, we\u2019re going to create a main function which calls these functions:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def main():\n\tchoose_username()\n\tprint_username()<\/pre><\/div>\n\n\n\n<p>We\u2019re then going to add in the <em>if __name__ = \u2018__main__\u2019<\/em> statement. This means that if we run our source file directly, the Python interpreter executes our two functions. If we run it as a module, the contents of our <em>main()<\/em> method will not execute.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>if __name__ == &quot;__main__&quot;:\n\tmain()<\/pre><\/div>\n\n\n\n<p>Let\u2019s run our code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>python username.py<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Choose a username: CareerKarma\nYour username has been selected.\nCareer Karma<\/pre><\/div>\n\n\n\n<p>Our code runs the <em>choose_username()<\/em> function, and then executes the <em>print_username()<\/em> function. If we select a username that is fewer than four characters, we\u2019ll see a response like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Choose a username: CK\nPlease choose a username greater than five characters long.\nChoose a username:<\/pre><\/div>\n\n\n\n<p>We\u2019re prompted to choose another username.\n\n<\/p>\n\n\n\n<p>If we were to import this code as a module, our <em>main()<\/em> function would not run.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The <em>main()<\/em> function is used to separate blocks of code within a Python program. While it\u2019s not necessary to write a <em>main()<\/em> function in Python, writing one is a good way to improve the readability of your code.<\/p>\n\n\n\n<p>Writing a <em>main()<\/em> function is mandatory in languages like Java, on the other hand. A main() function it makes it clear in what order lines of code run within a program. Including a main() function in your program is a good practice for all programmers for this reason.<\/p>\n\n\n\n<p>Now you\u2019re ready to start writing and working with main methods in Python like a professional! For more Python learning resources, check out our <a href=\"https:\/\/careerkarma.com\/blog\/how-to-learn-python\/\">How to Learn Python guide<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"The Python main() method contains the methods and code you want to execute in a main program. It is not necessary because Python runs code line-by-line but the main() method helps make code more readable. How to Use Python Main Functions \u201cI\u2019ve seen a few tutorials that use main() in Python. What does it mean?\u201d&hellip;","protected":false},"author":240,"featured_media":17859,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-19310","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Main Functions: A How-To Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"The Python main function is used to introduce logic into your Python programs. On Career Karma, learn how to use the main function and __main__ variable.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-main\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Main Functions: A How-To Guide\" \/>\n<meta property=\"og:description\" content=\"The Python main function is used to introduce logic into your Python programs. On Career Karma, learn how to use the main function and __main__ variable.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-main\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-03T01:54:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:05:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/kaitlyn-baker-vZJdYl5JVXY-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-main\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-main\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python Main Functions: A How-To Guide\",\"datePublished\":\"2020-12-03T01:54:29+00:00\",\"dateModified\":\"2023-12-01T12:05:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-main\/\"},\"wordCount\":1237,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-main\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/kaitlyn-baker-vZJdYl5JVXY-unsplash.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-main\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-main\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-main\/\",\"name\":\"Python Main Functions: A How-To Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-main\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-main\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/kaitlyn-baker-vZJdYl5JVXY-unsplash.jpg\",\"datePublished\":\"2020-12-03T01:54:29+00:00\",\"dateModified\":\"2023-12-01T12:05:11+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"The Python main function is used to introduce logic into your Python programs. On Career Karma, learn how to use the main function and __main__ variable.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-main\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-main\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-main\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/kaitlyn-baker-vZJdYl5JVXY-unsplash.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/kaitlyn-baker-vZJdYl5JVXY-unsplash.jpg\",\"width\":1020,\"height\":680},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-main\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Main Functions: A How-To Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Main Functions: A How-To Guide | Career Karma","description":"The Python main function is used to introduce logic into your Python programs. On Career Karma, learn how to use the main function and __main__ variable.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/python-main\/","og_locale":"en_US","og_type":"article","og_title":"Python Main Functions: A How-To Guide","og_description":"The Python main function is used to introduce logic into your Python programs. On Career Karma, learn how to use the main function and __main__ variable.","og_url":"https:\/\/careerkarma.com\/blog\/python-main\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-12-03T01:54:29+00:00","article_modified_time":"2023-12-01T12:05:11+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/kaitlyn-baker-vZJdYl5JVXY-unsplash.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-main\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-main\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python Main Functions: A How-To Guide","datePublished":"2020-12-03T01:54:29+00:00","dateModified":"2023-12-01T12:05:11+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-main\/"},"wordCount":1237,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-main\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/kaitlyn-baker-vZJdYl5JVXY-unsplash.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-main\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-main\/","url":"https:\/\/careerkarma.com\/blog\/python-main\/","name":"Python Main Functions: A How-To Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-main\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-main\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/kaitlyn-baker-vZJdYl5JVXY-unsplash.jpg","datePublished":"2020-12-03T01:54:29+00:00","dateModified":"2023-12-01T12:05:11+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"The Python main function is used to introduce logic into your Python programs. On Career Karma, learn how to use the main function and __main__ variable.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-main\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-main\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-main\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/kaitlyn-baker-vZJdYl5JVXY-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/kaitlyn-baker-vZJdYl5JVXY-unsplash.jpg","width":1020,"height":680},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-main\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python Main Functions: A How-To Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=19310"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19310\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/17859"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=19310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=19310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=19310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}