- What are python modules?
- What are the benefits of using python modules?
- How python modules can help to enhance our python programming skills?
- Types of python modules.
- How we use ‘import’ in python modules?
- Search sequence of python Module
- Exploring the existing module using dir() built-in function.
- Example of python modules.
1. What are python modules?
Python modules are python files (.py) which generally contain the python objects (variable & functions) of similar types. We commonly use python modules to logically organize our python project. It is used to categories the python code. We can group similar functions in one module. Using the python modules we can increase the code maintainability and re-usability.
2. What are the benefits of using python modules?
- Readable Programs
With help of python modules, we can create a python project that will be easy to understand. - Split-up the python project development using python modules
Cutting-up the large python application into smaller python modules can help programmers or developers to work efficiently. - Re-Use of Codes
Python module helps to re-use the existing module, it removes the redundancy of codes. - Improves Manageability
Once the whole project is broken into smaller python modules, it becomes very easy to test or design. - Programming Errors are Easy to Detect
By grouping the whole application into multiple modules, it becomes straightforward to find the error. We exactly know the location of the error ie. from which module and from which function the error is generating. - Collaboration
Multiple programmers can work parallel on different python modules on the same application. - Easy to Maintain
It becomes very easy to enhance the single module rather than enhancing the whole application altogether.
3. How python modules can help to enhance our python programming skills.
Python programming skills can be greatly increased by using the python module. Below are some points which are true to enhance the python programming skills
- Python modules give the developer capability to implement the logic in a structured way.
- Remove all the redundant code by using modules
- Keep similar functions in a single module to make the code maintainable.
- fast debugging
4. Types of python modules
Python modules can be of 2 types
- Built-in Modules.
- User-defined Modules.
-
Built-in Modules
Python has a vast range of built-in libraries and a vast number of build-in modules. e.g sys, os, random, etc.
-
User-defined Modules
In python, a user has the freedom to create his module and in that module, he can define any number of functions and variables.
5. How we use ‘import’ in python modules?
The ‘import‘ keyword is used to import the module in python script. When the interpreter encounters an import statement, it imports the module if the module is present in the search path.
We can import a particular function or a particular object from a module using from…import statement.
6. Search sequence of python Module
When we import a module, the Python interpreter searches for the module in the following sequence
- Search in the current directory.
- If not found, searches each directory path of shell variable PYTHONPATH.
- If nothing found, then on UNIX, it search in ‘/usr/local/lib/python/’ path.
7. Exploring the existing module using dir() built-in function.
To explore the modules (build-in module or user-defined module) we can use the dir() function.
Built-in functions can be defined as the functions whose functionality is pre-defined in Python. All built-in functions and exceptions can be found in the ‘builtins‘ module.
Example: Getting the python list or python array of the ‘random’ module.
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Jun 26 22:57:06 2021 @author: hadoop website : http://mycloudplace.com @author: mycloudplace """ import random if __name__ == "__main__": content = dir(random) print(content)
OUTPUT:
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_BuiltinMethodType', '_MethodType', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_itertools', '_log', '_os', '_pi', '_random', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
7. Example of python modules
In our example, we will create a ‘data_cleaning’ module, in this module we are going to create a function that will remove the duplicate values. You can follow the below steps to create a new module
- Create a working directory say “module_demo”
- Inside the “module_demo” directory add a python file ‘data_cleaning.py’
- Add below code to ‘data_cleaning.py’ python script
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Jun 26 23:44:45 2021 @author: hadoop website : http://mycloudplace.com @author: mycloudplace """ def remove_duplicate(input_list): result = [] [result.append(x) for x in input_list if x not in result] return result
- Inside the “module_demo” directory add a python file ‘main.py’
- Add below code to ‘main.py’ python script
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Jun 26 22:57:06 2021 @author: hadoop website : http://mycloudplace.com @author: mycloudplace """ import data_cleaning if __name__ == "__main__": list_with_duplicate = [1,2,2,2,3,4,5,5,6,7,7,7,7,8,9,9] print("List before removing duplicate value") print(list_with_duplicate) list_without_duplicate = data_cleaning.remove_duplicate(list_with_duplicate) print("List after removing duplicate value") print(list_without_duplicate)
- We will get the below output after executing the ‘main.py’ script
List before removing duplicate value [1, 2, 2, 2, 3, 4, 5, 5, 6, 7, 7, 7, 7, 8, 9, 9] List after removing duplicate value [1, 2, 3, 4, 5, 6, 7, 8, 9]
A list of all Python modules can be found here.
Below is our previous articles on python
- Python array | numpy for python | How to create numpy or np array in python
- Python Lambda Function With Examples | How To Create A New Anonymous function
- Python Useful Built-in Functions With Examples | How to create a new function
- python iteration | while loop | for loop with examples
- Python Data Types With Examples
- Python – installation indentation variable comments
- Python Programming Introduction
Nicely Described…
Thank you Eathan.
Very useful in improving my python programming skills
Thanks
Thank you
Thanks Prem sir for making python soo easy for me
Thank you
Pingback: File Handling Python With Examples | Python Input-Output Awesome Tips - Mycloudplace