
👉Tutorial-2: How to Call a Function in Python with Examples
👉Tutorial-3: Python Lambda Functions Explained
👉Tutorial-4: Python abs() Function
👉Tutorial-5: Python round() Function Explained
👉Tutorial-6: Python range() Function
👉Tutorial-7: Python map() Function with Examples
👉Tutorial-8: Python timeit() Function with Examples
👉Tutorial-9: Python Yield Keyword Tutorial
👉Tutorial-10: Python Queue
👉Tutorial-11: Python Counter in Collections with Examples
👉Tutorial-12: Python Enumerate() Function
👉Tutorial-13: Python time.sleep() Function
👉Tutorial-14: Python type() and isinstance() Functions
👉Tutorial-15: How to Print Without Newline in Python
👉Tutorial-16: Python Timer Function
👉Python Main Function Explained: Examples and Best Practices
Python Main Function Explained: The Python main function serves as the starting point of a Python program. When you run a Python script, the interpreter executes the code sequentially, starting from the top. The main function is executed only when the script is run directly, not when imported as a module. Python Main Function Explained Let’s explore its usage with examples.
👉What is the def main() Function in Python?
Python Main Function Explained : The def main() function is commonly used to define the main logic of a Python program. However, to ensure it runs only when the script is executed directly, you must include:
python
if __name__ == “__main__”:
main()
This conditional ensures that the code inside main() will run only if the file is executed directly.
👉Example 1: Without if __name__ == “__main__”
python
def main():
print(“Hello World!”)
print(“Software Moji Moji”)
👉Output:
nginx
Software Moji Moji
In this example, “Software Moji Moji” is printed, but “Hello World!” is not. This happens because the main() function was defined but never called.
👉Example 2: With if __name__ == “__main__”
python
def main():
print(“Hello World!”)
if __name__ == “__main__”:
main()
print(“Software Moji Moji”)
👉Output:
nginx
Hello World!
Software Moji Moji
👉Explanation:
- The main() function prints “Hello World!”.
- “Software Moji Moji” prints because it’s outside the main function.
👉Why Use if __name__ == “__main__”?
The __name__ variable is a special Python variable that indicates whether the file is run directly or imported as a module.
- Direct Execution: __name__ == “__main__” (The main block runs)
- Imported as a Module: __name__ equals the module name (The main block doesn’t run)
👉Example 3: Understanding the __name__ Variable
python
def main():
print(“Hello World!”)
if __name__ == “__main__”:
main()
print(“Value in built-in variable __name__ is:”, __name__)
👉Output (When Run Directly):
markdown
Hello World!
Value in built-in variable __name__ is: __main__
👉Output (When Imported as a Module):
csharp
Value in built-in variable __name__ is: MainFunction
👉Example 4: Python 2 Code (For Older Versions)
python
def main():
print “Hello World!”
if __name__ == “__main__”:
main()
print “Software Moji Moji”
Python 2 uses print without parentheses, so ensure compatibility by following the correct syntax.
👉Example 5: Python 3 Code Without if __name__ (Alternative Method)
python
def main():
print(“Hello World!”)
main()
print(“Software Moji Moji”)
👉Python Main Function Explained Output:
nginx
Hello World!
Software Moji Moji
While this method works, using if __name__ == “__main__” is considered best practice for better code organization and module usage.
👉Python Main Function Explained Key Takeaways:
✅ Use def main() to structure your Python programs clearly.
✅ Always include if __name__ == “__main__” to ensure the main logic runs only when intended.
✅ For Python 3, prefer the latest syntax with proper indentation to avoid errors.