in pyhton, why we have sometime only import and some other ime, from and import ?
- User
in pyhton, why we have sometime only import and some other ime, from and import ?
- Assistant
In Python, the keywords "import" and "from" are used to import modules or specific objects from modules.
The "import" statement is used to import an entire module into your Python script. It allows you to access all the functions, classes, and variables defined in that module. For example:
import math print(math.pi)Here, the "math" module is imported using the "import" keyword, and we can access its constant "pi" by prefixing it with the module name "math".
On the other hand, the "from" statement is used to import specific objects (functions, classes, or variables) from a module instead of importing the whole module. This allows you to access only the specific objects you need, without having to explicitly specify the module name each time. For example:
from math import pi print(pi)Here, we only import the "pi" constant from the "math" module using the "from" keyword. Now, we can directly access "pi" without referencing the "math" module.