---
description: What is Python main function? Why use def main() function and Python main method with example in this tutorial.
title: Python Main Function &#038; Method Example: Understand def Main()
image: https://www.guru99.com/images/python-main-function.png
---

[Skip to content](#main) 

## What is Python Main Function?

**Python main function** is a starting point of any program. When the program is run, the python interpreter runs the code sequentially. Main function is executed only when it is run as a Python program. It will not run the main function if it imported as a module.

What is the def main() function in Python? To understand this, consider the following example code

### def main() Example 1

def main():
     print ("Hello World!")
print ("Guru99")

[](https://www.guru99.com/images/Pythonnew/Python4%5F1.png)

Here, we got two pieces of print- one is defined within the main function that is “Hello World!” and the other is independent, which is “Guru99”. When you run the function def main ():

* Only “Guru99” prints out
* and not the code “Hello World!”

It is because we did not declare the call **function “if\_\_name\_\_== “\_\_main\_\_”.**

It is important that after defining the main function, you call the code by if\_\_name\_\_== “\_\_main\_\_” and then run the code, only then you will get the output “hello world!” in the programming console. Consider the following code

### def main() Example 2

def main():
    print("Hello World!")

if __name__ == "__main__":
    main()

print("Guru99")

  
Guru99 is printed in this case. 

[](https://www.guru99.com/images/Pythonnew/Python4%5F22.png)

Here is the explanation,

* When Python interpreter reads a source file, it will execute all the code found in it.
* When Python runs the “source file” as the main program, it sets the special variable (\_\_name\_\_) to have a value (“\_\_main\_\_”).
* When you execute the main function in python, it will then read the “if” statement and checks whether \_\_name\_\_ does equal to \_\_main\_\_.
* In Python **“if\_\_name\_\_== “\_\_main\_\_”** allows you to run the Python files either as **reusable modules or standalone programs.**

## The \_\_name\_\_ variable and Python Module

To understand the importance of \_\_name\_\_ variable in Python main function method, consider the following code:

def main():
    print("hello world!")

if __name__ == "__main__":
    main()

print("Guru99")

print("Value in built variable name is:  ",__name__)

[](https://www.guru99.com/images/2/python-main-function-new-03.png)

Now consider, code is imported as a module

import MainFunction

print("done")

[](https://www.guru99.com/images/2/python-main-function-new-04.png)

**Here, is the code explanation:**  

### RELATED ARTICLES

* [ Django Tutorial for Beginners: Features, Architecture & History ](https://www.guru99.com/django-tutorial.html "Django Tutorial for Beginners: Features, Architecture & History")
* [ Python Counter in Collections with Example ](https://www.guru99.com/python-counter-collections-example.html "Python Counter in Collections with Example")
* [ BEST Python Certification Exam in 2026 ](https://www.guru99.com/python-programming-certification.html "BEST Python Certification Exam in 2026")
* [ Python Dictionary Append: How to Add Key/Value Pair ](https://www.guru99.com/python-dictionary-append.html "Python Dictionary Append: How to Add Key/Value Pair")

  
Like C, Python uses == for comparison while = for assignment. Python interpreter uses the main function in two ways 

**direct run:**

* \_\_name\_\_=\_\_main\_\_
* if statement == True, and the script in \_main\_will be executed

**import as a module**

* \_\_name\_\_= module’s filename
* if statement == false, and the script in \_\_main\_\_ will not be executed

When the code is executed, it will check for the module name with “if.” This mechanism ensures, the main function is executed only as direct run not when imported as a module.

Above examples are Python 3 codes, if you want to use Python 2, please consider following code

def main():
  print "Hello World!"
  
if __name__== "__main__":
  main()

print "Guru99"

In Python 3, you do not need to use if\_\_name. Following code also works

def main():
  print("Hello World!")
  
main()
print("Guru99")

**Note:** Make sure that after defining the main function, you leave some indent and not declare the code right below the def main(): function otherwise, it will give indent error.  

This code is editable. Click Run to Execute   

  
#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

 You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search 

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/python-main-function.png","url":"https://www.guru99.com/images/python-main-function.png","width":"476","height":"319","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/learn-python-main-function-with-examples-understand-main.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/python","name":"Python"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/learn-python-main-function-with-examples-understand-main.html","name":"Python Main Function &#038; Method Example: Understand def Main()"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/learn-python-main-function-with-examples-understand-main.html#webpage","url":"https://www.guru99.com/learn-python-main-function-with-examples-understand-main.html","name":"Python Main Function &#038; Method Example: Understand def Main()","dateModified":"2024-08-12T16:16:22+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/python-main-function.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/learn-python-main-function-with-examples-understand-main.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/logan","name":"Logan Young","description":"I'm Logan Young, an expert in Python, providing top-tier tutorials and guides to enhance your coding skills and streamline your learning journey.","url":"https://www.guru99.com/author/logan","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/logan-young-author.png","url":"https://www.guru99.com/images/logan-young-author.png","caption":"Logan Young","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"@type":"Article","headline":"Python Main Function &#038; Method Example: Understand def Main()","keywords":"python","dateModified":"2024-08-12T16:16:22+05:30","articleSection":"Python","author":{"@id":"https://www.guru99.com/author/logan","name":"Logan Young"},"publisher":{"@id":"https://www.guru99.com/#organization"},"description":"What is Python main function? Why use def main() function and Python main method with example in this tutorial.","name":"Python Main Function &#038; Method Example: Understand def Main()","@id":"https://www.guru99.com/learn-python-main-function-with-examples-understand-main.html#richSnippet","isPartOf":{"@id":"https://www.guru99.com/learn-python-main-function-with-examples-understand-main.html#webpage"},"image":{"@id":"https://www.guru99.com/images/python-main-function.png"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/learn-python-main-function-with-examples-understand-main.html#webpage"}}]}
```
