Intro to Python for Data Science Learning 5 - Packages
Packages
-
Import package
As a data scientist, some notions of geometry never hurt. Let's refresh some of the basics.We can do this by importing package.
import math
# Definition of radius
r = 0.43
# Import the math package
import math
# Calculate C
C = 2 * math.pi * r
# Calculate A
A = math.pi * r ** 2
# Build printout
print("Circumference: " + str(C))
print("Area: " + str(A))
-
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
# Definition of radius
r = 192500
# Import radians function of math package
from math import radians #not radians()
# Travel distance of Moon over 12 degrees. Store in dist.
phi = radians(12)
dist = r * phi
# Print out dist
print(dist)
-
Different ways of importing
There are several ways to import packages and modules into Python. Depending on the import call, you'll have to use different Python code.
Suppose you want to use the function inv()
, which is in the linalg
subpackage of thescipy
package. You want to be able to use this function as follows:
my_inv([[1,2], [3,4]])
could import as:
from scipy.linalg import inv as my_inv