We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

What is the Main Function in Python and how to use it?

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.

  1. “Besant Technology” output is only printed out in the console.
  2. “Hello, Python..!” code is not executed and not displayed at console.

Output:

Python Main Function 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

Python Execution Output

Python Idle 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:

Variable of Python

Output:

Python Function

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

Python Excustion Method

Output:

Python Main Output

Related Blogs:

  1. Brief Overview of Python Language
  2. Python Career opportunities
  3. Python Break Continue
  4. Python Control Flow
  5. Python Data Types
  6. Python Dictionary
  7. Python Exception Handling
  8.  Python File
  9. Python Functions
  10. Python Substring

 

Besant Technologies WhatsApp