0% found this document useful (0 votes)
6 views30 pages

Modules Python 2

This document provides an overview of Python modules, explaining their structure, types, and how to import and use them. It details various built-in modules such as the Math and Random modules, including their functions and examples of usage. Additionally, it covers statistical functions like mean, median, and mode, illustrating their applications in data analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views30 pages

Modules Python 2

This document provides an overview of Python modules, explaining their structure, types, and how to import and use them. It details various built-in modules such as the Math and Random modules, including their functions and examples of usage. Additionally, it covers statistical functions like mean, median, and mode, illustrating their applications in data analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

PYTHON

MODULE
Group of functions, classes,
What is Python
Module
A is a file containing
 Python
Module definitions
(docstring , functions, variables,
s)
 Act of partitioning classes
a program into and
individual
statement
components(modules) is called modularity.
s.
A module is a separate unit in itself.
 It reduces its complexity to some degree
 It creates numbers of well-defined,
documented boundaries within program.
 Its contents can be reused in other program,
without having
to rewrite or recreate them.
Structure of Python
module
 A python module is simply a normal
python file(.py) and contains functions,
constants and other elements.
 Python
docstringmodule maycomments.
Triple quoted contains following
Useful for
objects: documentation
purpose
Variables For storing values
and
constants
Classes To create blueprint of any object
Objects Object is an instance of class. It represent class
in real world
Statements Instruction
Functions Group of statements
Composition/Structure of
python module
MODUL
ES
VARIABL OTHER
ES PYTHON

FUNCTION MODUL
S ES

VARIABL IMPOR
ES T
CLASSE
S
OTHER
MEMBE
METHODS PYTHON
RS
MODUL
ES
Importing Python
modules
 To import entire module
 import<module name>
 Example: import math

 To import specific function/object


from module:
 from <module_name> import
<function_name>
 Example: from math import sqrt

 import * : can be used to import all


names from module into current
calling module
Accessing function/constant of
imported module
 To use function/constant/variable of
imported module we have to specify
module name and function name
separated by dot(.). This format is known
as dot notation.
 <module_name>.<function_name>
 Example: print(math.sqrt(25))
 How ever if only particular function is
imported using from then module name
before function name is not required. We
will se examples with next slides.
Types of
Modules
 There are various in-built module in
python, we will discuss few of them
 Math module
 Random module
 Statistical module
Math
module
 This module provides various function
to perform arithmetic operations.
 Example of functions in math
modulessqrt
are: ceil floor pow
fabs sin cos tan
 Example of variables in math
modules are:
 Pi
 e
Math module
functions
 sqrt(x) : this function returns
the
number square root of module
name is
(x). required
before
 pow(x,y) : this function function name
here
returns the (x)y module name
is not required
before
function name
ceil : this function return the x
 here

(x) rounded to next


integer
.
Math module
functions
 floor(x) : this function returns thex
rounded to previous integer.
 fabs(x) : thisfunction returns absolute
value offloat x. absolute value means
number without any sign

 sin (x) : it return sine of x


(measured in radian)
Math module
functions
 cos(x) : it return cosine of x
(measured in radian)

 tan(x) : it return tangent of x


(measured in radian)

 pi: return the constant value of


pi (22/7)

 e : return the constant value of


constant e
Using Random
Module
 Python has a module namely random
that provides random – number
generators. Random number means any
number generated within the given
range.
 To generate random number in Python
we have to import random module
 2 most common method to generate
random number in python are :
 random() function
 randint(a,b) function
random()
function
 It is floating point random number
generator between 0.0 to 1.0. here
lower limit is inclusive where as
upper limit is less than 1.0.
 0<=N<1
 Examples:

Output is less
than 1
random()
function
 To generate random number between
given range of values using random(),
the following format should be used:
 Lower_range + random() * (upper_range-
lower_range)
 For example to generate number between
10 to 50:
 10 + random() * (40)
randint()
function
 Another way to generate
random number is randint() function,
but it generate integer numbers.
 Both the given range values are inclusive
i.e. if we generate random number as :
 randint(20,70)
 Inabove example random number between 20
to 70 will be taken. (including 20 and 70 also)
E

L
O

T
Just a
Minute…
 Give the following python code, which is
repeated four times. What could be the
possible set of output(s) out of four sets
(ddd is any combination of digits)
import random
print(15 + random.random()*5)
a) b) c) d)
17.ddd 15.ddd 14.ddd 15.ddd
19.ddd 17.ddd 16.ddd 15.ddd
20.ddd 19.ddd 18.ddd 15.ddd
15.ddd 18.ddd 20.ddd 15.ddd
Just a
Minute…
 What could be the minimum possible
and maximum possible numbers by
following code
import random
print(random.randint(3
,10)-3)
 In a school fest, three randomly chosen
students out of 100 students (having roll
number 1 -100) have to present the
bouquet to the guests. Help the school
authorities choose three students
Just a
Minute…
Just a
Minute…
Look at the following Python code and find the possible
output(s) from the options (i) to (iv) following it. Also, write
the maximum and the minimum values that can be assigned
to the variable PICKER.

‐ Assume all the required header files are already being


Note:

‐The function randint() generates an integer


included in the code.

between 1 to n import random


PICKER=1+random.randint(0,2)
COLOR=[”BLUE”,”PINK”,”GREEN”,”RED”]
for I in range(1,PICKER+1):
for j in range(I+1):
print(COLOR[j],end=‘’)
print()
What are the possible outcome(s) executed
from the following code? Also specify the
maximum and minimum values that can be
assigned to variable PICK

1) 2)
DELHIDELHI DELHI
MUMBAIMUMB DELHIMUMBAI
AI DELHIMUMBAICH
CHENNAICHEN ENNAI
NAI
KOLKATAKOL
KATA
3) 4)
DELHI DELHI
MUMBAI DELHIMUMBAI
CHENNAI KOLKATAKOLKATAKOLKAT
A
randrange()
function
 This function is also used to
generate random number within
given range.
 Syntax
 randrange(start,stop,step)
It will generate
random number
between 5 to 14

random output between 5 to 14,


may vary
randrange()
function
It will generate
random number
between 1 to 29
with stepping of
2 i.e. it will
generate
number with
gap of 2 i.e.
1,3,5,7 and so
on
Mathematics Game
for Kids
Mathematics Game
for Kids
Statistical
Module
 This provid functions for
module es calculating
mathemati statisti of numeric (Real-
cal cs valued)
 We will deal with 3 basic function under
data.
this module
 Mean
 Median
 mode
Mea
n
 The mean is the average of all
numbers and is sometimes called
the arithmetic mean.

55, is the average of all numbers


in the list
Media
n
 The median is the middle number in
a group of numbers.
With odd
number of
elements it will
simply return
the middle
position value

With even
number of
elements, it will
return the
average of value
at mid + mid-1
i.e. (50+60)/2 =
55.0
Mod
e
 The mode is the number that occurs
most often within a set of numbers i.e.
most common data in list.

Here, 10
occurs
most in the
list.

You might also like