Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Modules in Python
  5. Importing modules in python

Importing modules in python

Modules refer to a file containing Python statements and definitions.

If you want to save a code as module save it using .py extension. The Module consists of functions, classes, variables, constants, etc.

We can use the module by importing it in our program. Grouping related code into a module makes the code easier to understand and use.

The Module name is the same as the file name we have saved our code.

If we want to use a particular module we just have to write import with the module name or file name. After importing module we can use all the functions, classes, variable, etc. present in the module.

syntax:-

import module_name

e.g., We will make a demo.py module.

def addition(a, b):
    return (a+b)
def subtraction(a, b):
    return (a-b)

Now import the module demo.py in a program and use its function.

import demo

print(addition(12, 4))
Output- 16

This is all about importing module in python.

Was this article helpful to you? Yes No

How can we help?