What is the import statement good for in Python?

Experience Level: Junior
Tags: Python

Answer

The import statement imports a module to another module. When you import a module using import statement, the functions and classes are not imported directly. Only the module is imported and you can access the functions and classes that are inside the module using the dot (.) operator prefixed with the module name.

Example:

File main.py

# Another module main.py

import basic_math

print(basic_math.add(1,3))

File basic_math.py

# Module basic_math.py

def add(x, y):
  return x+y

def subtract(x, y):
  return x-y

Related Python job interview questions
Python for beginners
Python for beginners

Are you learning Python ? Try our test we designed to help you progress faster.

Test yourself