Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

import
python

What is the import statement in Python?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The import statement is used to import external modules in Python. These modules add to the language’s functionality in some specified area. A few keywords are involved in the import statement:

Keyword 1: import

Primarily, an import statement comprises of the import keyword,​ followed by the name of the module to be imported.

import math
square_root = math.sqrt(9)
print(square_root)
In the code above, importing the math module gives us access to the sqrt function built into this module.

Keyword 2: from

The from keyword is used to import only a very specific part of a module.

from math import sqrt
square_root = sqrt(9)
print(square_root)
Note that we don't need to write math.sqrt( ) anymore.

To import all of the ​functions from the module, the * character can also be used.

from math import *
square_root = sqrt(9)
print(square_root)
Note that we don't need to write math.sqrt( ) anymore.

Keyword 3: as

The as keyword is used to give a custom name to the module for usage within the program.

import math as M
square_root = M.sqrt(9)
print(square_root)

RELATED TAGS

import
python
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring