[Go to site: main page, start]

0% found this document useful (0 votes)
5 views3 pages

Module (Python)

A Python module is a .py file containing reusable code such as functions, classes, and variables. Modules can be imported using 'import' or 'from-import' statements, allowing for organization and reuse of code across multiple programs. Additionally, modules can be renamed during import for convenience.

Uploaded by

tejas1mohan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Module (Python)

A Python module is a .py file containing reusable code such as functions, classes, and variables. Modules can be imported using 'import' or 'from-import' statements, allowing for organization and reuse of code across multiple programs. Additionally, modules can be renamed during import for convenience.

Uploaded by

tejas1mohan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Modules in Python

A module is a single Python file (.py) containing Python code. It can include func ons, classes,
and variables that you can reuse in other programs.

Why use modules?

• To organize code into smaller, manageable chunks.

• To reuse code across mul ple programs.

# Create a module:

[Link]

# Python Module example


def sum(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return a/b

Loading the module in our python code


We need to load the module in our python code to use its functionality. Python provides
two types of statements as defined below.

1. import statement

2. from-import statement

1. import statement
The import statement is used to import all the functionality of one module into another.
Here, we must notice that we can use the functionality of any python source file by
importing that file as the module into another python source file.
We can import multiple modules with a single import statement.
Syntax:

import module1,module2..

Example:

import demo #importing entire Module


a=int(input("Enter a :"))
b=int(input("Enter b :"))
print("Sum is :",[Link](a,b))
print("Sub is :",[Link](a,b))
print("Mul is :",[Link](a,b))
print("Div is :",[Link](a,b))

2. from-import statement
Instead of importing the whole module into the namespace, python provides the flexibility
to import only the specific attributes of a module. This can be done by using from - import
statement. In such case we don't use the dot operator.
Syntax:

from module-name import *

Example:

from demo import *


a=int(input("Enter a :"))
b=int(input("Enter b :"))
print("Sum is :",sum(a,b))
print("Sub is :",sub(a,b))
print("Mul is :",mul(a,b))
print("Div is :",div(a,b))
We can import specific function from a module without importing the module as a whole.
Here is an example.
Syntax:

from module-name import function1,function2…

Example:

from demo import sub,mul


#importing specific functionality from Module
a=int(input("Enter a :"))
b=int(input("Enter b :"))
print("Sub is :",sub(a,b))
print("Mul is :",mul(a,b))

Renaming a module
Python provides us the flexibility to import some module with a specific name so that we
can use this name to use that module in our python source file.
Syntax:

import module-name as specific-name

Example:

import demo as c
a=int(input("Enter a :"))
b=int(input("Enter b :"))
print("Sum is :",[Link](a,b))
print("Sub is :",[Link](a,b))

You might also like