Python Modules and Library: Purpose: Advantage
Python Modules and Library: Purpose: Advantage
Module is a collection of variables, functions, classes, object etc related to a particular type of entity.
Purpose : Implement modularity i.e. partition a program into several well individual components.
Advantage : Reduces complexity of development and further modification
Example:
Python Libraries
Python standard
library SciPy Library NumPy Library Matplotlib
Library
random.random()*(30-10) + 10
=29.70674695914648
2 randint(int, int) int randint(a,b) generates an random.randint(10,30) = 23
integer N between a and b,
a≤N≤b random.randint(-30,10) = -29
3 randrange(int,int,int) int 1. randrange(start,stop,step) random.randrange(10,30,5) = 25
generate an integer N between # start = 10, stop = 30, step = 5
start and stop-1 with a step
value step, random.randrange(10,30) = 17
start ≤ N < stop # start = 10, stop = 30, step = 1
These functions works on sequence data types like list and tuple
Sl. Function Name Ret. Purpose Example
No. type
1 mean(sequence) float mean/average value statistics.mean((10, 50.5, 30, 40.5)) = 32.75
2 median(sequence) float middle value in the statistics.median([10, 50, 30, 40]) = 35.0
sequence of # Average of 2 if no. of elements is even
ascending /
descending order of statistics.median([10, 50, 90, 30, 40]) = 40
values. #Only one in case of odd no. of elements
statistics.median(['a','c','p','x','z']) = 'p'
#statistics.median(['a','c','p','x','z','m']) ERROR
as middle cannot be calculated
3 mode(sequence) float Most frequent statistics.mode(['a','c','p','a','x','z','m','a'])='a'
element in a statistics.mode([10, 50, 40, 30, 40, 50, 40])=40
sequence. If multiple
elements have same #ERROR
highest frequency statistics.mode(['a','c','p','c','a','x','c','z','m','a'])
then ERROR. as a →3 and c → 3
statistics.mode([10, 50, 10, 30, 40, 30])
as 10 → 2 and 30 → 2
4. Import selective object from module -from math import pi, sqrt
<module prefix Not Required> print(round(sqrt(pi),3)) # Output = 1.772
#INR_USD.py
#Functions
def toUSD(INR):
""" Returns: INR converted to USD """
return round(INR * 0.013, 2)
def toINR(USD):
""" Returns: USD converted to INR """
return round(USD * 75.38, 2)
# Constants
Base_USD = 75.38
""" 1 USD = 75.38 INR as on 30.03.2020 """
Base_INR = 0.013
""" 1 INR = 0.013 USD as on 30.03.2020 """
'''
OUTPUT:
>>> import INR_USD
>>> help(INR_USD)
NAME
INR_USD - Conversion function between Indian Rupee and US Dollar
FUNCTIONS
toINR(USD)
Returns: USD converted to INR
toUSD(INR)
Returns: INR converted to USD
DATA
Base_INR = 0.013
Base_USD = 75.38
FILE
/home/sonam/INR_USD.py
'''
if choice.upper()[0] == 'I':
inr = float(input("Enter Amount of Rupee : "))
usd = Cur.toUSD(inr)
print("The equivalent amount in US currency is = ", usd, " USD")
else:
print("Error in selection of Menu!!!")
'''
Output:
PACKAGE