Using Python Libraries

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

Chapter 2 Python Libraries

Python Libraries
Library is a collection of modules (and package) that contains a specific
type of applications or requirement. A library can have multiple module in
it. Some commonly used python libraries

1 Python standard library:- This library is distributed with Python that


contains module for various type of functionalities. Some commonly used
modules of Python standard libraries are

i) math module which provides mathematical functions to support different


types of calculation
ii) cmath module which provides mathematical function for complex
function.
iii) random module which provides function for generating pseudo random
number.
iv) statistics module which provides mathematical statistics function.
v) urlib module which provides URL handling functions so that you can
access websites from with in your program.

2 numpy Library :- This function provides some advance functionalities


along with tools to create and manipulate numeric array.

3 SciPy Library:- This is another useful library that offers algorithm and
mathematics tools for scientific calculation.
4 tkinter library:- This library provides traditional Python user interface
toolkit and help you to create user-friendly GUI interface for different types
of application.
5 matplotlib library:- This library offers many functions and tools to
produce quality output in variety of format such as plots, charts ,graphics.

What is modules:- The act of partitioning a program into individual


components (Known as modules) is called modularity. A module is a
separate unit. The justification for partitioning a program is that
* it reduce its complexity to some degree and
* it creates a number of well defined documented boundaries with in the
program.
It content can be reused in other program without having to rewrite or
recreate them.
Structure of Python Module :- A Python module can contain just function. A
Python module is normal file (.py file) contain one or more of the following
objects related to a particular task.
Docstring :- Triple quotes comment , useful for documentation purpose. For
documentation the docstring should be the first string store
inside a module/Function body/Class.
Variables and Constants :- Labels for data.
Classes :- Template/Blueprints to create object of a certain kind.
Objects :- Instance of class. In general objects are representation of real or
abstract entity.
Statements :- Instructions.
Functions :- Name group of instructions.
Modules Import Other modules
Variables (Python)
Variables
Functions Function
Class/Types
Variables
Import
Other modules
Classes
(Python)
Member Variables
Function
Methods Class/Types
#tempConversion.py
‘’’Conversion function between Fahrenheit and Centigrade’’’
#Function
def to_centigrade(x):
‘’’Return: x converted to Centigrade’’’ Docstring
return 5*(x-32)/9
def to_fohrenheit(x):
‘’’Return: x converted to Fahrenheit’’’ Two function definition inside
return 9*x/5.0+32 module tempConversion.py
#Constant Two
FREEZING_C=0.0 # Water freezing temp (in Celsius) constant
FREEZING_F=32.0 # Water freezing temp (in Fahrenheit) defined
Name of Module :- tempConversion

Module File Name :- tempConversion.py

Contains :- Two Functions i) to_centigrade() ii) to_fohrenheit()


Two Constant i) FREEZING_C ii) FREEZING_F
Three Docstring (Triple quote string)

>>> import tempConversion


>>> help(tempConversion)
Help on module tempConversion
NAME
Conversion function between Fahrenheit and Centigrade
FILE
FilePath/tempConversion.py
FUNCTION The docstring of modules
displayed documentation
to_centigrade(x):
Return: x converted to Centigrade
to_fohrenheit(x):
Return: x converted to Fahrenheit
DATA
FREEZING_C=0.0
FREEZING_F=32.0
Importing Module in Python Program :- Python provides import statement
to import modules in a program. The import statement can be used in two
form.

i) To import entire module :- the import<module> command

ii) To import selected object from module :-


the from <module> import <object> command
Importing Module in Python
Importing Single Object Importing Single Object

from math import pi from tempConversion import


print(pi) o/p 3.14 FREEZING_F
print(FREEZING_F) o/p 32.0
Importing Selected Object of a
Module Importing Selected Object of a
from math import sqrt , pow Module
from tempConversion import
print(sqrt(16)) to_centigrade,to_fohrenheit
print(pow(5,2)) print(to_centigrade(100))
print(to_fohrenheit(100))
Importing Module in Python
Importing all object of module Importing all object of module

from math import * from tempConversion import *

print(pi)
print(sqrt(16)) print(FREEZING_F)
print(pow(5,2)) print(FREEZING_C)
print(ceil(2.9)) print(to_centigrade(100))
print(floor(2.9)) print(to_fohrenheit(100))
Using Python Standard Library Functions and Modules :- The Python
interpreter has a number of functions built in to it that are always available
you need not import any modules for them. The built in function are part of
current namespace of Python interpreter.

num=17 tnum=5.555682
rnum1=round(tnum)
onum=oct(num)
rnum2=round(tnum,3)
hnum=hex(num) print(rnum1)
print(“Octal number conversion ”,onum) print(rnum2)
print(“Hexadecimal conversion ”,hnum)
o/p Octal number conversion O013 o/p 5.555682
5.556
Hexadecimal conversion Ox11
‘*’.join(‘Hello’) op H*e*l*l*o
‘****’.join(‘TRIAL’) op T****R****I****A****L
‘$$’.join(‘Trial’,’Hello’) op Trial$$Hello
‘####’.join (‘Trial’,’Hello’,’New’) op Trial####Hello####New
‘I Love Python’.split() op I, Love, Python
‘I Love Python’.split(‘ ‘) op I, Love, Python
‘I Love Python’.split(‘o’) op I L, ve Pyth, n
‘I Love Python’.replace(‘Python’,’Programming’) op I Love Programming
Using Random Module

random() function return a random


import random floating point number N in the
range.[0.0,0.9] N may be 0.0 < 1.0
print(random.random) randint(a , b) function return a
print(random.randint(1,50)) random integer N in range(a , b)
between a and b including a and b
print(random.random()*(35-15)+15) op Give number between 15 to 35
print(random.randint(3,10) - 3) op Give number between 0 to 7
Using Random Module
Given the following Python code, which is represented four time. What
could be the possible set of outputs out of given four sets (dddd represent
any combination of digit)

import random
print(15 + random.random() * 5 )

i) 17.dddd 19.dddd 20.dddd 15.dddd


ii) 15.dddd 17.dddd 19.dddd 18.dddd
iii) 14.dddd 16.dddd 18.dddd 20.dddd
iv) 15.dddd 15.dddd 15.dddd 15.dddd
Using Random Module
import random
import math
print(str( int( math.pow( random.randint( 2,4),2 ) ) ) , end=‘ ‘ )
print(str( int( math.pow( random.randint( 2,4),2 ) ) ) , end=‘ ‘ )
print(str( int( math.pow( random.randint( 2,4),2 ) ))

What could be possible output out of given choice.


i) 2 3 4 ii) 9 4 4 iii) 16 16 16 iv) 2 4 9 v) 4 9 4 vi) 444

import random
import math
print( math.ceil( random.random() )) What will be possible output

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