Functions defined in modules
Pre-defined functions that are in particular
modules and can only be used when
corresponding module is imported.
e.g.
import math
sin(), cos(),abs(),floor() etc
Modules
Modules
•is a part of a program
•used for dividing up the program into smaller, more easily
understood, reusable parts.
A module is a file containing
[Link] definitions and statements.
[Link] can define functions, classes and variables.
3.A module can also include runnable code.
[Link] related code into a module makes the code easier to
understand and use.
Python
Module
Python provide inbuilt standard
modules, like math, random etc.
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.
How to use math function
import math
[Link](4)
Functions using libraries(Functions defined in Modules)
Functions available in Python Math Module
Python
Module
[Link]()
The [Link]() method returns the square root of a given number.
>>>[Link](100)
10.0
>>>[Link](3)
1.7320508075688772
Python
Module
[Link]() and [Link]()
The ceil() function approximates the given number to the smallest integer,
greater than or equal to the given floating point number.
The floor() function returns the largest integer less than or equal to the given
number.
>>>[Link](4.5867)
5
>>>[Link](4.5687)
4
Python
Module
[Link]()
The [Link]() method receives two float arguments, raises the first to
the second and returns the result.
In other words, pow(2,3) is equivalent to 2**3.
>>>[Link](2,4)
16.0
Python
Module
[Link]()
Returns the absolute value of x
>>> import math
>>> [Link](-5.5)
5.5
The math module contains functions for calculating various
trigonometric ratios for a given angle.
The functions (sin, cos, tan, etc.) need the angle in radians as an
argument.
>>> [Link](270)
-0.1760459464712114
Write the corresponding Python expressions
for the following mathematical expressions:
Python
Random Module Module
The random module provides access to functions that support
many [Link] the most important thing is that it allows
us to generate random numbers.
[Link]()
Randint accepts two parameters: a lowest and a highest number.
import random
print ([Link](0, 5))
This will output either 0,1, 2, 3, 4 or 5.
[Link]()
Genereate random number from 0.01 to [Link] we want a larger
number, we can multiply it.
import random
print([Link]() * 100)
Python
Module
randrange()
generate random numbers from a specified range and also allowing rooms for steps to be
included.
Syntax : [Link](start(opt),stop,step(opt))
import random
# Using randrange() to generate numbers from 0-100
print ("Random number from 0-100 is : ",end="")
print ([Link](100))
OUTPUT
Random number from 0-100 is : 27
# Using randrange() to generate numbers from 50-100
print ("Random number from 50-100 is : ",end="")
print ([Link](50,100))
OUTPUT
Random number from 50-100 is : 48
# Using randrange() to generate numbers from 50-100 # skipping 5
print ("Random number from 50-100 skip 5 is : ",end="")
print ([Link](50,100,5))
OUTPUT
Random number from 50-100 skip 5 is : 80
Python
statistics module Module
This module provides functions for calculating mathematical statistics of numeric
(Real- valued) data.
[Link](data)
Return the sample arithmetic mean of data which can be a sequence or
[Link] arithmetic mean is the sum of the data divided by the number of data
points(AVERAGE).
import statistics
print([Link]([5,3,2]))
OUTPUT
3.3333333333333335
[Link](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([Link]([5,5,4,4,3,3,2,2]))
OUTPUT
3.5
Python
Module
• [Link](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 [Link] data is empty, or if there is
not exactly one most common value, StatisticsError is raised.
• import statistics
• print([Link]([1, 1, 2, 3, 3, 3, 3, 4]))
• OUTPUT
•3