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

1. Python Module (1)

A Python module is a program file that contains functions, classes, or variables, allowing for organized code. Modules can be used by importing them into other files, either fully or selectively, using import statements. Aliasing allows modules or specific entities to be referenced with different names to avoid conflicts.

Uploaded by

katelyncansino3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

1. Python Module (1)

A Python module is a program file that contains functions, classes, or variables, allowing for organized code. Modules can be used by importing them into other files, either fully or selectively, using import statements. Aliasing allows modules or specific entities to be referenced with different names to avoid conflicts.

Uploaded by

katelyncansino3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Python

Module
DCSN03C – Computer Programming 2
What is Module?
Python Module
● A python modules can be defined as a python program file.
● It contains -- functions, class or variables.
● Modules in Python provide us the flexibility to organize the code in a
logical way.
● Example:

def myModule(name):
print("Hi!", name)

● •Save the above code with <filename>.py


How to make use of a module?
The handling of modules consists of two different
issues
● Using a Module: As a module's user, you utilize
an existing module written by someone else or
created by yourself as part of a complex
project.
● Creating a Module: As a module's supplier, you
develop a new module either for personal use
or to assist other programmers.
Loading Module in Python Code
● Python provides 2 types of statement.

1. Import Statement
2. From-Import Statement
Import Statement
● Import statement is used to import all
the functionality of one module into
another. (think of it like of taking a book
off the shelf).
● We can import multiple modules with a
single import statement, but a module is
loaded once regardless of the number of
times.
Syntax:
import module1, module2, ..., module n
Namespace
Inside a certain namespace, each name must
remain unique. This may mean that some names
may disappear when any other entity of an
already known name enters the namespace.

If the module of a specified name exists and is


accessible (a module is in fact a Python source
file), Python imports its contents, i.e., all the
names defined in the module become known

ex: from math import pi


Import Statement Example
from math import sin, pi
print(sin(pi/2))

The instruction consists of the following elements:


•the from keyword;
•the name of the module to be (selectively) imported;
•the import keyword;
•the name or list of names of the entity/entities which are being
imported into the namespace.
Import Statement Example
#file.py
def displayMsg(name):
#Displays a personalized message.
print(f"Hello, {name}! Welcome to the program.")

#test.py
import file
name = input("Enter your name:")
file.displayMsg(name)
From-Import Statement
● •It provides the flexibility to import only the specific
attributes of a module.

Syntax:
from <module-name> import <name1>, <name2> ...
From-Import Statement Example
#Calculation.py #Myprogram.py
def sum(a,b): from Calculation import sum
return a + b a = int(input("Enter the first number"))
def mul(a,b): b = int(input("Enter the second
return a * b number"))
def div(a,b): print("Sum = ", sum(a,b))
return a/b
Importing a module: *
Syntax

from module import *

the name of an entity (or the list of entities' names) is replaced with a
single asterisk (*).
Such an instruction imports all entities from the indicated module.
The as keyword
import module variant and you don't like a particular module's name
(e.g., it's the same as one of your already defined entities, so
qualification becomes troublesome) you can give it any name you
like - this is called aliasing.

Aliasing causes the module to be identified under a different name than the
original. This may shorten the qualified names, too.

Syntax:
import module as alias
Aliasing Example
import math as m

print(m.sin(m.pi/2))
Note: after successful execution of an aliased import, the original module name
becomes inaccessible and must not be used.

In turn, when you use the from module import name variant and you need to change
the entity's name, you make an alias for the entity. This will cause the name to be
replaced by the alias you choose.

from module import name as alias


Aliasing Example
from math import pi as PI, sin as sine
print(sine(PI/2))

You might also like