UNIT-II Modules and Packages
UNIT-II Modules and Packages
If our program is getting bigger, Instead of putting everything in a single file, we can use modules to
separate codes in separate files as per their functionality. This makes our code organized and easier to
maintain.
mod_use.py
Python from...import statement
We can import specific names from a module without importing the module as a whole. For example,
print(pi)
# Output: 3.141592653589793
To import more than one item from a module, use a comma list. Example
You can aslo import a module with a different name using “as” keyword
Output: 9.0
Python allows us to supply command line arguments usins “sys” module
#main.py
import sys
print(sys.argv)
We can obtain the name of the module using the attribute “__name__”
The dir() built-in function lists the identifiers defined in a module.These identifiers may include class,
function, and variable name.
OUTPUT:
PACKAGES
A package is hierarchical file directory structure that has modules asn other packages in it
Every Package in python is a directory which must have a special file called “__init__.py”.
This file may not have single line if code,It us sinply added to indicate that this directory is not an ordinary directory
and cantains package
For Example: Create a package call MyPackage having modules and sub packages
<<MyPackage>>
<<Sum>>
Arth.py
Sum_eg.py
LAB 6: EXERCISES
1.Write a python program to create and use the following hierarchical file strucuture
3. Write a program to create a package “Example” that contains “Module1” and “Module2” with certain functions.Use
Module1 to accept input and use Module2 to print the input
STRING
string_manipulation.py
mypackage MATHOP
math_operations.py
CURRENCY
currency_converter.py
PYTHON STRINGS
COMPARING STRINGS
ITERATING STRINGS
String is a sequence of characters.You can iterate trough the string using for loop.Example
One convert a charater to its ASCII value using function “ord(string)” and vice versa is also possible using
chr(ASCII_Value)
REGULAR EXPRESSION ON STRINGS
Regular expression is a spacial sequnce of characters that helps to match ot find the given string in other strings
Python regular expression can be accessed using “re” module
The “re” module contains number of function to perform this pattern matching
The match() function :This function tries to match the given pattern against the beginning of the string
The sub() function :This function tries to match the given pattern and replace it with another pattern
2. Ask the user to enter his gmail as input and check wether it is valid or not(use regular expressions)
3.Write a program to parse and email id input from user and print it mail server name
5.Write a program to count number of small letters, capital letters and digits present in the given string
6. Write a program to validate each phone number taken from input list of phone numbers using regular expressions