Page 248 - Computer Science Class 11 Without Functions
P. 248
randint(a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.
>>> random.randint(1,10)
4
Write a function call that would randomly generate 0 or 1.
The randrange(start, stop=None, step=1) function of the random module enables us to generate a random
number between start and stop, not including stop.
>>> help(random.randrange)
Help on method randrange in module random:
randrange(start, stop=None, step=1) method of random.Random instance
Choose a random item from range(start, stop[, step]).
This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
>>> random.randrange(1,11,2) # outputs any one odd number excluding 11.
5
10.1.2 Statistics Module
Python module statistics enables us to compute statistical quantities like mean, median, and mode. Let us recall
the terms mean, median, and mode. Mean refers to the average value in a dataset. Median refers to the middle value
in a dataset arranged in ascending or descending order. Mode refers to the value that occurs most frequently in the
dataset. Next, let us examine some examples of the use of statistical functions:
>>> import statistics
>>> statistics.mean([5, 3, 1, 2, 4, 7, 6, 8, 10, 9])
5.5
>>> statistics.median([5, 3, 6, 8, 9, 12, 5])
6
>>> statistics.mode([5, 3, 6, 8, 9, 12, 5])
5
Of course, there are many more functions included in the statistics module, and you may find details of those
functions by invoking the help function as help(statistics).
What value will the following function call yield?
statistics.median([2, 4, 6, 8])
What value will the following function call yield?
statistics.mode([2, 4, 4, 6, 6, 6, 8, 10, 10])
10.2 User-defined Modules
A Python module is nothing but a file comprising Python code. It can be imported into another program, just like
other modules available in Python. In this section, we will build a module geometry.py that enables computation
of the area and perimeter of rectangles and triangles. We encourage you to extend this module by incorporating
the definitions of the functions for computing the area and perimeter of some more geometric figures. Currently,
the module comprises the definition of the functions to compute the area and perimeter of rectangles and triangles
in the file geometry.py. As the computations are self-explanatory, we do not describe these functions further in
the text.
246 Touchpad Computer Science-XI

