Using Python Libraries Class 12 Cs Python Notes
Using Python Libraries Class 12 Cs Python Notes
Using Python Libraries Class 12 Cs Python Notes
Notes
Library
Library is a collection of models and packages that together cater to a specific type of
applications or requirements. A library can have multiple models in it.
Some examples of python libraries are listed below:-
1) Python standard library
-math module
-cmath module
-random module
-statistics module
-Urllib module
2) NumPy library
3) SciPy library
4) tkinter library
5) Malplotlib library
Module
•The act of partitioning a program into individual components (known as modules) is called
as modularity.
•The justification for partitioning a program is that it reduces its complexity to some degree
and it creates a number of well defined, documented boundaries within the program.
•A python module can contain much more than just functions. A python module is a normal
python file (.py file) containing one or more of the following objects related to a particular
task:-
-docstrings
-variables and constants
-classes
-objects
-statements
-functions
1
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) Importing Entire Module
•The imports statement can be used to import entire module.
Syntax :- import <module 1 >, <module 2>....
Example:-
>>>import time
>>>Import decimals, fractions
•After importing a module, any function or definition of the imported module can be
used as per following syntax:-
<module-name>.<function-name> ()
For example:
>>>import math
>>>math.sqrt(16)
•To import multiple objects from the module, you can use following syntax :-
from <module name>import<object name>,<object name>,<object name>....
For example:-
>>>from math import sqrt, pi, pow
•To import all items from the module, you can use following syntax:-
from <module name> import *
For example:-
>>>from math import *
2
Python standard library is very extensive that offers many built-in functions. this
library is by default available so it don’t need to be imported separately.
1) Using Python Built In Functions
Python's built in Mathematical Functions
Python provides many mathematical built in functions that are given below:-
ii) pow():- Returns when a and b are given as arguments, e.g., po'v(3' 4) gives
81.
iii) str():- Converts a number to a string, e.g., str(12) will give '12' and str(12.4) will
give '12.4'.
iv) int():- Converts an integer-convertible string to integer, e.g., int('12') will give
12.
vi) range():- Returns an immutable sequence type, e.g., range(3) will give
sequence 0, 1, 2.
vii) type():- Returns the data type of passed argument, e.g., type(12) will give
<class 'int'>.
ii) <Str>.split(<string/char>)
•It splits a string (i.e., <str>) based on given string or character (i.e.,
<string/char>) and returns a list containing split strings as members.
ExampleExample:-
>>>"I Love Python". split()
['I', 'Love', 'Python']
3
>>>"I Love Python". split ("o")
['I L','ve Pyth','n']
ii) randint (a, b) : - It returns a random integer N in the range (a, b) , i.e. , a ≤ N ≤
b (both range-limit are inclusive).
Example:-
>>>print(random.randint(15, 35))
16
4
Python has module by the name string that comes with many constants and
classes. To use any of the constants/functions defined in the string module, it
needs to be imported, e.g., import string
iv) string.digits :- it returns a string containing all the digits Python allows,
i.e., the string '0123456789'.
vi) string.octdigits :- it returns a string containing all the octal digits Python
allows, i.e., the string '01234567'.
5
• A package is a collection of python module under a common name space, created
by placing different modules on a single directory along with some special files (such
as __init__.py). A library can have one or more packages and subpackages.
•In a directory structure in order for a folder (containing different modules i.e., .py
files) to be recognised as a package, a special file namely __init__.py must also be
stored in the folder even if the file __init__.py is empty.
Structure of a Package
As you have read above that an __init__.py file must be part of the folder for python
files to be recognised as a package.
def average(x,y):
return (x+y)/2
def power(x,y):
return x**y
The package is ready now. Now we can import it’s modules and use its
functions.
6
Using/Importing Python Libraries