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.pydefget_list_fruit(): fruits=['Banana', 'Apple', 'Orange', 'Coconut']returnfruitsdefcheck_fruit_exist(fruit): fruits=['Banana', 'Apple', 'Orange', 'Coconut']forfrinfruits:iffr==fruit:returnTruereturnFalse
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 *"fromfruitsimportget_list_fruit,check_fruit_existdefmain(): 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
