XI_built-in modules.ppt features of module , types of module, functions of maths module, random module
kumarchandrawanshic
31 views
14 slides
Oct 03, 2024
Slide 1 of 14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
About This Presentation
Class 11th python built in modules ppt.
Size: 340.83 KB
Language: en
Added: Oct 03, 2024
Slides: 14 pages
Slide Content
Presented By –
Sanjay Kr. Upadhyay
MODULE
It is a “.py” file containing codes that can define
function, classes and variables related to particular
task.
Features of Module
Provides facility of code reusability.
All the module names are in lowercase
A module is written once and used/imported as
many times as required.
Allows us to logically organise the python code.
Grouping of related code in a module, make the
code easier to understand.
Module
Built-in
Module
User
Defined
Module
Predefined modules
available with an
interpreter
Module defined by
programmer to
perform specific
task
Contains functions to
handle extraction and
formatting of date
and time. Ex. -
today(),date(),now()
Contains functions used
for mathematical
calculations and most
of the function returns a
float value.
Ex- fabs(), factorial(),
sqrt(), sin(), etc.
BUILT-IN MODULES IN PYTHON
math
module
Contains function used for
generating random
numbers. Ex.– random()
randint()
randrange()
Contains functions
used for calculating
statistics of numeric
(real-valued) data.
Ex- mean, median,
mode
BUILT-IN
MODULES
IN
PYTHON
random
module
Statistics
module
datetime
module
It contains different types of mathematical
functions. Most of the functions in this module
return a float value.
e.g.
To use sqrt () function we have to write statements
like given below.
>>> import math
>>> r = math.sqrt(4)
>>> print(r)
OUTPUT : 2.0
Math Module
Functions in Math Module
This module contains functions that are used for
generating random numbers.
To use these functions, we have to import random
module by using the statement :
>>> import random
After that, call the function as given below:
>>> random.random()
Commonly used random module functions are :-
1) random ()
2) randint ()
3) randrange()
Random Module
Functions of Random Module
Function
Syntax
Uses Example
random ( ) Generates random number
from 0 to 1 (excluding 1). It
takes no argument and returns
float value.
x=random.random()
print(x)
0.29949494
randint(x,y)Generates random number
between x and y (including
both x and y).
x,y are integer such that x<=y.
x=random.randint(3,7)
print(x)
4
randrange(y)Generates random number
between 0 and y (excluding y).
x=random.randrange(5)
print(x)
4
randrange(x,y)Generates random number
between x and y (excluding y)
x=random.randrange(2,7)
print(x)
2
Functions of Random Module
Function
Syntax
Uses Example
uniform (x,y)Generates float
random number
from x to y
(excluding y).
x=random.uniform(1,100)
print(x)
85.476788
choice() Used for making a
random selection
from a sequences like
list, tuple or string.
x=random.choice([‘1.East’,
’2.West’,’3.North’,4. South’])
print(x)
2.West
shuffle() Used to shuffle or swap
the contents of a list.
gm=[‘lodo’,’chess’,’cricket’,
‘hockey’,’football’]
random.shuffle(gm)
print(gm)
cricket
This module provides functions for calculating
statistics of numeric (Real-valued) data.
Statistics module can be included in the program
by using the statement :
>>> import statistics
After that, call the function as given below:
>>> statistics.mean(x)
where, x is a numeric sequence.
Commonly used statistics module functions are :-
1) mean ()
2) median ()
3) mode()
Statistics Module
Functions of Statistics Module
Function
Syntax
Uses Example
mean(x)Find the arithmetic
mean of a given
numeric sequence i.e.
x.
statistics.mean ([11,24,32,45,51])
OUTPUT : 32.6
median(x)Find the median
(middle value) of x.
statistics.median([11,24,32,45,51])
OUTPUT : 32
mode(x)Find the mode
(the most repeated
value) in x.
statistics.mode ([11,24,11,45,11])
OUTPUT : 11
statistics.mode ( (“red”,”blue”,
”red”, ”green”))
OUTPUT : red
This module handles the extraction and formatting
of date and time variable.
datetime module can be included in the program by
using the statement :
>>> import datetime
The datetime module has two classes date and time.
To call the function of date class, use:
>>> datetime.date.today() #displays current system
date
To call the function of time class, use :
>>> datetime.time.now() #displays current system date
DateTime Module
Functions of datetime.date class
Function Returns Example
today()Current system
date
dt=datetime.date.today()
print(dt) #display current date
year() Year from the
datetime object.
dt=datetime.date.today()
print(dt.year) #display year
print(dt.month) #display month
print(dt.day) #display day
Functions of datetime.time class
now() Current System date
and time
dt_tm=datetime.time.now()
print(dt_tm)
hour() Hours from datetime
object.
tm=datetime.time.now()
print(tm.hour) #display hour
print(tm.minute) #display minute
print(tm.second) # display second