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))
This exercise is part of the course
Introduction to Python
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
You'll learn how to use functions, methods, and packages to efficiently leverage the code that brilliant Python developers have written. The goal is to reduce the amount of code you need to solve challenging problems!
Exercise 1: FunctionsExercise 2: Familiar functionsExercise 3: Help!Exercise 4: Multiple argumentsExercise 5: MethodsExercise 6: String MethodsExercise 7: List MethodsExercise 8: List Methods (2)Exercise 9: PackagesExercise 10: Import packageExercise 11: Selective importExercise 12: Different ways of importingWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.