Modules in python tactics that can boost your python programming skills

  1. What are python modules?
  2. What are the benefits of using python modules?
  3. How python modules can help to enhance our python programming skills?
  4. Types of python modules.
  5. How we use ‘import’ in python modules?
  6. Search sequence of python Module
  7. Exploring the existing module using dir() built-in function.
  8. 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.

Jump to the top of the page↵

2. What are the benefits of using python modules?

  1. Readable Programs
    With help of python modules, we can create a python project that will be easy to understand.
  2. 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.
  3. Re-Use of Codes
    Python module helps to re-use the existing module, it removes the redundancy of codes.
  4. Improves Manageability
    Once the whole project is broken into smaller python modules, it becomes very easy to test or design.
  5. 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.
  6. Collaboration
    Multiple programmers can work parallel on different python modules on the same application.
  7. Easy to Maintain
    It becomes very easy to enhance the single module rather than enhancing the whole application altogether.

Jump to the top of the page↵

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

Jump to the top of the page↵

4. Types of python modules

Python modules can be of 2 types

  1. Built-in Modules.
  2. User-defined Modules.
  1. 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.

  2. 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.

Jump to the top of the page↵

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.

Jump to the top of the page↵

6. Search sequence of python Module

When we import a module, the Python interpreter searches for the module in the following sequence

  1. Search in the current directory.
  2. If not found, searches each directory path of shell variable PYTHONPATH.
  3. If nothing found, then on UNIX, it search in ‘/usr/local/lib/python/’ path.

Jump to the top of the page↵

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']

Jump to the top of the page↵

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

  1. Create a working directory say “module_demo”
  2. Inside the “module_demo” directory add a python file ‘data_cleaning.py’
  3. 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
    
  4. Inside the “module_demo” directory add a python file ‘main.py’
  5. 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)
    
  6. 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]
    

Jump to the top of the page↵

A list of all Python modules can be found here.

Below is our previous articles on python

Jump to the top of the page↵

7 thoughts on “Modules in python tactics that can boost your python programming skills”

  1. Pingback: File Handling Python With Examples | Python Input-Output Awesome Tips - Mycloudplace

Leave a Comment

Your email address will not be published. Required fields are marked *