Chapter 4 (Using Python Libraries)
Chapter 4 (Using Python Libraries)
NOIDA EXTENSION
CLASS 12
COMPUTER SCIENCE
CHAPTER 4. USING PYTHON LIBRARIES
Python Libraries:
1. Frequently used modules are generally known as libraries which contain
code for general purpose.
2. These libraries are the collection of methods, classes which can be used
easily.
3. Python program is made using 3 different components:
a. Library or package
b. Module
c. Functions/ sub- modules
Modules in Python:
1. A module is a file containing python definition, functions, variables,
classes and statements. The extension of this file is ‘.py’.
2. The name of module is the name of .py file eg, in math.py the module
name is math and _name_ variable is used to store this module.
Advantages:
a. Its biggest advantage is that we can import its functionality in any
program and use it.
b. Reusability is one of the biggest advantages.
c. It helps in logically organization of Python code.
d. Programming becomes vert easy when we use the collection of same
types of codes.
Library/ Package: Library is a collection of modules. Some commonly used
Python libraries are as listed below:
i. Python Standard library: This library is distributed with Python that contains
modules for various types of functionalities. Some of them are:
a. math module provides mathematical functions.
b. cmath module provides mathematical functions for complex numbers.
c. random module provides functions for generating pseudo- random
numbers.
d. statistics module provides mathematical statistics functions.
e. Urllib module provides URL handling functions so that you can access
websites from within your program.
ii. NumPy Library: Provides some advance mathematical functions along with
tools to create and manipulate numeric arrays.
iii. SciPy Library: This library offers algorithmic and mathematical tools for
scientific calculations.
iv. Malplotlib Library: Offers many functions and tools to produce quality
output in variety of formats such as plots, charts, graphs etc.
Steps to make a package is as follows:
a. We create a folder or directory named geometry which will contain two
files area.py and volume.py.
b. In the same directory or folder, we will put another file named ‘_init_.py’
c. ‘_init_.py’ is necessary because this is the file which tells Python that
directory is package. And this file initializes the package.
d. Then we import the package and use the content.
Processing of import <module> command:
When you run the import <module> command then the following things occur:
a. Imported module’s code gets executed after interpretation.
b. All the programs and variables of imported module are present in the
program.
c. A namespace setup is created for the imported module in the name of
module.
Processing of from <module> import <object> command:
When you run the from <module> import <object> command then the
following things occur:
a. Imported module’s code gets executed after interpretation.
b. Only asked programs and variables of imported module are present in
the program.
c. No namespace is created. Import objects of module are attached to
current namespace.
How to make modules in Python:
1. To make a module in Python you have to make a .py file which has a
name. Suppose we make a file ‘Shape.py’.
2. Then we write different functions of area within it.
3. And we put it within same folder where our program is stored.
How to make modules in Python?
Using form <module> import <function_name>
Namespace in Python:
1. For every name reference python always follow name resolution rule
when you access variables from any program or function.
2. This is known as LEGB rule in which:
a. First variable is checked in Local Environment, and if it is available
then it will be used.
b. Then variable is checked in Enclosing Environment, and if it is
available then it will be used.
c. Then variable is checked in Global Environment, and if it is
available then it will be used.
d. Then variable is checked in Built- in Environment, and if it is
available then it will be used.
e. Other wise Python will generate an error.
Module Aliasing in Python:
a. When you import module in Python program then we have to use the
following syntax to make alia name of module:
import <ModuleName> as <AliaName>
b. Then we use (.) operator to use the members of the module with alia
name of module. Example is given below:
c. When you import member of any module with alia name, then you
have to use the following syntax:
from <ModuleName> import <MemberName> as <AliaName>
Locating the module: