×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python program to call methods from main() method

Here, we will write a Python program to call methods from the main() method. By Shivang Yadav Last updated : January 05, 2024

Basically, there is no specific main() with special functionality as we see in C / C++.

The main() functionality will not be the first block of Python code to be executed. If the programmer wishes to, it can be done using the __name__ variable that is set to the name value by interpreter. If the file is directly being run __name__ will have __main__ value. If it is imported to a file __name__ will have a calling module name.

Python program to call main() as a function

# Python program to call main() as a function

# function - main() 
def main():
    hi()
    
# function - hi()
def hi():
    print("Hi")

# calling main function 
main()

Output:

Hi

Python program to call main() using __name__

# Python program to call main() using __name__ 

# function - main() 
def main():
    hi()
    
# function - hi()
def hi():
    print("Hi")

# calling main function using __name__ 
if __name__=="__main__":
    main()

Output:

Hi

To understand the above program, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Advertisement
Advertisement

Related Programs

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.