Gói (Package)

Nhập môn Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Động lực

  • Các hàm và phương thức rất mạnh

  • Toàn bộ code trong bản phân phối Python?

    • Cơ sở mã nguồn khổng lồ: lộn xộn

    • Nhiều đoạn code sẽ không được sử dụng

    • Vấn đề bảo trì

Nhập môn Python

Gói (Package)

  • Thư mục Python script

  • Mỗi script = mô-đun

  • Chỉ định các hàm, các phương thức, các kiểu

  • Hàng nghìn package có sẵn

    • NumPy

    • Matplotlib

    • scikit-learn

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

Nhập môn Python

Cài đặt package

Nhập môn 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])
Nhập môn Python

từ 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)
  • Sử dụng NumPy, nhưng chưa rõ lắm
Nhập môn 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
Nhập môn Python

Cùng thực hành nào!

Nhập môn Python

Preparing Video For Download...