How do you import a specific function from a module in Python?
a) Using the import function keyword
b) Using the import keyword
c) Using the from…import statement
d) Using the include keyword
Answer:
c) Using the from…import statement
Explanation:
To import a specific function from a module in Python, you use the from...import
statement. This allows you to import only the function or functions you need from the module, rather than importing the entire module.
from math import sqrt
result = sqrt(16) # Uses the sqrt function from the math module
In this example, the sqrt
function is imported from the math
module, allowing you to use it directly in your code without having to prefix it with the module name.
Using the from...import
statement helps to keep your code clean and focused by only importing the specific functions or classes you need.