Packages

Introduction to Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Motivation

  • Functions and methods are powerful

  • All code in Python distribution?

    • Huge code base: messy

    • Lots of code you won’t use

    • Maintenance problem

Introduction to Python

Packages

  • Directory of Python Scripts

  • Each script = module

  • Specify functions, methods, types

  • Thousands of packages available

    • NumPy

    • Matplotlib

    • scikit-learn

Screen Shot 2019-09-08 at 9.18.56 AM.png

Introduction to Python

Install package

Introduction to Python

Import package

import numpy

array([1, 2, 3])
NameError: name 'array' is not defined
numpy.array([1, 2, 3])
array([1, 2, 3])
import numpy as np

np.array([1, 2, 3])
array([1, 2, 3])
from numpy import array

array([1, 2, 3])
array([1, 2, 3])
Introduction to Python

from numpy import array

  • my_script.py
from numpy import array

fam = ["liz", 1.73, "emma", 1.68, "mom", 1.71, "dad", 1.89] ...
fam_ext = fam + ["me", 1.79] ...
print(str(len(fam_ext)) + " elements in fam_ext") ...
np_fam = array(fam_ext)
  • Using NumPy, but not very clear
Introduction to Python

import numpy

import numpy as np

fam = ["liz", 1.73, "emma", 1.68, 
    "mom", 1.71, "dad", 1.89]

...

fam_ext = fam + ["me", 1.79] ...
print(str(len(fam_ext)) + " elements in fam_ext") ...
np_fam = np.array(fam_ext) # Clearly using NumPy
Introduction to Python

Let's practice!

Introduction to Python

Preparing Video For Download...