Python modules
● In Python, a module is a file containing Python code that defines
variables, functions, and classes.
● Modules allow you to organize and reuse code by breaking it into
separate files, making it easier to maintain and understand complex
programs.
● Python's standard library comes with a vast collection of built−in
modules that cover various functionalities
● If needed, you can also create your own custom modules.
● To use a module in your Python code, you need to import it using the
import statement.
● The math module in Python is a built−in module that provides
various mathematical functions and constants.
● It is part of the Python Standard Library i.e. it does not require any
additional installation to use.
● To use the math module, you need to import it at the beginning of your
Python script.
● Once you've imported the module, you can access its functions and
constants using the math prefix.
Here are some commonly used functions and constants provided by the
math module:
math module
● [Link]: Represents the mathematical constant u (pi).
● math.e : represents the mathematical constant e.
● [Link](x): Returns the square root of x.
● [Link](x, y): Returns x raised to the power y.
● [Link](x): Returns the exponential of x (e^x).
● [Link](x, base): Returns the logarithm of x to the specified
base (default base is e). Trigonometric Functions (all angles are
in radians):
● [Link](x), [Link](x), [Link](x): Sine, cosine, and tangent of x,
respectively.
● [Link](x): Converts x from radians to degrees.
● [Link](x): Converts x from degrees to radians. Miscellaneous:
● [Link](x): Returns the smallest integer greater than or equal to x.
● [Link](x): Returns the largest integer less than or equal to x.
● [Link](x): Returns the absolute value of a number.
>>> import math
>>> [Link](4.5)
4
>>> [Link](4.5)
5
>>> [Link](2,4)
16.0
>>> [Link](25)
5.0
>>> [Link](-5)
5.0
>>>[Link](2,5)
32.0
Statistics module:
● The statistics module in Python is another built−in module that
provides functions for working with statistical data.
● It offers a variety of statistical functions to compute measures like
mean, median, standard deviation, variance, etc.
● The statistics module is part of the Python Standard Library, so there's
no need to install any additional packages to use it.
● Here are some commonly used functions provided by the statistics
module:
● [Link](data): Calculates the arithmetic mean (average) of the
data.
● [Link](data): Computes the median value of the data. First
the data will be arranged in ascending/descending order and then the
middle data will be taken.
● [Link](data): Finds the mode (most common value) in the
data. If there is more than one data will same frequency, then the first
occurrence will be considered.
>>> import statistics
>>> [Link]([10,20,30,30,10])
20
>>> [Link]([10,20,30,30,10])
20
>>> [Link]([10,20,30,30,10])
10
>>> [Link]([10,20,30,30,10,30])
25.0
random module
● The random module in Python is another built−in module that
provides functions for generating random numbers, and sequences,
and making random choices.
● It is commonly used for tasks such as random number generation,
random shuffling, and random sampling.
Here are some commonly used functions provided by the random module:
● [Link](): Generates a random float number in the
range [0.0, 1.0).
● [Link](a, b): Generates a random integer in the range [a, b]
(inclusive).
● [Link](a.b) : Generates a random integer in the range
[a,b)
Example 1:
What is the possible outcome/s of the following code?
Possible options:
a) green b) yellow c) blue d) orange
Here, the possible values for the variable random sample are 3, 4 and 5.
Hence, the possible Outputs of the above code are b) Yellow and d)
orange.
Example 2:
What are the possible output/s for the following code?
i . 29 : 26 : 25 : 28 : ii. 24 : 28 : 25 : 26 :
iii. 29 : 26 : 24 : 28 : iv. 29 : 26 : 25 : 26 :
Solution : option iv
Example 3:
What are the possible outcome/s for the following code:
Output Options:
i. 103#102#101#100# ii. 100#101#102#103#
iii. 100#101#102#103#104# iv. 4#103#102#101#100#
Solution: option i and iv