Selective import
General imports, like import math
, make all functionality from the math
package available to you. However, if you decide to only use a specific part of a package, you can always make your import more selective:
from math import pi
Try the same thing again, but this time only use pi
.
This is a part of the course
“Introduction to Python”
Exercise instructions
- Perform a selective import from the
math
package where you only import thepi
function. - Use
math.pi
to calculate the circumference of the circle and store it inC
. - Use
math.pi
to calculate the area of the circle and store it inA
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import pi function of math package
from math import ____
# Calculate C
C = 2 * 0.43 * ____
# Calculate A
A = ____ * 0.43 ** 2
print("Circumference: " + str(C))
print("Area: " + str(A))