KnowledgeBoat Logo
|

Computer Science

What is the difference between import statement and from import statement?

Python Modules

2 Likes

Answer

import statementfrom import statement
It imports entire module.It imports single, multiple or all objects from a module.
To access one of the functions, we have to specify the name of the module and the name of the function, separated by a dot. This format is called dot notation. The syntax is : <module-name>.<function-name>().To access functions, there is no need to prefix module's name to imported item name. The syntax is : <function-name>.
Imports all its items in a new namespace with the same name as of the module.Imports specified items from the module into the current namespace.
This approach does not cause any problems.This approach can lead to namespace pollution and name clashes if multiple modules import items with the same name.
For example:import math print(math.pi) print(math.sqrt(25))For example: from math import pi, sqrt print(pi) print(sqrt(25))

Answered By

1 Like


Related Questions