Welcome, with new chapter’s Comprehensive notes computer science class 11 python modules. In this article, we will discuss the basics of python modules. So here we go!
Class 11 python modules
Let’s understand class 11 python modules with example. It is always a good practice to divide your work into different units. For example, like a book, you can see all the contents of a book are divided into different chapters. If it is not done then it looks boring to read for you.
A python module is .py file contains variables, class definitions, statements and functions to solve specific problem.
Similarly in programming, we create a module. The modularity is one of the important feature of programing. It allows to divide the code into different small units. The advantages of modularity are as following:
- Reduces the complexity of program
- Make reading easy
- Make the program to easily understand
- Offers reusability
In the next section of class 11 python modules, we will discuss about standard library modules.
Standard Library Modules
Python offers a variety of standard library modules. The library modules refer to preloaded with some pre-defined modules in the python library. These preloaded modules are known as standard library modules. For example math, random, statistics etc.
Follow the below given link to read about structure of python module:
In the next section of class 11 python modules we are going to discuss about random() module.
Using random() module
Python offers a random module to generate random numbers. It means the number is generated by the system itself from the specified range.
There are two most common functions used to generate random numbers in python.
- random() – 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.
- randint(a,b): It generates random number between a to b, both inclusive.
Using random() function
import random print(random.random())
Generate number by specifying number greater than using random()
Observe the following code and output:
In this screenshot you can see different methods of generating random from a specific series using random() function.
The first function generates any random number between 0 to 10. The second line of code generated a random number between 10 to 25.
Try this code different time and observe the output. This function mostly used with games coding.
Using randint(a,b) function
The randint(a,b) function requires two parameters i.e. upper limit and lower limit numbers to generate a series. Observe the following lines of code :
>>> print(random.randint(10,25)) 12 >>> print(random.randint(10,25)) 14 >>> print(random.randint(10,25)) 17 >>> print(random.randint(5,15)) 14 >>> print(random.randint(5,15)) 12 >>> print(random.randint(5,15)) 14 >>> print(random.randint(5,15)) 8 >>>
Using randrange() function
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.
Observe the following code and output for understanding:
>>> print(random.randrange(3, 9)) 6 >>> print(random.randrange(3, 9,2)) 7 >>> print(random.randrange(3, 9,2)) 3 >>> print(random.randrange(3, 9,2)) 3 >>> print(random.randrange(3, 9,2)) 5 >>>
Now in the next section of class 11 python modules we are going to discuss statistics module function.
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.
The mean() function computes the average or mean of the given set of numbers. Observe the following code and output:
>>> import statistics >>> print(statistics.mean([23,23,23,20])) 22.25 >>> print(statistics.mean)
This function computes the central tendency of given data. Observe the following code:
>>> print(statistics.mode([23,22,21,20])) 23 >>> print(statistics.mode(['India','UK','Pak','India'])) India >>>
It calculates the median (middle) values of given dataset. Observe the following code and output:
>>> import statistics >>> print(statistics.median([1, 3, 5, 7, 9])) 5 >>> print(statistics.median([2, 4, 6, 8, 10, 12])) 7.0 >>> print(statistics.median([-3,0,3,6,12,15,18])) 6 >>>
So here I am concluding this article. Like and share your views on this article in the comment section. Thank you for our blog visit.