Using Python Libraries Class 12 Cs Python Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

USING PYTHON LIBRARIES

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)

•Imported models can be given alias name.


Syntax:- import <module> as <alias name>
Example:- >>>import math as a
>>>a.sqrt(16)

2) Importing Selected Objects From A Module


•To import some selected items, you can use following syntax:-
from <module name >import<object name>
For example:
>>>from math import sqrt

•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 *

Using Python Standard Libraries Functions And Modules

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:-

i) len():- Returns the length of a sequence or iterable e.g., lenf'abc") gives 3.

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.

v) float():- Converts a float-convertible string to integer, e.g., float('12.2') will give


12.2.

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'>.

Python's Built in String Functions


i) <Str>.join(<string iterable>)
•It joins a string or character (i.e., <str>) after each member of the string
iterator i.e., a string based sequence.
ExampleExample:-
>>>"***".join("Hello")
'H***e***l***l***o'

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


'Hello***Python'

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']

iii) <Str>.replace(<word to be replaced>, <replaced word>)


•It replaces a word or part of the string with another in the given string <str>.
Example:-
>>>"I Love Python". replace ("Python", "Programming")
"I Love Programming"
2) Working With Some Standard Library Modules

Using Random Module


Python has module random that provides random number generators. To use
random number generators in Python program, random module needs to be
imported using import command, e.g., import random

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


i.e., 0.0 ≤ N < 1.0.
Example:-
>>>import random
>>>print(random.random())
0.022472947483

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

iii) 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.
Example:-
>>>random.uniform(11, 55)
41.38562846284629

iv) random.randrange(stop) or random.randrange(start, stop, [ steps]) : - It returns


a randomly selected element from range ( start, stop, step ) .
Example:-
>>>random.randrange(23, 47, 3)
38
Using String Module

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

i) string.ascii_letters :- it returns a string containing all the collection of


ASCII letters.

ii) string.ascii_lowercase :- it returns a string containing all the lowercase


ASCII letters, i.e., 'abcdefghijklmnopqrstuvwxyz'.

iii) string.ascii_uppercase :- it returns all the uppercase ASCII letters, i.e.,


,ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

iv) string.digits :- it returns a string containing all the digits Python allows,
i.e., the string '0123456789'.

v) string.hexdigits :- it returns a string containing all the hexadecimal digits


Python allows, i.e., the string '0123456789abcdefABCDEF'.

vi) string.octdigits :- it returns a string containing all the octal digits Python
allows, i.e., the string '01234567'.

vii) string.punctuation :- it returns a string of ASCII characters which are


considered punctuation characters, i.e., the string ‘!”#$%’()*+,-
./:;?@[\]^_‘{|}~’

viii) capwords(<str>, [sep=None|) :- it splits the specified string <Str> into


words using <Str>.split( ). Then it capitalizes each word using
<Str>.capitalize( ) function. Finally, it joins the capitalized words using
<Str>.join().

If the optional second argument sep is absent or is None, it will remove


leading and trailing whitespaces and all inside whitespace characters
are replaced by a single space.

Creating A Python Library


As there are numerous libraries available which can be installed and used in
programs such as NumPy, SciPy, tkinter etc, we can create our own libraries.
Package

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.

Procedure For Creating Packages

i) Create a new folder named D:\MyApp.


ii) Inside MyApp, create a subfolder with the name 'mypackage'.
iii) Create an empty __init__.py file in the mypackage folder.
iv) Using IDLE, create module functions.py in mypackage folder.
Example:-
def sum(x,y):
return x+y

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

To use and install python library you need to do the following:

i) Import the library using import command:


Import <full name of library>
ii) Use the functions, attributes etc. defined in the library by giving their full
name.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy