Computer Science
What is the difference between import statement and from import statement?
Python Modules
2 Likes
Answer
| import statement | from 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
- How is math.ceil(89.7) different from math.floor(89.7)? 
- Out of random() and randint(), which function should we use to generate random numbers between 1 and 5. Justify. 
- Why is from import* statement not recommended for importing Python modules in an external program? 
- Write a function to calculate volume of a box with appropriate default values for its parameters. Your function should have the following input parameters : - (a) length of box ; - (b) width of box ; - (c) height of box. - Test it by writing complete program to invoke it.