Notes And Questions NCERT Class 12 Computer Science Chapter 12 Using python libraries

Notes for Class 12

Please refer to Using python libraries Class 12 Computer Science Notes and important questions below. The Class 12 Computer Science Chapter wise notes have been prepared based on the latest syllabus issued for the current academic year by CBSE. Students should revise these notes and go through important Class 12 Computer Science examination questions given below to obtain better marks in exams

Using python libraries Class 12 Computer Science Notes and Questions

The below Class 12 Using python libraries notes have been designed by expert Computer Science teachers. These will help you a lot to understand all the important topics given in your NCERT Class 12 Computer Science textbook. Refer to Chapter 12 Using python libraries Notes below which have been designed as per the latest syllabus issued by CBSE and will be very useful for upcoming examinations to help clear your concepts and get better marks in examinations.

Learning Outcomes: Learn how to create and use Python libraries

Python Libraries:

Frequently used modules are generally known as libraries which contain code for general purpose.
• These libraries are the collection of methods, classes which can be used easily.
• Python program is made using 3 different components. –
–Library or package
–Module
–Functions/sub-modules

Relation between Python Libraries, Module and Package:

• A module is a file containing python definition, functions, variables, classes and statements. The extension of this file is “.py”.
• While Python package, is directory (folder) of python modules.
• A library is collection of many packages in python.

Module in Python:

A module is a file containing python definition, functions, variables, classes and statements. The extension of this file is “.py”.
•The name of module is the name of .py file e.g. in math.py the module name is math. And _name_ variable is used to store this module.

•Advantages–

–Its biggest advantage is that we can import its functionality in any program and use it.
–Reusability is one of the biggest advantages.
–It helps in logically organization of Python code.
–Programming becomes very easy when we use the collection of same types of codes.
–Categorization: same attributes can be stored in one module.

Processing of import <module> command

• When you run the import <module> command then the following things occur –
1. Imported module’s code gets executed after interpretation.
2. All the programs and variables of imported module are present in the program.
3. 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 –
1. Imported module’s code gets executed after interpretation.
2. Only asked programs and variables of imported module are present in the program.
3. No namespace is created. Import objects of module are attached to current namespace.

How to make modules in Python?
• To make a module in python you have to make a .py file which has a name. Suppose we make a file “Shape.py”.
• Then we write different functions of area within it.
• And we put it within same folder where our program is stored.

Notes And Questions NCERT Class 12 Computer Science Chapter 12 Using python libraries
Notes And Questions NCERT Class 12 Computer Science Chapter 12 Using python libraries

We used the module in our program by using import keyword. To use the members of module we use (.) as shown in the example.
• Using from <module> import <function_name> OUTPUT

By using “from shape import AreaCircle “only AreaCircle function is imported and to call this there is no need to use (.) operator.

PACKAGE / LIBRARY:

• Python Package is the collection of same type of modules.
• You can use built in package as well as your own package.
• Python Package is a simple directory. Modules are stored in that directory.
• Package or library are generally same.
• Steps to make a package is as follows – (Lets say package name is geometry)
1. We create a folder (directory) named geometry which will contain two files area.py and volume.py
2. In the same directory we will put another file named “__init__.py”
3.__init__.py is necessary because this is the file which tells Python that directory is package. This file initializes the package.
4. Then we import the package and use the content.
EXAMPLE:

Let’s create a package named Shape and build three modules in it namely Rect, Sq and Tri to calculate the area for the shapes rectangle, square and triangle.
Step-1 First create a directory and name it Shape. (In this case it is created under
C:\Users\ViNi\AppData\Local\Programs\Python\Python37-32\ directory path.)
Step-2 Create the modules in Shape directory.
To create Module-1(Rect.py), a file with the name Rect.py and write the code in it.

Notes And Questions NCERT Class 12 Computer Science Chapter 12 Using python libraries

To create Module-2(Sq.py), a file with the name Sq.py and write the code in it.

Notes And Questions NCERT Class 12 Computer Science Chapter 12 Using python libraries

To create Module-3 (Tri.py), a file with the name Tri.py and write the code in it.

Notes And Questions NCERT Class 12 Computer Science Chapter 12 Using python libraries

Step-3 Create the __init__.py file. This file will be placed inside Shape directory and can be left blank or we can put this initialisation code into it.

from Shape import Rect
from Shape import Sq
from Shape import Tri
That’s all. Package created. Name of the package is Shape and it has three modules namely Rect, Sq and Tri.

How to use the created package in a program:

Following steps should be followed to use the created package.
Step-1 Create a file main.py in the same directory where Shape package is located.
Step-2 Write the following code in main.py file

Notes And Questions NCERT Class 12 Computer Science Chapter 12 Using python libraries

OUTPUT:
Area of Rectangle is : 200
Area of square is : 25
Area of Triangle is : 24.0

4.4 Import the package in program:
Method-1
To use the package/library in a program, we have to import the package in the program. For this we use import keyword.

If we simply import the Shape package, then to access the modules and functions of Shape package, we have to write the following code:
Syntax:
Package-name.Module-name.function-name(parameter)
Example: import Shape
r=Shape.Rect.Area( )
s=Shape.Sq.Area( )
t=Shape.Tri.Area( )

Method-2:
If we want to access a specific module of a package then we can use from and import keywords.
Syntax:
from package-name import module-name
Example:
from Shape import Rect
r=Rect.Area( )
s=Sq.Area( ) This will give error, as we have imported only Rect module
Method-3:
If a user wants to import all the modules of Shape package then he/she can use * (asterisk).
Example:
from Shape import *
r=Rect.Area( )
s=Sq.Area( )
t=Tri.Area( )