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.
math module:
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:
Mathematical Constants:
● [Link]: Represents the mathematical constant π (pi).
● math.e: Represents the mathematical constant e (Euler's number). Basic Mathematical
Functions:
● [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), [Link](x), [Link](x): Arcsine, arccosine, and arctangent of x,
respectively.
Hyperbolic Functions:
● [Link](x), [Link](x), [Link](x): Hyperbolic sine, cosine, and tangent of x,
respectively.
Angular Conversion:
● [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 factorial of x. Study the following examples:
EXAMPLE 1 :
Example 2:
Example 3:
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.
● [Link](data): Finds the mode (most common value) in the data.
Example 1:
Example 2:
random module
● The random module in Python is another built-in module that provides functions for
generating random numbers, sequences, and making random choices.
● It is commonly used for tasks such as random number generation, random shuffling, and
random sampling.
Import random
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 float number in the range [a, b).
● [Link](a, b): Generates a random integer in the range [a, b] (inclusive).
● [Link](sequence): Picks a random element from a sequence (list, tuple, string,
etc.).
● [Link](sequence): Shuffles the elements of a sequence randomly (in- place).
Example 1:
Possible options:
a) green
b) yellow
c) blue
d) orange
Solution:
Here, the possible values for variable random-sample are 3, 4 and 5. Hence, the possible
Outputs of above code are b) Yellow and d) orange.
Example 2:
What are the possible output/s for the following code?
Output Options:
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 option iv