How to use the main function in Python Programming Language

On this article, I’ve defined about how one can use the primary operate within the python programming language.



important operate

There’s a operate named important which is taken into account the entry level of a program. It means, irrespective of what number of features we have now outlined in our program it would discover the primary operate and executes it on the very starting, and afterward different packages might be executed serially which are inside the primary operate.

As python is an interpreted language, it doesn’t have any important operate of any entry factors. It merely executes this system from the highest to backside method. Python offers a particular built-in variable named __name__ and on this article, we are going to discover how we are able to use the primary operate because the entry level of a python program and executes it on the very starting.

Let’s see the under instance first the place we don’t add any performance and easily outline a python operate and named it important()

def important():
     print ("Welcome to KCD Chennai!")

print ("Welcome")


# output: Welcome
Enter fullscreen mode

Exit fullscreen mode

Within the above program, we are able to see that it solely prints the Welcome within the output and doesn’t execute the primary operate. However we need to execute the primary operate in our program. To carry out this motion we have to use the python’s built-in __name__ variable and set the variable worth as the primary operate. See the under code instance:

def important():
     print ("Welcome to KCD Chennai!")

if __name__ == "__main__":
    important()

print ("Welcome")


# Output: 
# Welcome to KCD Chennai!
# Welcome
Enter fullscreen mode

Exit fullscreen mode

This time the primary operate has been executed first after which the remainder of the code. The primary motive behind is that we have now used python’s built-in variable and when the if block of code executes it has discovered the primary operate and easily returned true in consequence, the primary operate has been executed.

Thanks for studying my article until finish. I hope you realized one thing particular immediately. Should you loved this text then please share to your pals and if you realize another methods to make use of the primary operate in Python Programming Language or ideas to share with me then please write within the remark field.

Add a Comment

Your email address will not be published. Required fields are marked *