0% found this document useful (0 votes)
19 views1 page

Main Function in Python Language

The document discusses how to define and call the main function in Python. It explains that a main function is the starting point of a Python program and will only execute when the file is run as the entry point, not when imported as a module. It also demonstrates how to properly call the main function using the if __name__ == '__main__' syntax to ensure it executes when run as a program.

Uploaded by

ismailtaayah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views1 page

Main Function in Python Language

The document discusses how to define and call the main function in Python. It explains that a main function is the starting point of a Python program and will only execute when the file is run as the entry point, not when imported as a module. It also demonstrates how to properly call the main function using the if __name__ == '__main__' syntax to ensure it executes when run as a program.

Uploaded by

ismailtaayah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

HOW MAIN IN PYTHON

Definition :

Python main function is a starting point of any program. When the program is
run, the python interpreter runs the code sequentially. Main function is executed
only when it is run as a Python program. It will not run the main function if it
imported as a module.

To understand this, consider the following code

def main():
print ("hello world!")
print ("Guru99")

Output : Guru99

Here, we got two pieces of print- one is defined within the main function that is "Hello
World" and the other is independent, which is "Guru99". When you run the function def
main ():
• Only "Guru99" prints out
• and not the code "Hello World."

It is because we did not declare the call function "if__name__== "__main__".

It is important that after defining the main function, you call the code by if__name__==
"__main__" and then run the code, only then you will get the output "hello world!" in the
programming console. Consider the following code

def main():
print("Hello World!")

if __name__ == "__main__":
main()

print("Guru99")

Output :
Hello World!
Guru99

Here is the explanation,

• When Python interpreter reads a source file, it will execute all the code found in
it.
• When Python runs the "source file" as the main program, it sets the special
variable (__name__) to have a value ("__main__").
• When you execute the main function, it will then read the "if" statement and
checks whether __name__ does equal to __main__.
• In Python "if__name__== "__main__" allows you to run the Python files
either as reusable modules or standalone programs.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy