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

Unit 3 (Second) - 1

The document provides an overview of Python modules and libraries, explaining their structure, types, and common examples such as the Python Standard Library, Numpy, and Matplotlib. It details how to create and use modules, including predefined and user-defined modules, as well as various functions for mathematical operations and random number generation. Additionally, it discusses packages, their structure, and the importance of the __init__.py file.
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 views17 pages

Unit 3 (Second) - 1

The document provides an overview of Python modules and libraries, explaining their structure, types, and common examples such as the Python Standard Library, Numpy, and Matplotlib. It details how to create and use modules, including predefined and user-defined modules, as well as various functions for mathematical operations and random number generation. Additionally, it discusses packages, their structure, and the importance of the __init__.py file.
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/ 17

Modules

• Library : a library is a collection of modules that


is used to cater specific type of need or
application.
• Some common libraries are :
– Python standard Library : math module, cmath
module, random module, stat module, URLlib
module.
– Numpy library: advance math function along with
tools to create and manipulate arrays.
– Scipy library: used for scientific calculations.
– Tkinter library: used for GUI interface.
– Matplotlib library : used for producing quality o/p in
variety of formats such as plots, charts, graphs etc.
Modules
• Modules are nothing but group of functions, variables and classes that are saved to a file.
• Module : python module is .py file, contaning variables, class definition, statements and
functions related to a particular task.
• The main features of module is that its content can be reused in other program without
having to rewrite or recreate them.
• Structure of python Module :
– Docstring,: triple quoted string for documentation purpose.
– Variables and constant
– Classes, template to create object
– Statements, instructions
– Function to perform task
• Types :
– Predefined : random, calender, keyword(69 predefined functions)
– Userdefined : first, second
– Module level
• Example:
– First.py
• Def show():
• Print (‘hello’)
– Second.py
• Import first
• First.show()
• ## save sum.py
• def sum_ser(N):
• ''' this function is used to return the sum of first N natural numbers...'''
• sum=0
• for i in range(1,N+1):
• sum=sum+i
• return sum

• def sum_odd(N):
• ''' this function is used to return the sum of first N odd numbers...'''
• sum=0
• for i in range(1,N+1,2):
• sum=sum+i
• return sum
• print(sum_odd(7))

• def sum_even(N):
• ''' this function is used to return the sum of first N even numbers...'''
• sum=0
• for i in range(2,N+1,2):
• sum=sum+i
• return sum
• print(sum_even(7))
• ## open new file in IDLE
• Write :
– import sum
– print(sum.sum_even(10)) # call first function
– # to know the help of any file write
• help(sum)
– # to know about specific function write:
• Help(sum.funname)
• Help(sum.sum_even)

• help(sum.sum_odd)
• Help on function sum_odd in module sum:

• sum_odd(N)
• this function is used to return the sum of first N odd
numbers...
ways to call the module
• 1. from <modulename> import <funname>
• #print(<funname()>
• 2. from <modulename> import * # all function called
• 3. import <modulename>
• #call function
• <modulename>.<funname>
• # using Alias name
• Import <modulename> as <aliasname>
• Like : import m1 as m
• Print(M.<funname>)
Predefined

• import calendar
• print(calendar.month(2023,11))

• import keyword
• print(keyword.kwlist)

• max(20,50,30,5)
• 50
• min(50,6,45,0,-1,20)
Random Modules

• When we require such numbers which are not


known earlier like captcha or any type of serial
numbers, then in this situation we need random
numbers.
• To use random function we have to import
random module.
• Random() : this generates the floating values b/w
0 and 1(including 0 but excluding 1). It does not
required any argument.
• Eg :
– random.random()
– 0.6888655671643036
• Randrange() : this method always return an
integer b/w given lower and upper limit to this
method(excluding upper limit).
• Syntax : random.randrange(a,b,stepvalue)
• Eg :
– random.randrange(2,10,2)
– random.randrange(2)
– random.randrange(2,7)
– random.randrange(7,3,-1)
• randint() : this method takes 2 parameters a,b
in which first one is lower and second is upper
limit. This function returns any integer number
b/w the two numbers, including both limits.
• Eg :
– random.randint(2,6)
• uniform() : this method returns any floating
point number b/w two given numbers.
• Eg :
– random.uniform(2,4)
– random.uniform(2,2)
– random.uniform(20,2)
• Choice() : this method is used is used for
random selection from list, tuple, string or any
other sequence.
• It returns a random element from the given
sequence.
• Eg :
– l=[11,22,33,44,55,66]
– random.choice(l)
– random.choice(['11','33','55'])
– random.choice('2345556')
• shuffle(): this method can shuffle the items of a
given list.
• Shuffle list x in place and return NONE.
• Eg :
– =[11,22,33,44,55,66]
– id(l)
– 903878088128
– random.shuffle(l)
– l
– [44, 33, 11, 66, 22, 55]
– id(l)
– 903878088128
Math Module
• It contains different types of mathematical functions.
Most of the functions in this module returns a float
value.
• Sqrt: returns the square root of x. It return the result
in floating point number.
• Eg :
– math.sqrt(5)
• Ceil(x) : it return the smallest integer not less than x.
• Eg : math.ceil(2.6)
• Floor() : it return the largest integer not greater than x.
• Eg :
– Math.floor(4.5)
• Pow(x,y) : it returns the value of x^y.
• Fabs() : it returns the absolute value of float x.
• Eg : abs(-98)
• math.fabs(96)
• Degree(x): it converts angle x from radians to degree.
• Radians(x): it converts angle x from degrees to
radians.
• Eg :
– Math.radians(6)
– math.degrees(90)
– 5156.620156177409
– math.radians(5156.620156177409)
– 90.0
Packages
• A package is a directory that contains subpackages
and modules in it along with some special files such
as __init__ py.
• A package can contain one or more relevant
modules.
• The package folder contains a special file
called __init__.py, which stores the package's
content. The __init__.py file is normally empty.
• Without __init__.py a folder is not considered
a python package.
• Suppose there is a package called pkg
containing a module called mod.py. If the
module contains functions f1( ) and f2() then
the directory structure would be as follows:
• Directory - pkg
• Contents of pkgdirectory - mod.py and
__init__.py
• Contents of mod.py -f1() and f2( )
• Create package:
– Import os
– Os.mkdir(“geometry) # geometry folder created
• Help() : it is used to display docstrings as
description of module along with following details.
• Module name, filename and its location, function
name along with its description and constants.
• Import random
• Help(random)

You might also like