You are on page 1of 5

1/16/22, 7:46 AM 9.a.

Libraries and Modules

Libraries or Modules
Libraries are pre defined code

Using libraries we can use the functions or method which are not in build

We use modules to break down large programs into small manageable and organized files.
Furthermore, modules provide reusability of code.

In [2]:
# Example of Libraries

import math

import time

In [ ]:
math.sqrt

In [2]:
math.sqrt(36)

Out[2]: 6.0

In [3]:
math.pi

Out[3]: 3.141592653589793

In [3]:
pi

---------------------------------------------------------------------------

NameError Traceback (most recent call last)

<ipython-input-3-f84ab820532c> in <module>

----> 1 pi

NameError: name 'pi' is not defined


We use import keyword to use libraries

Syntax

import moduleName or

import PackageName

In [6]:
import math

math.pi

Out[6]: 3.141592653589793

In [ ]:
file:///C:/Users/rgandyala/Downloads/9.a. Libraries and Modules.html 1/5
1/16/22, 7:46 AM 9.a. Libraries and Modules

In [4]:
import time

In [6]:
time.tzname

Out[6]: ('India Standard Time', 'India Daylight Time')

In [7]:
time.time()

# time is library

# time is function

Out[7]: 1637123333.666412

In [5]:
print("sleep for ten seconds")

time.sleep(10) # it will stop execution for ten seconds

print("wake up")

sleep for ten seconds

wake up

In [3]:
import math

In [4]:
math.pi

Out[4]: 3.141592653589793

In [4]:
import datetime

x = datetime.datetime.now()

print(x)

2021-11-17 20:35:23.711246

In [5]:
import datetime

x = datetime.datetime.now()

print(x.year)

2021

In [6]:
import datetime

x = datetime.datetime(2020, 5, 17)

## yy mmm dd - ymd

print(x)

file:///C:/Users/rgandyala/Downloads/9.a. Libraries and Modules.html 2/5


1/16/22, 7:46 AM 9.a. Libraries and Modules

2020-05-17 00:00:00

In [9]:
import math

In [31]:
#min() and max() are inbuild function to find the min and max value

In [8]:
import math

x = math.sqrt(64)

print(x)

8.0

In [11]:
import math

x = math.ceil(1.4)

y = math.floor(1.4)

print(x) # returns 2

print(y) # returns 1

In [7]:
import math

In [10]:
math.sqrt(36)

Out[10]: 6.0

you can create an alias when you import a module, by using the as keyword:

In [12]:
import pandas

In [ ]:
# pandas.read_CSV(" Path ")

In [10]:
import numpy as np

In [11]:
import pandas as pd

#pd.read_CSV("path")

In [13]:
import sklearn.preprocessing

file:///C:/Users/rgandyala/Downloads/9.a. Libraries and Modules.html 3/5


1/16/22, 7:46 AM 9.a. Libraries and Modules

In [14]: sklearn.preprocessing.binarize

Out[14]: <function sklearn.preprocessing.data.binarize(X, threshold=0.0, copy=True)>

In [11]:
import sklearn.preprocessing as sp

In [17]:

Out[17]: <function sklearn.preprocessing.data.binarize(X, threshold=0.0, copy=True)>

Syntax

packages are collection of module

import packagename.libraryname

from packagename import library Name

In [20]:
# without using Alias Keyword

# we can use from keyword to import modules without using alias

In [21]:
from sklearn.preprocessing import binarize

In [22]:
# If we user * we will import all function in that module

In [23]:
from time import *

To install external Library

In [24]:
import pytroch

import keras

---------------------------------------------------------------------------

ModuleNotFoundError Traceback (most recent call last)

<ipython-input-24-ddab4d34effa> in <module>

----> 1 import pytroch

ModuleNotFoundError: No module named 'pytroch'

In [25]:
# we dont have module here in pytroch so we have to install that module in pythin

# we pip keyword to install the module

PIP
PIP is a package manager for Python packages, or modules

file:///C:/Users/rgandyala/Downloads/9.a. Libraries and Modules.html 4/5


1/16/22, 7:46 AM 9.a. Libraries and Modules

syntax

pip install modulename

Open the command prompt ( anaconda prompt ) then run this code to install the module

Some examples to install external Module

1) pip install pytroch

2) pip install keras

3) pip insall tensorflow

In [9]:
import sklearn

In [10]:
from sklearn.preprocessing import scale

In [ ]:

file:///C:/Users/rgandyala/Downloads/9.a. Libraries and Modules.html 5/5

You might also like