Modules libraries
Basic Introduction
Libraries libraries are collections of modules
Modules modules are collections of functions
classes variables
Python has millions of libraries so we only install
the ones we need
How to install library module
Pip install module name library name
Pip is the pipeline between PC cloud serving
Python
For this installation active internet connection is
must
If we need to uninstall then pip uninstall
once the installation is done
if we want to import module from library
from library name import module name 1 modules
we can import n no of modules
library package are simillar but slightly
different we will learn later
Python contains lots of modules libraries If
helps our task
we will learn more about the modules that
help in the Data science journey
There are many more modules for example the
ones we use for web development we don't
learn them in this course
During the pip install if there is no internet
connection the kernel will keep on waiting until
the internet connection is back
Random module
It is a module by using which we can create
random numbers
Python does not have inbuilt functionality
to generate random numbers
we import random module by using the
coma
import random
few functions inside in random module
1 random This random function will generate
a random number between o and I and this
is a float value The range includes o and 1
ex random random C
0 0890652
2 randintc this randint function will generate
random integer values we need to provide
and Ending values i a b Range
starting e
includes the values a s
ex random randint 1,10
can be 1 2,3 4 5 6,718 9,10
4
3 rand range c this randrange function will
generate a value between a specified range
This will generate integer but unlike
randint will not include and Ending
starting
Value
ex random randrangeci no
6 will be 2,3 4,5 6,718,9
4 Uniform this uniform function given a
starting and ending value will return float
values with a uniform distribution statistics
later
ex random uniform 1,4
3.6594
5 sample C This sample function gives a
subset of the total number of things Inputs
to the function are iterable object and
sample size
Sample size should be less than the
sequential data length
from iterable object set tuple list string
it will sample out the sample mentioned
choosing without replacement
ext random Sample 1,2 3,47 12 2
Claus
G Choices Simillar to sample c function
but slight difference i e sample will have
repitition of values
sample size can be more than the length
of sequential data as repetition is allowed
choosing with replacement
ext random choices abc def 1 7
Ca c e d d C f
7 shuffled this shuffle function will change
values variable is
the positional sample
taken shuffled and stored back to some
variable
This variable needs to be sequential
data
Strings and tuples are immutable and
cannot be used
If we need to apply on these first they
need to be converted back to list
ex 2 a bit d
random shuffle 2
Caba c d ay
Aliasing this is giving a temporary name
to module or library This just makes calling
easy when library name or module name is
long or complicated
This is only temporary and works as long
as the kernel is active or we need to
re execute it
ext import random as nd
so now instead of random module name we
can use red as the new alias name
ext nd sample 1,2 3,47 1 27
3,4
Q How random module is generating a
random number
Inside the random module there is a
random generator
whenever this random generator wants
to generate a random number it requires
something as a input
This input is given by a function called
seed function
ex random random e
seed function is called
Seed function will take our current time
in milliseconds as input so the random
number keeps changing
Instead if we mention the input of seed
function so for one single input we will
same random value
only get
ex nd seed lo
nd random I too
18 this will not change
How to create our own module
Python will only recognize the extention
Py for module
So we cannot create ipynb file in Jupiter
notebook
Instead we can create a tent file in python
directory and name if module name Py
when Python sees any file with spy extension
if thinks it is a module and we can
import it
module name has to be unique and we
cannot use the names of the pre existing
modules
module share the
Creating a we can
functionalities with others
est create a module maths sss py which is a
collection of functions class and variable
It takes 2 variables and returns arithmetic
operations
Maths Sss Py
def sum a s
return atb
b
def subtr a
return a b
class F
def init self a s
self a a
self D b
def malt self
return self a self b
2 10
import maths's as Mt
Mt SumCl B
a
Mt SubtrCU 3
1
Mt malt 214
x error as malt is defined
inside the class F
object of that class
Only
can access it
O Mt F 2,4
O malt C
8
mt.se
to
Assignment
Create string module without using string
methods
ex Input Abcd
we need functionality of following methods
without using the actual methods or other
string methods
D upper C
2 lower C
3 is upper C
4 islowerc
5 Capitalized
6 title C
7 is titled
8 Swap case C
9 is digits
lo is alpha C
11 is alum C
hint can use orde and chre inbuilt functions