Python - Statistics Module
This module offers methods to work up on statistical data in python. There are certain functions supported by this
module, we will discuss three functions mean(), median(), mode function as given in your syllabus.
You need to import statistics module to use these function.
mean() function
The mean() function computes the average or mean of the given set of numbers. Observe the following code and
output:
>>> import statistics
>>> print([Link]([23,23,23,20]))
22.25
mode() function
This function computes the central tendency of given data. Observe the following code:
>>> print([Link]([23,22,21,20]))
23
>>> print([Link](['India','UK','Pak','India']))
India
median() function
It calculates the median (middle) values of given dataset. Observe the following code and output:
>>> import statistics
>>> print([Link]([1, 3, 5, 7, 9]))
5
>>> print([Link]([2, 4, 6, 8, 10, 12]))
7.0
>>> print([Link]([-3,0,3,6,12,15,18]))
6
Python - Random Module
The random module is a built-in module to generate the pseudo-random variables. It can be used perform some
action randomly such as to get a random number, selecting a random elements from a list, shuffle elements
randomly, etc.
Generate Random Floats
1) The [Link]() method returns a random float number between 0.0 to 1.0. The function doesn't
need any arguments. Generates random numbers of floating-point number N in the range
from 0.0<=N<1.0. Import module required to be imported before using this function.
Example:
>>> import random
>>> [Link]()
0.645173684807533
Generate Random Integers
2) [Link]() method returns a random integer between the specified integers.
Example:
>>> import random
>>> [Link](1, 100)
95
>>> [Link](1, 100)
49
>>> print([Link](10,25)) 12 >>> print([Link](10,25)) 14 >>> print([Link](10,25)) 17 >>>
print([Link](5,15)) 14 >>> print([Link](5,15)) 12 >>> print([Link](5,15)) 14 >>>
print([Link](5,15)) 8
Generate Random Numbers within Range
3) The [Link]() method returns a randomly selected element from the range created by the
start, stop and step arguments. The value of start is 0 by default. Similarly, the value o f step is 1 by
default. This function accepts the start, stop, and step values to generate random number by a specified
series. The syntax is as following: randrange(start, stop, step)
Start: It is an optional parameter that starts the range from the given number. If not given the range starts from 0.
Stop : This is mandatory parameter for this function. It specifies the upper limit for the range.
Step: This is an optional parameter which changes the value and increment the value as specified.
Example:
>>> [Link](1, 10)
2
>>> [Link](1, 10, 2)
5
>>> [Link](0, 101, 10)
>>> print([Link](3, 9)) 6 >>> print([Link](3, 9,2)) 7 >>> print([Link](3, 9,2)) 3
>>> print([Link](3, 9,2)) 3 >>> print([Link](3, 9,2)) 5
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.
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=[Link](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=[Link](10)
print(a)
Q2. Write the code to generate a random number between 7 and 17 (both inclusive)
Ans.
import random
a=[Link](7, 18)
print(a)
Q3. Write the code which always generate 0 number by using randrange( ).
Ans.
import random
a=[Link](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=[Link](______________)
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 :
[Link]()
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([Link]( )*6+10))
Q6. Write the code to generate random three digit number using random in python.
import random
print(int([Link]( )*900+100))
Q7. Write the code to generate random two digit number using random in python.
import random
print(int([Link]( )*90+10))
randint() :
This function generates a random number between the lower and upper limit (including both the limits).
Syntax of randint() is
a = [Link](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([Link](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=[Link](l+1,h) #we are not including upper limit and lower limit
if(a%2==0):
[Link](a)
if(len(L)==5):
break
print(L)
Following are the situations where we need to generate random numbers in Python
1. To generate the scratch card of online Lottery.
2. To generate captcha code.
3. To generate OTP (One Time Password)
4. Computer games like LUDO (where need to generate random number between 1 to 6)
Not included in syllabus
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([Link](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([Link](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([Link](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 :
[Link](List)
Q13. Write a program to shuffle the elements of given list
import random
r=["Amit","Sumit","Naina","Suman","Parth"]
[Link](r)
print(r)
Output:
['Suman', 'Naina', 'Sumit', 'Amit', 'Parth']
Solve the following:
Fill in the blank (Random in Python)
1. _________ module is to be imported to use randrange() function.
2. ___________ and _________ functions of random module return floating point random number.
3. A function which generates a floating point number between 0 and 1 is _____________________
4. __________ and _________ functions of random module return random integers.
5. ________________ function of random module takes no argument.
6. ________________ function change the sequence of elements in the list.
True/False (Random in Python)
1. random() function generates integers.
2. [Link](2,20) will always generate number greater than 2 and less than 20.
3. [Link]()*900 + 100 always generate 3 digits number.
4. shuffle() function can not work on tuple.
5. uniform() function always generate floating point numbers.