Using Python Libraries – Notes

Using Python Libraries – Notes

  • Using Python libraries: create and import Python libraries.
  • Using libraries: Mathematical, Random, String and Datetime.

What is Library?

Library refers to the collection of related module(s), which works together to fulfil specific requirements or help us in making any application.

The library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming.

Commonly used Python Libraries

Python Standard Library

The Python Standard Library is a collection of exact syntax, token, and semantics of Python. It comes bundled with core Python distribution. Some common used module under standard Library are –

math module  – collections of mathematical functions for different calculations.

cmath module – collection of  mathematical functions for complex numbers.

random module – collection of functions for generating pseudo-random numbers

statistic  module – collection of functions for doing statistical calculation.

urllib module – collection of url handling functions.

NumPy Library – Provide several tools to manipulate numeric arrays.

SciPy Library –  helps in scientific calculation by using algorithmic and mathematical tools.

Tkinter Library – helps to create user friendly GUI interface

Matplotlib Library – Matplotlib helps with data analyzing, and is a numerical plotting library

Pandas Library – Pandas is a must for data-science. It provides fast, expressive, and flexible data structures to easily (and intuitively) work with structured (tabular, multidimensional, potentially heterogeneous) and time-series data.

What is Module?

Module is a simple file that contains Python code.

It is a file containing functions and variables .

Module helps in breaking a program into separate modules, each module should contain functions that perform related tasks.

Modules allows to reuse the code more than in one program.

To use a module in Python, use the import statement.

Python provides some built in module called Standard library module, for example – String, Math, Random, etc.

Structure of a Python Module

Generally Python module is a collection of functions. It may contain following objects –

docstring  – useful for documentation purpose.

variable & constants –  to store some data i.e. fixed.

classess  – blueprints to create object.

Objects – instances of classes.

Statements – set of instructions i.e. module body

Functions – collection of instructions i.e. named group of instructions.

Creating Module
 """Calculate Different types of Interest"""
 #Function - Simple
 def simple(p = 0, r = 0, t = 0):
     """Calculate Simple Interest"""
     return ( (p*r*t)/100.00)
 def compound(p = 0, r = 0, t = 0):
     """Calculate Compound Interest"""
     ci = p * ((1+r/100)**t) - p
     return ci
 SI = 0.00
 CI = 0.00 

Module Summary

File Name –

Module Name – interest

Contains :-

   Two Functions :

  (a) simple ( )

  (b) compound ( )

 Two constants/Variable with fixed value

  (a) SI

  (b) CI


  Three triple quotes string.

Getting Details of  Module

help(module name) – In Python, help(modulename) command is used to get complete details with defined name inside module.

 import Interest 
 Help on module interest:
     interest - Calculate Different types of Interest
     compound(p=0, r=0, t=0)
         Calculate Compound Interest
     simple(p=0, r=0, t=0)
         Calculate Simple Interest
     CI = 0.0
     SI = 0.0

dir – In Python dir(modulename) command is used to get all names (variables / function / classes) defined inside the module.

 >>> import interest
 >>> dir(interest)
 ['CI', 'SI', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'compound', 'simple'] 

Importing Modules

Leave a Comment

You cannot copy content of this page

Table of Contents
Scroll to Top