What is the Main Function in Python and how to use it?
Python Functions
The python function is used to be a block of reusable of the python code and the python function is used to performing statements, Action, In-build methods that form the blocks of the python programming language. Which could behave more input data to perform the leveraged on the input data and provided the output to the user.
A python function is a block of python code that is written to act out a specific statement, methods, and task. In Python functions, we have Four types of functions which are Anonymous python functions, User-defined python functions, and Built-in python function, the main function these are the function in Python.
Python Main Function
In python programming language the function which is automatically executed the pice of code program is every time to be run. the python main function, or main() as it is denoted to be normal. It is the start of the execution of the python program from the compiler point of and The python main is not mandatory to declare at the start of the program and it is not mandatory to code in every python program. Main Function in python is done by using an import function and module in python main method code.
Consider the following python main function
def main(): print "Hello Python..!" print "Besant Technology"
In the above program which is defined to be two lines of print function which is “Hello Python..!” and one more line of print code is “Besant Technology” which is done by a conditional statement of the above example of python main function.
- “Besant Technology” output is only printed out in the console.
- “Hello, Python..!” code is not executed and not displayed at console.
Output:
Because the Conditional statement is not implemented in the above program so the “Hello Python..!” is not displayed at the console and the interpreter is finding the source file of the __name__ and the value to a __main__. Then it will be executed when we declared a __name__ as special variable and __main__ as to be python main function.
When we execute the main function which will execute by the conditional statement by if statement and it checks whether the special variable is equal to python main function then it executes the following statements.
Consider the following python main function
def main(): print "Hello Python..!" If __name==’__main’: main() print "Besant Technology"
Executed in IDLE and Output
Example-1:
In the below python code as an example, which is defined to be a function called “Besant_Technology“, which will be storing the string value in the variable “course” also it is stored in the method variable to be a “BT” the output shown below:
Output:
Include the conditional statement for the above program then execute the main function which will execute by the conditional statement by if statement and it checks whether the special variable is equal to python main function then it executes the following statements else it will not execute the below program.
Program:
def Besant_Technology(): print("Python in Besant Technlogy") course="Python,Java,C++,AWS" print("Completed") return course def Course(BT): course=BT.lower() print course def main(): BT=Besant_Technology() print BT Course(BT) if __name__ == "__main__": main()
Executed in IDLE
Output:
Related Blogs:
- Brief Overview of Python Language
- Python Career opportunities
- Python Break Continue
- Python Control Flow
- Python Data Types
- Python Dictionary
- Python Exception Handling
- Python File
- Python Functions
- Python Substring