Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
128 views

Python Modules notes

Modules in python
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

Python Modules notes

Modules in python
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Rising Academy

Class 11, Computer Science


Introduction to Python Modules

What is module in Python?


Module is a file consisting of code that can define the functions, classes and variables related
to a particular task. Modules are used to divide our python code in smaller parts.

Python Library
Python library are also known as python package. It is collection of python model which are
further divided into sub module.

Advantages of Python Module


1. Similar type of code are kept under a same place.
2. To logically organize our Python code.
3. We can easily import these modules in other programs too.
4. Reusability of code increases.

Importing python Modules


There are three ways to import python modules in your program.
1. using import statement
2. using from keyword
3. using import *

Retrieving objects from a Module


Using dir() function we can get names of all objects of a module.

Example :
import math
print( dir(math) )

What is Module Aliasing ?


Python provides a feature of renaming a module at the time of import known as aliasing.
Example:
import math as m

Types of Python module


1. Built in module (Already created modules)
2. User defined module (modules which are created by user)

math Module
It contains different types of mathematical functions.
Commonly used functions in math module.
1. pi - gives the value of pi (3.14159)
2. e - gives the value of e (2.718281)
3. ceil( x ) – returns the smallest integer that is greater than or equal to x
>>> math.ceil(3.4)
Output 4
4. floor(x) – returns the largest integer that is less than or equal to x
>>> math.floor(100.12 )
Output 100
>>> math.floor( - 45.12)
Output -56
5. pow(x, y) – returns the (y power of x)
6. sqrt(x) – returns the squarroot of x
7. fabs(x) – returns the absolute value of x
>>> math.fabs(500.23)
Output 500
>>> math.fabs(-200)
Output 200
>>> math.fabs(-15.33)
Output 15.22
8. log10(x) – returns the base 10 logarithm of x
9. sin(x) – returns the sine of x
10. cos(x) – returns the cosine of x
11. tan(x) – returns the tangent of x

random Module
random module in python helps to generate random numbers. This module contains functions
that are used for generating random numbers.
Commonly used functions in random module.
1. randrange( ) - it generates an integer between its lower and upper argument. By
default, the lower argument is 0.
Example:
>>> random.randrange(30) # it will generate random number between 0 to 29
2. random( ) – it generates a random number from 0 to 1 such as 0.56. It can be used
to generate floating point values.
3. randint( ) – This function generates a random integer number between two given
numbers.
>>> random.randint(5, 30) # it will generate random number between 5 to 30

statistics Module
It helps to implement many common statistical formulas for efficient calculations.
Commonly used functions in statistics module.
1. mean( ) – calculates the arithmetic mean of the numbers in a list (average).
Example:
>>> statistics.mean([4,6,7,8,10,45])
Output 13.3333
2. median( ) – returns the middle value of numeric data in a list. This function find
the centre value, and if the data set has an even number of values, it averages the
two middle items.
Example:
>>> statistics.median( [1,2,3,8,9])
Output 3
>>> statistics.median([1,2,3,7,8,9])
Output 5.0

3. mode( ) – returns the most repeated value of the list/sequence.


Example:
>>> statistics.mode([21,24,21,45,56,67,21])
Output 21
>>> statistics.mode((“Red”, “Blue”, “Red”))
Output “Red”

You might also like