What is the main() Function in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

The starting point for a program in most programming languages is a particular function called main(). On the other hand, the python interpreter reads each line sequentially from the beginning of the file and lacks a main() function.

In python, we can also set the starting point of the code snippet using the main() function and __name__ property. While compiling the code, the python interpreter checks for some certain special variable, and __name__ is one such special variable, so for calling the main() function as the starting point of the program, __name__ is set to __main__.

A Basic Python main()

For using the main() function in Python code, we can check a condition where __name__ == "__main__" and pass the main () function in the statement of conditional If. So, the code inside the main() function will be the start of the code.

Example:

Below is the small example of main() function in Python:

Output:

It can be seen that the main() function is executed first

Main function as Module

When a Python script is imported as a module, the__name__variable now has the same value as the name of the Python script imported. So main() function in Python can be used as a module

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Example:

Let's take a file namedfile1.py and file2.py and will import file1 in file2.

File1.py:

File2.py: Importing file1 in file2

Output:

After importing file1, it is used as a module and is executed.

Using if Conditional with __name__

we can use the condition if __name__ == "__main__" and conditionally execute the given commands when__name__equals "__main__". Let's take an example of the following:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example:

In this example, we execute the conditional and the given statements.

Output:

Best Practices for Python Main Functions

These best practices can be used with the main() function in Python for making the code more interactive and readable:

  • Put the majority of your code into a function or a class.
  • To control the execution of your code, use __name__.
  • Create a main() function to store the code that you want to run.
  • Use the main function to call additional functions.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Examples

Create a Function Called main() to Contain the Code You Want to Run

We can use the main() function in Python and call it wherever we want in our code, we can check a condition where __name__ == "__main__" and pass the main the main() function in the statement of conditional If. So, the code inside the main() function will be the start of the code.

Example:

Below is a small example of the following:

Output:

It can be seen that the main() function is executed.

Call Other Functions From main()

We can call more functions from the main() function in Python and then use those values for our purposes.

Example:

we are getting values of a and b from funa() and funb() functions.

Output:

Conclusion

  • The starting point for a program is called the main() function.
  • In python, we can also set the starting point of the code snippet using the main() function and __name__ property.
  • When a Python script is imported as a module, the __name__ variable now has the same value as the name of the Python script imported.
  • These best practices can be followed for making the code more interactive and readable:
    • Put the majority of your code into a function or a class.
    • To control the execution of your code, use __name__.
    • Create a main() function to store the code that you want to run.
    • Use the main function to call additional functions.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more