Using Python Libraries
Wednesday, April 27, 2022 12:19 PM
A module is a python file that contains a collection of related functions.
Important relevant terms:
• Modularization: The approach of making a set of functions in a file (a module).
Makes program easier to understand, test and maintain.
• Libraries: The commonly-used modules in Python are known as libraries.
• Packages: Directory of Python module(s).
Advantages of Python Modules:
1. Importable
2. Reusable
3. Allow logical organisation
4. Creates well-defined, documented boundry numbers
5. Easy grouping
6. Can be used for storing similar attributes
7. Reduces program complexity
Importing Modules:
1. import <module_name>
2. import <module_name1>, <module_name2>, <module_name3>, . . .
3. from <module_name> import *
4. from <module_name> import <fun()_1>, <fun()_2>, <fun()_3>, . . .
Locating Modules:
• Current directory Each directory in Default path where
shell variable. Python is installed.
• PYTHONPATH is a variable to add additional directories where python will look for modules and
packages. It can be manually set.
NAMESPACES AND SCOPE
Namespaces are used to distinguish between different
sections of a program.
• They are essential for mapping of names to corresponding
objects.
• Maintain name-to-object mapping in form of dictionaries.
key value
• Python uses namespaces to keep track of variables.
• There are three types of namespaces:
i. GLOBAL Created when the module definition is read in Executed by the top-level invocation of the
and normally lasts until the interpreter quits. interpreter. Module has a global namespace.
ii. LOCAL Created when the function is called, and Each recursive invocation has its own local
deleted when the function returns or raises an namespace.
exception.
iii. BUILT-IN Created when the Python interpreter starts Encloses the local and global namesapces.
up, and is never deleted.
NameResolution is resolving of scope of name.
• The entire Python program revolves around the variable scope and their names.
• Python follows an LEGB rule. It ensures that for name resolution the order is followed.
• Checks for variables with same names
L. E. G. B.
LOCAL ENCLOSING GLOBAL BUILT-IN
Uses value if found
Uses value if found.
Uses value if found.
Uses value if found.
If not found moves to ii. If not found, moves to iii. If not found, moves to iv. If not found, generates error.
- Repeats step to higher end. - name <variable> is not defined
Note: Checks for variables with same names, if yes, takes it's value, if not, moves to next step.
ALIASING
Done using a keyword/alias.
1. Module
syntax: import <module_name> as <key_word>
2. Member
syntax: from <module_name> import <member_name> as <key_word>
PACKAGE/LIBRARY
A Python package is a collection/directory of related modules.
• They can be built-in imported and created.
• Point of differences:
i. A module is a file containing Python code. A package, however, is like a directory that
holds sub-packages and modules.
ii. A package is simply a collection of simlar modules, sub packages, etc. A package and
library are technically the same thing.
Advantages of Libraries/Packages:
1. Structure python modules by namespace using "dotted module names".
2. Dotted modules names avoid worrying about other's module names.
Creating and Importing a package:
1. Create the directory
2. Place different modules in package
3. Create __init__.py file
4. Import package and attributes
__init__.py
• It is used to consider directories on the disk as package of Python.
• It initializes the Python packages.
SOME LIBRARY/MODULES
I. Datetime
Python supports yyyy-mm-dd and format.
a. date class
today() year() month() day()
import datetime
import datetime
import datetime
import datetime
datetime.date.today() t=datetime.date.today()
t=datetime.date.today()
t=datetime.date.today()
t.year t.month t.day
b. time class
now() hour() minute() second()
import datetime
import datetime
import datetime
import datetime
datetime.date.now() t=datetime.date.now()
t=datetime.date.now()
t=datetime.date.now()
t.hour t.minute t.second
II. Math
ceil(x) floor(x) pow(x,y) fabs() sqrt(x)
import math
import math
import math
import math
import math
math.ceil(x) math.floor(x) math.pow(x,y) math.fabs(x) math.sqrt(x)
log10(x) cos(x) sin(x) tan(x) help()
import math
import math
import math
import math
import math
math.log10(x) math.cos(x) math.sin(x) math.tan(x) math.help()
pi Euler's number degrees(x) radians(x)
import math
import math
import math
import math
math.pi math.e math.degrees(x) math.radians(x)
III. String
randrange() random() randint() uniform() choice() shuffle()
random.randrange() random.random() random.randint random.uniform random.choice random.shuffle
random.randrange (a,b) (a,b) (seq) (list)
(strt,stop,step)