Introduction to Programming Using Python — Question 32
You are writing an application that uses the sqrt function. The program must reference the function using the name squareRoot.
You need to import the function.
Which code segment should you use?
Answer options
- A. import math.sqrt as squareRoot
- B. import sqrt from math as squareRoot
- C. from math import sqrt as squareRoot
- D. from math.sqrt as squareRoot
Correct answer: C
Explanation
The correct option, C, correctly imports the sqrt function from the math module and assigns it the alias squareRoot. Option A attempts to import sqrt but does not allow for aliasing to squareRoot correctly, while option B uses incorrect syntax for importing. Option D tries to import from the sub-module directly, which is not how the function is structured in the math module.