0% found this document useful (0 votes)
4 views

Python modules

The document explains Python modules, which are files containing functions, constants, and variables, with a .py extension. It highlights the advantages of using modules, such as code reusability and better organization, and details how to import modules, specifically the math module and its functions like floor, ceil, pow, sqrt, and fabs. Additionally, it discusses the random module for generating random numbers in various applications like online lotteries and computer games.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python modules

The document explains Python modules, which are files containing functions, constants, and variables, with a .py extension. It highlights the advantages of using modules, such as code reusability and better organization, and details how to import modules, specifically the math module and its functions like floor, ceil, pow, sqrt, and fabs. Additionally, it discusses the random module for generating random numbers in various applications like online lotteries and computer games.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PYTHON MODULES

Module: A module is a Python file where functions, constants and variables are defined.
Extension of Python module is.py
Advantages of Modules in Python
1.It makes the code reusable, means we can use the same code in different programs.
2.It makes our code easier to understand.
3.It help us to organize or manage our code in Python.
Importing Python modules
Ways to import a module in Python.
*Using import statement: This is one of the most common way to use module in Python. This
method allow us to access all the functions/objects defined in the module. for example: to use
math module, we can write - import math
Math module:
1. Math module: This module has many mathematical functions like ceil, floor, sqrt etc. Let we
discuss few functions of math module.
a. floor(n): This function returns the largest integer which is less than or equal to n. for example
import math print(math.floor(23.45)
Output: 23
import math print(math.floor(-40.36)
Output: -41
import math print(math.floor(23.96)
Output: 23
b. ceil(n): This function returns the smallest integer which is more than or equal to n. for
example
import math print(math.ceil(23.45)
Output: 24
import math print(math.ceil(-40.36)
Output: -40
import math print(math.ceil(23.96)
Output: 24
c. pow(m, n): This function returns m raise to the power n. for example
import math print(math.pow(2,3))
Output : 8
import math
print(math.pow(4,2))
Output: 16
d. sqrt(): This function return the square root of a number passed as argument. for example
import math
print(math.sqrt(16))
Output: 4.0
import math
print(math.sqrt(121))
Output: 11.0
import math
print(math.sqrt(-121))
Output : Error
e. fabs(): This method return the absolute value(positive value) of a number passed as an
argument. for example
import math print(math.fabs(-9))
Output: 9.0
import math print(math.fabs(78))
Output: 78
import math print(math.fabs(-65.78))
Output: 65.78
Random module:
When ever there is a situation where we need to generate random numbers in coding of python,
then python allow us to generate random numbers by using module RANDOM in Python.
Following are the situations where we need to generate random numbers in Python
1. To generate the scratch card of online Lottery.
2. To generate captcha code.
3. To generate OTP (One Time Password)
4. Computer games like LUDO (where need to generate random number between 1 to 6)

You might also like