![]() ![]() When the interpreter reaches the if statement it checks for the value of the name and when the value of it is true it runs the main function else the main function is not executed. When this program is executed, the interpreter declares the initial value of the name as “main”. print("Hello") def main(): print("Hey There") if _name_="_main_": main() _name_ is a built-in variable which evaluates to the name of the current module. ![]() If this file is being imported from another module, _name_ will be set to the module’s name. If the source file is executed as the main program, the interpreter sets the _name_ variable to have a value _main_. However, before doing that, a few special variables are defined. Since there is no main() function in Python, when a Python program is run, the code present at level 0 indentation is executed. ![]() It does not matter if the main function is present or not. But as we already know, the Python interpreter runs the code right from the very first line and then goes line by line. In any program, the main() function is like the entry point. “’This function will print the passed string’” Only that, we will use a docstring here to describe what the function will do. We will do the same in this example as well. In the example provided for calling a function, we used a comment to describe what the function was going to do. A docstring can also extend up to multiple lines. We use triple quotes around the string to write a docstring. Įven though including a docstring in our function is optional, it is considered a good practice as it increases the readability of the code and makes it easy to understand. ‘Docstring’ is the abbreviation for ‘documentation string’.įor getting certified and having the best career growth, check out our Python training course. It is used to briefly and crisply describe what a function does. The first statement or string in any Python function (optional statement) is called a docstring. Go through this Python Course in London to get a clear understanding of Python! Adding a Docstring in a Python Functions ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |