Class XI (As Per CBSE Board) : Computer Science
Class XI (As Per CBSE Board) : Computer Science
syllabus
2020-21
Chapter 15
Python
Modules
Computer Science
Class XI ( As per CBSE Board)
Visit : python.mykvs.in for regular updates
Python Module
math module
The math module is a standard module in Python
and is always available. To use mathematical
functions under this module, we have to import
the module using import math statement.
math.fabs()
Returns the absolute value of x
>>> import math
>>> math.fabs(-5.5)
5.5
Random Module
The random module provides access to functions that support many
operations.Perhaps the most important thing is that it allows us to
generate random numbers.
random.randint()
Randint accepts two parameters: a lowest and a highest number.
import random
print (random.randint(0, 5))
This will output either 1, 2, 3, 4 or 5.
random.random()
Genereate random number from 0.01 to 1.If we want a larger
number, we can multiply it.
import random
print(random.random() * 100)
Visit : python.mykvs.in for regular updates
Python Module
randrange()
generate random numbers from a specified range and also allowing rooms for steps to be
included.
Syntax :
random.randrange(start(opt),stop,step(opt))
import random
# Using randrange() to generate numbers from 0-100
print ("Random number from 0-100 is : ",end="")
print (random.randrange(100))
# Using randrange() to generate numbers from 50-100
print ("Random number from 50-100 is : ",end="")
print (random.randrange(50,100))
# Using randrange() to generate numbers from 50-100
# skipping 5
print ("Random number from 50-100 skip 5 is : ",end="")
print (random.randrange(50,100,5))
OUTPUT
Random number from 0-100 is : 27
Random number from 50-100 is : 48
Random number from 50-100 skip 5 is : 80
OUTPUT
3.3333333333333335
statistics.median(data)
Return the median (middle value) of numeric data, using the common “mean of middle
two” method. If data is empty, StatisticsError is raised.
import statistics
print(statistics.median([5,5,4,4,3,3,2,2]))
OUTPUT
3.5
Visit : python.mykvs.in for regular updates
Python Module
statistics.mode(data)
Return the most common data point from discrete or nominal data. The mode (when it
exists) is the most typical value, and is a robust measure of central location.If data is
empty, or if there is not exactly one most common value, StatisticsError is raised.
import statistics
print(statistics.mode([1, 1, 2, 3, 3, 3, 3, 4]))
OUTPUT
3