A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Example file .py into folder projects
projects/fruits.py
projects/trees.py
projects/garden.py
A Python module can have a set of functions, classes or variables defined and implemented. Example file fruits.py define more functions
#fruits.py
def
get_list_fruit
(): fruits=
['Banana', 'Apple', 'Orange', 'Coconut']
return
fruitsdef
check_fruit_exist
(fruit): fruits=
['Banana', 'Apple', 'Orange', 'Coconut']
for
frin
fruits:if
fr==
fruit:return
True
return
False
Importing module objects to the current namespace in file garden.py
#garden.py #import fruit module #or you can import all functions in file fruits.py: "from fruits import *"
from
fruitsimport
get_list_fruit
,check_fruit_exist
def
main
(): list_fruit =get_list_fruit
() is_fruit_cherry =check_fruit_exist
('Cherry'
)if
__main__ =='__main__'
:main
()
Executing modules as scripts. Open terminal on Window/Unbuntu/Macos and run execute command
>>> python garden.py