Lecture24 25 Unit5 1 Modules and Libraries
Lecture24 25 Unit5 1 Modules and Libraries
1
Lecture Intended Learning Outcomes
2
Contents
3
Structure of Python program
• Python program consists of multiple text files containing Python statements
• Program is structured as main file (top-level file) along with zero or more
supplemental files known as modules in Python
4
Structure of Python program
• Any single Python file with the *.py extension can be called as module
• It contains a set of functions, classes and variables that can be used by other Python
programs
• Definitions from a module can be imported into other modules or into the main module
6
Modules in Python
Modules can be defined in Python in 3 ways:
7
Modules in Python
Defining/Creating a module in Python:
# module definition
>>>def square(number):
result=number**2
return result
• Module is created
8
Modules in Python
Importing a module in Python: Functions from one module can be imported to
another program/module using the keyword import
# importing the function square to another file or program
>>>import example_module # module is imported
# to call the function square() within the module
>>>x=example_module.square(6) # use a dot operator to call the respective
function
>>>print('The output of the square fn in module is ', x)
Output is : The output of the square fn in module is 36
9
Importing modules in Python program
• file b.py defines a function spam for external use
>>>def spam(text):
print(text)
>>>import b # This statement gives the file a.py access to the file b.py or it means
load b.py and give access to all its attributes
>>>help('matplotlib’)
Out: 3.141592653589793
11
Standard Modules in Python
>>>import math
>>>dir(math)
• Built-in function dir() is used to find out which names a module defines
>>>import math,cmath
>>>print(math.__name__)
math
13
Standard Modules in Python
Import statement can be used in two ways to import modules in a program:
>>>import math
The way of referring to an object in a module is called dot notation eg: math.sqrt()
14
from...import Statement in Python
To import selected objects from <module name> import <object>
If this command is used for importing, do not use module name with imported object as
imported object is a part of the program
>>>from math import * # All variables can be used without prefixing module name
15
dir() Built-in Function in Python
dir(): To identify names declared within a module
>>>print(dir(math), end=‘ ‘)
• When two variables with the same name are local and global, the local variable
takes the role of the global variable
>>>randint(1,6)
Out: 5
22
Libraries in Python
• A library refers to a collection of modules that together cater to a specific type of
applications
• NumPy library
• SciPy library
• tkinter library
• Matplotlib library
23
Libraries in Python
• Python standard library: Available in default, no need to import it separately
• NumPy library
• SciPy library
• tkinter library
• Matplotlib library
24
Libraries in Python
Python standard library includes:
urllib module: URL handling functions to access websites within the program
eg: urllib.request.urlopen
25
Other Libraries in Python
NumPy library : Provides advance math functionalities and tools to create numeric
arrays
SciPy library: Offers algorithmic and mathematical tools for scientific calculations
tkinter library: Python user interface toolkit and helps to create user-friendly GUI
interface for different types of applications
26
Summary
• A program can be divided into individual components/units known as modules
• A module is a file containing Python definitions and statements
• Any single Python file with the .py extension can be called as module
• Python provides import statement to import modules as a whole in a program
import math
• Import specific names from a module without importing the module as a whole
also is possible from math import sqrt
• A library refers to a collection of modules that together cater to a specific type of
applications
27
References
https://www.javatpoint.com/python-modules
28