0% found this document useful (0 votes)
1 views15 pages

Introduction to Python Modules

A library in programming is a collection of modules that serve specific needs, enhancing modularity by reducing complexity and defining boundaries. Python modules, which are .py files, can be imported using various commands to utilize functions and constants like math.sqrt() and random.randint(). The document also covers the functionality of the math and random modules, as well as the statistics module for calculating mean, median, and mode.

Uploaded by

ATL
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)
1 views15 pages

Introduction to Python Modules

A library in programming is a collection of modules that serve specific needs, enhancing modularity by reducing complexity and defining boundaries. Python modules, which are .py files, can be imported using various commands to utilize functions and constants like math.sqrt() and random.randint(). The document also covers the functionality of the math and random modules, as well as the statistics module for calculating mean, median, and mode.

Uploaded by

ATL
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/ 15

LIBRARY

• A library refers to a collection of modules that together cater to


specific type of needs or applications.
• Modules?
• The act of partitioning a program into individual components known
as modules is called modularity
• Advantage:
• It reduces its complexity to some degree and
• It creates a number of well defined , documented boundaries within
the program.
• Example:

• Import math
• Math.sqrt()

from math import sqrt,pow


Ceil()-returns the smallest integer
• A python module is a file(.py file) containing variables, class
definitions, statements and functions related to a particular task.
• Importing Modules in a python program:
• Python provides the import statement to import modules in a program
• The import statement can be used in two ways:
• import <module> command
• Import <module> command as nick name
• from <module> import <object>command
• From <module> import <object>*
• Import module1,module2---- importing entire module
• module name.function name
Sqrt()-returns the square root of
number
Exp()-returns the natural logarithm
e raised to the power
• Explanation:math.exp(2.0) and
math.exp(2) return the same value
because 2.0 and 2 represent the same
numeric value,
• one as a float and one as an

𝑒−2≈0.1353e −2 ≈0.1353math.exp(-2.1)
integer.math.exp(-2) computes

computes 𝑒−2.1≈0.1225e −2.1 ≈0.1225


• The math.exp() function works for any
real number, positive or negative.
fabs()-returns the absolute value
floor()-returns the largest integer
not greater than number.
Pow(base,exp)
• A domain error occurs
if if base=0 and exp<=0;
also if base <0 and exp
is not integer
Math.pi and math.e
• math.pi gives the
mathematical
constant pi=3.14
• math.e gives the
mathematical
constant e=2.71
Working with Random Module
• Python has a module namely random that provides random-number
generators. –a number generated by chance,randomly
• The most common random number generator function are]
• random(): 0.0<=N<1.0
• randint(a,b): a<=N<=b
• randrange(start,stop,step): start<=N< stop
random-range(0.0,1.0)
To generate random floating point
number(upper-lower) and add it to lower limit
Eg: between 15 to 35
random.randint(a,b): a is included,b
is also included

Random.randint(
randrange(start,stop,step)
From 11 to 45
Possible random numbers are
11,15,19,23,27,31,35,39,43
Statistics Module
• Mean()-returns the average value of the data
• Median()-returns the middle value before that arrange it in ascending
order, if the length is in even add the two middle numbers and then
divide by 2
• So the for the above function if the length is even the output will be in
float.
• Mode()-returns the most often repeated value .

You might also like