Using With Python Libraries || Notes || Sumita Arora || Class 12 || Computer science || Information practices



PDF Download link given below of this Blog


Note = All given notes are based on Sumita Arora book class 12






Using With Python Libraries

 

Library: - Library is a collection of modules (and packages) that together cater to a specific type of applications or requirements.

Modules: - The act of partitioning a program into individual components (known as modules) is called modularity.
A module is a separate unit in itself.

Advantage of module: -
• It reduces its complexity to some degree.
• It creates a number of well defined, documented boundaries within the program.
• Its contents can be reused in other programs, without having to rewrite or recreate them.

Python module: - A python module is a file (.py file) containing variables, class definitions, statement and functions related to a particular task.
• The python module that come preloaded with python are called standard library modules.

Importing Modules in a Python Program
Python provides import statement to import modules in a program. The import statement can be used in two forms.
(1) To import entire module: the import <module> command
(2) To import selected objects from a module: the from <module> import <object> command

Importing Entire Module: -
The imports statement can be used to import entire module and even for importing selected items.
To import entire module syntax is -
import <module 1 >, <module 2>....
For example:

import time
import decimals, fractions

Dot notation :- After importing a module, to access one of the functions, you have to specify the name of the module and the name of the function, separated by a dot (also known as a period) this format is called dot notation.
Syntax: -
<module-name>.<function-name> ()
This way of referring to a module's object is called dot notation.
For example:
import math
math.sqrt(16)

• You can give alias name to imported module as
Syntax :
import <module> as <alias name>

For Example :-

import math as a
a.sqrt (16)

Importing Select Objects from a Module: -
If you want to import some selected items, not all from a module, then you can use following syntax:-
from <module name >import<object name>

For example:
from math import sqrt

To Import Multiple Objects:-
If you want to import multiple objects from the module then you can use following syntax :-
from <module name >import<object name>,<object name>,<object name>....

For example: -
from math import sqrt, pi, pow


USING PYTHON STANDARD LIBRARY'S FUNCTIONS AND MODULES: -
Python's standard library offers many built in functions and modules for specialized type of functionality.

For example:-
len(),str(),type()
math module, random module , etc.

Using python's built in functions: -
The python interpreter has a number of functions built into it that are always available, you need not import any module for them.






Python's built in string functions: -
That are ---

<str>.join (<string iterable>) - Joins a string or character after each member of the string iterator.

(I) If the string based iterator is a string then the <str> is inserted after every character of the string.
For example:

>>>"***". join ("Hello")
'H***e***l***l***o'

(ii) If the string based iterator is a list or tuple of string then, the given string / character is joined with each member of the list or tuple, But the list or tuple must have all member as strings otherwise Python will raise an error.

>>>"***". join (("Hello", "Python"))
'Hello***Python'

>>>"***". join (["Hello", "Python", "Language"])
'Hello***Python***Language'

>>>"***". join ((123,"Hello","Python"))
Error

<str>. split (<string/char>) - Split a string based on given string or character and return a list containing split strings as members.

(i) If you do not provide any argument to split then by default it will split the give string considering whitespace as a separator.

For example:
>>>"I Love Python". split()
['I', 'Love', 'Python']

(ii) If you provide a string or a character as an argument to split (),then the given string is divided into parts considering the given string/character as separate and separator character is not included in the split string.

For example:
>>>"I Love Python". split ("o")
['I L','ve Pyth','n']

<str>. replace (<word to be replaced>,<replace word>) - Replaces a word or part of the string with another in the given string <str>.

For example:
>>>"I Love Python". replace ("Python", "Programming")

>>>"I Love Programming"



USING RANDOM MODULE: -
Python has a module namely random that provides random number generators.
To use random number generators in your Python program, you first need to import module random using any import command
import random

Some most common random number generator functions in random module are:

random () : - It returns a random floating point number N in range [0.0,1.0] , i.e., 0.0 ≤ N ≥ 1.0.

randint (a, b) : - It returns a random integer N in the range (a, b) , i.e. , a ≤ N ≤ b (both range-limit are inclusive).

random.uniform(a, b) : - It returns a random floating point number N such that
a ≤ N ≤ b for a ≤ b and
b ≤ N ≤ a for b < a and

random.randrange(stop) or random.randrange(start, stop, [ steps]) : - It returns a randomly selected element from rang ( start, stop, step ) .

USING STRING MODULE: -
Python has a module by the name string that comes with many constant and classes.
If you want to use string module, then you must import it by using import command: -
Like that: -

import string



For example: -

>>>import string
>>>string.digits
'0123456789'

CREATING A PYTHON LIBRARY: -

Package: - A package is a collection of Python modules under a common namespace, created by placing different modules on a single directory along with some special file such (__init__.py).
• A library can have one or more packages and sub-packages.

 

Steps of making package: -

·       At first you have known a path where all files of python saved.

You can find the path of python by following command as shown in figure:-




·       Copy that path and paste in computer paths like this: -

 



·       Make a file (package name). For example we make pathwala (package) folder.




·       Now make another folder (Sub Package) in package folder; if you want

For example we make portal_express (sub package)




·       Now make an empty module with name __init__.py in portal_express (sub package)

And save that module like that:-






Like that:-         



              

·       Similarly also, make an empty  module with name __init__.py in pathwala(package)




·       Now make modules as you want in pathwala(Package)

For example : We make volume module as shown in figure



 

And save that module in pathwala (Package)



 

·       Now make modules as you want in portal_express(Sub Package)

For example : We make area module as shown in figure

 



·       And save that module in portal_express (Sub Package)




·       Now, Your package become ready to use in Python 

For example as shown in figure: -






Thankyou!!!!!

 

For  Sumita Arora Type C solution ( Programing Question ) Visit our YouTube Channel    Portal Express

For Solution of Sumita Arora visit on Path Wala

 

 

 

 

 

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post