In this article you learn about Python Module.
Python Module – Before moving ahead, let’s take a look of Python Scope
Module – It defined as a file saved with .py extension and contains statements, block of codes and anything which can be written in the file. In other words, a module can store variables, data types, class, function, etc., and make them easier to deal with.
Extension – File must be saved with .py extension, otherwise Python will not support file with another extension such as .txt, etc. Another extension’s file can be used for editing or writing code but to execute that file’s statements, save it to .py extension.
Syntax – module_name.function_name.
module name – It refers as file name.
function name – It refers extension types such as, .py, .txt, etc.
Create a module – To create a module (file) save file with .py extension.
Example 1- Create a file (module) and save it with .py extension.
x = 1 y = 2 r = sum(x , y) print('The sum of x and y is: ', r)
Explanation – In the above example, an action of addition two numbers is performed and return output.
Use a Module – To use a module, use import keyword.
Example 2- Open your file(module.py) with import statement.
Explanation – In the above example, import module to use file.
Variables in Module – A module can contain Python variables like list, arrays, dictionary etc.
Example 3- Save code in file module.py.
x = 10 c = 5
Explanation – In the above example, wrote the codes in file module.py and saved them.
Get items – By referring particular element.
Example 4- Import module and get specified item.
x = 10 c = 5 from module import * z = add(x , c)
Explanation – In the above example, from file module.py import every block of codes from sign (*) and after that performed an action from file module.py.
Changing a module name – To change a module (file) name use ‘as’ keyword, but extension should be .py.
Example 5- Re-naming a module name and get specified item.
x = 10 c = 5 import module as another_file_name z = another_file_name.multi(x , c)
Explanation – In the above example, file module.py import as new file name i.e., another_file_name and after that performed an action.
Built -in Modules – Python contains so many modules in it which can be used at any time.
Example 6- Import Python built-in module platform.
import platform z = platform.system() print (z)
Explanation – In the above example, keyword ‘platform’ returned on which platform or application text editor in running.
Use the dir() Function – Use it to list all the function names including variable names in a module.
Example 7- Use of dir() function to list all the function.
import platform z = dir(platform) print(z)
Explanation – In the above example, function ‘dir’ returned all the function names including variable names in a module.
Import a part of module – To Import a part of module use from keyword.
Example 8- Import a part of module
x = 10 c = 5 from module import multi z = multi(x , c)
Explanation – In the above example, from file module.py import particular function to be executed.
Note: While using from keyword, did not use module name, example start.index(0).
If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.
Like us on