Module : A module is a Python file where functions, constants and variables are defined.
Extension of Python module is .py
Advantages of Modules in Python
1. It makes the code reusable, means we can use the same code in different programs.
2. It makes our code easier to understand.
3. It help us to organize or manage our code in Python.
Importing Python modules
There are three ways to import a module in Python.
1. Using import statement : This is one of the most common way to use module in Python.
This method allow us to access all the functions/objects defined in the module. for example :
to use math module, we can write —
import math
To use more than one module like math and random in our code, we can write :
import math, random
After importing module in our code, we can access a specific function by writing:
<module name>.<function name>
example math.sqrt
2. Using ‘from’ statement : This method allow us to import specific function of a module. for
example
from math import ceil
To import more than one function of a module, we can write
from math import sqrt, fabs
3. Using import* statement : This statement can be used to import all functions of a specific
module like
from math import*
Types of Python Modules
There are two types of Python modules:
1. Built-in Modules
2. User defined Modules
1. Built-in Modules : There are many built in modules in Python Library. Some of the
common built-in modules are:
Math module
Random module
1. Math module : This module has many mathematical functions like ceil, floor, sqrt etc. Let
we discuss few functions of math module.
a. floor(n) : This function returns the largest integer which is less than or equal to n. for
example
import math
print(math.floor(23.45))
Output : 23
import math
print(math.floor(-40.36))
Output : -41
import math
print(math.floor(23.96))
Output : 23
b. ceil(n) : This function returns the smallest integer which is more than or equal to n. for
example
import math
print(math.ceil(23.45))
Output : 24
import math
print(math.ceil(-40.36))
Output : -40
import math
print(math.ceil(23.96))
Output : 24
c. pow(m, n) : This function returns m raise to the power n. for example
import math
print(math.pow(2,3))
Output : 8
import math
print(math.pow(4,2))
Output : 16
d. sqrt( ) : This function return the square root of a number passed as argument. for
example
import math
print(math.sqrt(16))
Output : 4.0
import math
print(math.sqrt(121))
Output : 11.0
import math
print(math.sqrt(-121))
Output : Error
e. fabs( ) : This method return the absolute value(positive value) of a number passed as an
argument. for example
import math
print(math.fabs(-9))
Output : 9.0
import math
print(math.fabs(78))
Output : 78
import math
print(math.fabs(-65.78))
Output : 65.78
Random in Python :
Whenever there is a situation where we need to generate random numbers in coding of
python, then python allow us to generate random numbers by using module RANDOM in
Python.
Following are the situations where we need to generate random numbers in Python
To generate the scratch card of online Lottery.
To generate captcha code.
To generate OTP (One Time Password)
Computer games like LUDO (where need to generate random number between 1 to 6)
Functions in random Module
There are many functions in random module in python. Let we start
randrange( ) :
This function generates a random number between the lower and upper limit (including
lower limit and excluding upper limit). for example the following code will generate a
random number less than 20 :
import random
a=random.randrange(20)
print(a)
The above code will generate any number from 0 to 19.
NOTE :
1. By default the lower limit of randrange function is 0.
2. randrange( ) always generate a number one less than the higher limit.
Q1. Write the code to generate a number less than 10.
Ans.
import random
a=random.randrange(10)
print(a)
Q2. Write the code to generate a random number between 7 and 17 (both inclusive)
Ans.
import random
a=random.randrange(7, 18)
print(a)
Q3. Write the code which always generate 0 number by using randrange( ).
Ans.
import random
a=random.randrange(1)
print(a)
Q4. Write the code to select random names from the given list of names.
import random
name=["Amit","Sumit","Naina","Suman","Parth"]
rd=random.randrange(______________)
print(name[rd])
What minimum number should we fill in the above blank so that it will print any name from
the given list. (Answer is 5)
random():
This function generates a random floating point number from 0 to 1(including 0 and
excluding 1) like 0.15895645215423562.
Syntax is :
random.random()
random( ) function takes no argument.
NOTE : It always generate floating point number between 0 and 1(including 0 and excluding
1)
Q5. Write code to generate any numbers between 10 and 15(including both)
import random
print(int(random.random( )*6+10))
Q6. Write the code to generate random three digit number using random in python.
import random
print(int(random.random( )*900+100))
Q7. Write the code to generate random two digit number using random in python.
import random
print(int(random.random( )*90+10))
randint() :
This function generates a random number between the lower and upper limit (including
both the limits).
Syntax of randint() is
a = random.randint(L,U) # where L is the lower limit and U is the upper limit.
The value of a will be in the range ( L<=a<=U)
Q8. Write a program to generate a random number between 20 and 30(including both)
import random
print(random.randint(20,30))
Q9. Write a program which adds any random five even numbers in a list that falls between
the highest and the lowest number.(Both highest and lowest numbers are accepted from
the user.)
import random
L=[]
h = int(input("Enter the highest limit"))
l=int(input("Enter the lowest limit"))
while(i):
a=random.randrange(l+1,h) #we are not including upper limit and lower limit
if(a%2==0):
L.append(a)
if(len(L)==5):
break
print(L)
uniform()
This function is used to generate a random floating point number between the lower limit
and upper limit (excluding both).
Q10. Write the code to generate random floating point number between 10 and 20.
import random
print(random.uniform(10,20))
Output :
12.123199345853024 (It could be any other number)
choice() :
This function is used to select any random element from any sequence like any list, tuple or
dictionary.
Q11. Write the code to select any random element from given list.
import random
L = [30,40,50,60,10,90]
print(random.choice(L))
Output :
40(It could be any other element from the list)
Q12. Write the code to select any random element from given tuple.
import random
L = (30,40,50,60,10,90)
print(random.choice(L))
Output :
40(It could be any other element from the list)
shuffle():
This function is used to shuffle(change the sequence) the elements in the list.
Syntax is :
random.shuffle(List)
Q13. Write a program to shuffle the elements of given list
import random
r=["Amit","Sumit","Naina","Suman","Parth"]
random.shuffle(r)
print(r)
Output:
['Suman', 'Naina', 'Sumit', 'Amit', 'Parth']