Importing Modules in Python

05 min read

Python is an object-oriented programming language. This means that the construct "class" in Python lets you structure the software in a particular way. Using classes in Python you can add consistency to your programs so that they can be used in a cleaner way.

Say, there is a lengthy program that needs to be structured neatly involving several functions that are called multiple times in the program. Here, we can create modules, i.e create different programs with certain functions and call that file ( .py) in another program whenever needed.

For example, consider 2 programs in 2 disparate files:

Program 1: 

FACE.py                                                                                                               # program file name is FACE.py
def placements(): # a function is declared.
        print( ' Full name = Focus Academy of Career Education") # command inside function

 

Now create another program 2:

main.py              # A completely new program file, named main.py
import FACE #Import the previously defined module by using import command.
print('what is the full form of FACE?')
FACE.placements() # run placements function in the imported module FACE.

Output when program 2 alone is executed:

what is the full form of FACE
Full name = Focus Academy of Career Education


In this case, we have used 2 programs in which both were developed manually. But, in Python, there are several inbuilt modules added in its library for easier use to build complex programs. Some of the most common inbuilt libraries are sys, beautifulsoup - web crawling, pillow for image processing, matplotlib for data visualization purposes, permutations and combinations to find all possible permutations for a given list of items, math to calculate basic math functions like pow(x, y) etc and many more modules which are discussed in detail in further articles. 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error