Paket

Introduktion till Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Motivation

  • Funktioner och metoder är kraftfulla

  • All kod i Pythons distribution?

    • Enorm kodbas: rörigt

    • Mycket kod du aldrig använder

    • Underhållsproblem

Introduktion till Python

Paket

  • Katalog med Python-skript

  • Varje skript = modul

  • Definierar funktioner, metoder, typer

  • Tusentals paket tillgängliga

    • NumPy

    • Matplotlib

    • scikit-learn

Skärmbild 2019-09-08 kl. 9.18.56.png

Introduktion till Python

Installera paket

Introduktion till Python

Importera paket

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])
Introduktion till 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)
  • Använder NumPy, men inte tydligt
Introduktion till 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
Introduktion till Python

Nu kör vi en övning!

Introduktion till Python

Preparing Video For Download...