Today, we will learn about most important concepts of modules in python.
Modules in Python:-
- A modules is a .py files containing definitions and statements. So all .py files that we created for our python programs are modules.
- A python modules can be defined a python program file which contains .py files including python variable , class and functions etc.
- A python modules provides us the flexibility to organize the code in a logical way.
e.g. Create a modules named as modules.py
def show ():
print("Welcome to MNT LAB")
#calling the show() function
show()
Output:-Welcome to MNT LAB
>>>
The Main Modules:-
- When we execute a program its module name is __main__.
- This name is available in the variable __name__.
- This technique is executed only when we run program directly and not executed when imported as a module.
- When we run any python codes ,python interpreter start interpreting the codes inside it. At that time it sets some implicit variable values like math, random etc. ,one of them is __name__ whose value set as __main__.
- If the program source file imported as a module , At that situation interpreter sets the __name__ value to module name. Then this main method will not work.