0% found this document useful (0 votes)
22 views

UNIT-II Modules and Packages

Uploaded by

Venkata satish
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)
22 views

UNIT-II Modules and Packages

Uploaded by

Venkata satish
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/ 16

UNIT-II

PYTHON MODULES & PACKAGES


INTRODUCTION to MODULES
MODULE DEFINITION
In Python, Modules are simply files with the “.py” extension containing Python code that can be
imported inside another Python Program.

 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.

The module contains the following components:


 Definitions and implementation of classes,
 Variables, and
 Functions that can be used inside another program.
CREATING & IMPORTING MODULE

 Let us create a module. Type the following and save it as arth.py.

mod_use.py
Python from...import statement
 We can import specific names from a module without importing the module as a whole. For example,

# import only pi from math module


from math import pi

print(pi)

# Output: 3.141592653589793

 To import more than one item from a module, use a comma list. Example

from math import pi, sqrt

 You can aslo import a module with a different name using “as” keyword

from math import sqrt as square_root


print(square_root(81))

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__”

print(“Name of the module is :”,__name__)

 To get list of available built-in modules, we can use help


The dir() Function

 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

2. Create an College package with two subpackages Employee and Student


 The Employee subpackage should contain the modules details(to print employee details),salary(To calculate salary)
 The Student subpackage should contain the modules marks(To print total marks),details(to print student details)

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

NOTE: match() function only


matches the 1st word in the
given string
 The search() function :This function tries to match the given pattern anywhere in the input string

 The sub() function :This function tries to match the given pattern and replace it with another pattern

NOTE: Here argument four represents


maximum replacements we can make
 Metacharacters of python regular expression

[] - A set of characters e.g. "[a-m]"


. - Any single character e.g. "i."
^ - Starts with e.g. "^hello"
$ - Ends with e.g. "planet$"
* - Zero or more occurrences e.g. "he.*o"
+ - One or more occurrences e.g. "he.+o"
? - Zero or one occurrences e.g. "he.?o”
\d – represents digits e.g. “ABC\d+”
{N} - No of occurences e.g. “[7-9]{1}[0-9]{9}”
 The findll() function :This function tries to match the given pattern and returns list all the matches
LAB 7: EXERCISES
1. Write the program to print the following 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

4.Write a program to generate the following pattern

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

You might also like