Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Chapter 4 (Using Python Libraries)

Uploaded by

sharad9shashwat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Chapter 4 (Using Python Libraries)

Uploaded by

sharad9shashwat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

GD GOENKA INTERNATIONAL SCHOOL

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

Relation between Python Libraries, Module and Package:


1. A module is a file containing python definition, functions, variables,
classes and statement. The extension of this file is ‘.py’.
2. In python, package is directory (folder) of python modules.
3. A library is a collection of many packages in python. Generally, there is
no difference between python package and python library.

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. We imported a module in to a program which was referred as a


namespace.
2. To define a namespace, it is necessary to define its name.
3. Python name is a kind of identifier which we use to access and python
object. And in Python each and everything is an object.
4. Namespace is used to separate the different sections of a program.
5. Python has 3 types of namespaces: Global Local Built- in
6. This is just like variable scope in Python.
7. When we start interpreter, then a namespace is created in which print ()
and id () etc. are already exist. This holds all the built- in name.
8. Each module creates its own global namespace.
9. When we call a function then a local python namespace is created where
all the names of functions are existed.
Resolving the scope of a name:

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:

a. When we import a module then interpreter of python searches the


module in the following sequence:
• First current directory.
• If module not found then it will be searched in shell variable
PYTHONPAT.
• If not found anywhere then python searches in default. Default path
is the path where Python is installed.

You might also like