0% found this document useful (0 votes)
12 views3 pages

Lec-17 Module in Python

A module in Python is a collection of functions grouped together, allowing for better code management and reuse across different programs. To use a module, it must be imported, granting access to its functions, which can be called using the syntax 'modulename.functionname()'. The document also outlines several built-in modules such as math, random, and statistics, detailing their functions and usage examples.

Uploaded by

Bong Chess Hub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Lec-17 Module in Python

A module in Python is a collection of functions grouped together, allowing for better code management and reuse across different programs. To use a module, it must be imported, granting access to its functions, which can be called using the syntax 'modulename.functionname()'. The document also outlines several built-in modules such as math, random, and statistics, detailing their functions and usage examples.

Uploaded by

Bong Chess Hub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module

While a function is a grouping o f instructions, a module is a grouping of


functions. As we know that when a program grows, function is used to simplify the
code and to avoid repetition. For a complex problem, it may not be feasible to
manage the code in one single file. Then, the program is divided into different
parts underdifferent levels, called modules. Also, suppose we have created some
functions in a program and we want to reuse them in another program. In that
case, we can save those functions under a module and reuse them.A module is
created as a python (.py) file containing a collection of function definitions.

To use a module, we need to import the module. Once we import a module, we


can directly use all the functions of that module. The syntax of import
statement is as follows:
import modulename1 [,modulename2, …]
This gives us access to all the functions in the module(s). To call a function of
a module, the function name should be preceded with the name of the module
with a dot(.) as a separator.
The syntax is as shown below:
modulename.functionname()
Built-in Modules
Python library has many built-in modules that are really handy to
programmers. Let us explore some commonly used modules and the
frequently used functions that are found in those modules:
• math
• random
• statistics

Module name : math


It contains different types of mathematical functions. Most of the functions
in this module return a float value.
import math
Function Arguments Returns Example Output
Syntax
math.ceil(x) x may be an ceiling value of >>> math.ceil(-9.7)
integer or x -9
floating point >>> math.ceil (9.7)
number 10
>>>
math.ceil(9) 9
math.floor(x) x may be an floor value of x >>> math.floor(-4.5)
integer or -5
floating point >>> math.floor(4.5)
number 4
>>> math.floor(4)
4

math.fabs(x) x may be an absolute value >>>math.fabs(6.7


integer or floating of x ) 6.7
point number >>> math.fabs(-
6.7) 6.7
>>>math.fabs(-
4) 4.0
math.factorial(x) x is a positive factorial of x >>>math.factorial(5)
integer 120
math.fmod(x,y) x and y may be an x % y with sign >>> math.fmod(4,4.9)
integer or floating of x 4.0
>>>math.fmod(4.9,4.9)
point number 0.0
>>>math.fmod(-
4.9,2.5)
-2.4
>>>math.fmod(4.9,-
4.9)
0.0
math.gcd(x,y) x, y are positive gcd (greatest >>>math.gcd(10,2
integers common )2
divisor) of x
and y
math.pow(x,y) x, y may be an xy (x raised to >>> math.pow(3,2)
integer or floating the power y) 9.0
>>> math.pow(4,2.5)
point number 32.0
>>>math.pow(6.5,2
) 42.25
>>>math.pow(5.5,3.2
) 233.97
math.sqrt(x) x may be a positive square root of x >>>math.sqrt(144
integer or floating ) 12.0
point number >>> math.sqrt(.64)
0.8
math.sin(x) x may be an integer sine of x in >>>math.sin(0)
or floating point radians 0
number >>> math.sin(6)
-0.279
Module name : random
This module contains functions that are used for generating random numbers.
import random

Function Argument Return Example


Syntax Output
random.random() No Random Real >>> random.random()
argume Number 0.65333522
nt (void) (float) in the
range
0.0 to 1.0
random. x, y are Random integer >>>
randint(x,y) integers between x and y random.randint(3,7) 4
such that >>> random.randint(-3,5)
x <= y 1
>>> random.randint(-5,-3)
-5.0
random. y is a positive Random integer >>> random.randrange(5)
randrange(y) integer between 0 and y 4
signifying the
stop value
random. x and y are Random integer >>>
randrange(x,y) positive between x and y random.randrange(2,7) 2
integers
signifying the
start and stop
value
1. Module name : statistics
This module provides functions for calculating
statistics of numeric (Real-valued) data.
import statistics

Function Syntax Argument Return Example


Output
statistics.mean(x) x is a arithmetic >>> statistics.
numeric mean mean([11,24,32,45,51])
sequence 32.6
statistics.median(x) x is a median >>>statistics.
numeric (middle median([11,24,32,45,51])
sequence value) of x 32
statistics.mode(x) x is a mode (the >>> statistics.
sequence most mode([11,24,11,45,11]) 11
repeated >>> statistics.
value) mode(("red","blue","red"))
'red'

You might also like