Thư viện trong Python là gì

Thư viện là một tập hợp các mô-đun, nhưng các thuật ngữ này thường được sử dụng thay thế cho nhau, đặc biệt là vì nhiều thư viện chỉ bao gồm một mô-đun duy nhất, vì vậy đừng lo lắng nếu bạn trộn lẫn chúng

Một chương trình phải nhập mô-đun thư viện trước khi sử dụng nó

  • Sử dụng
    help(string)
    
    0 để tải mô-đun thư viện vào bộ nhớ của chương trình
  • Sau đó gọi những thứ từ mô-đun là
    help(string)
    
    1
    • Python sử dụng
      help(string)
      
      2 có nghĩa là “một phần của”
  • Sử dụng
    help(string)
    
    3, một trong các mô-đun trong thư viện tiêu chuẩn

import string

print('The lower ascii letters are', string.ascii_lowercase)
print(string.capwords('capitalise this sentence please.'))

The lower ascii letters are abcdefghijklmnopqrstuvwxyz
Capitalise This Sentence Please.

  • Bạn phải tham khảo từng mục với tên của mô-đun
    • help(string)
      
      4 sẽ không hoạt động. tham chiếu đến
      help(string)
      
      5 không bằng cách nào đó “kế thừa” tham chiếu của hàm tới
      help(string)
      
      3

Sử dụng help(string) 7 để tìm hiểu về nội dung của mô-đun thư viện

  • Hoạt động giống như trợ giúp cho một chức năng

help(string)

Help on module string:

NAME
    string - A collection of string constants.

MODULE REFERENCE
    https://docs.python.org/3.6/library/string
    
    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    Public module variables:
    
    whitespace -- a string containing all ASCII whitespace
    ascii_lowercase -- a string containing all ASCII lowercase letters
    ascii_uppercase -- a string containing all ASCII uppercase letters
    ascii_letters -- a string containing all ASCII letters
    digits -- a string containing all ASCII decimal digits
    hexdigits -- a string containing all ASCII hexadecimal digits
    octdigits -- a string containing all ASCII octal digits
    punctuation -- a string containing all ASCII punctuation characters
    printable -- a string containing all ASCII characters considered printable

CLASSES
    builtins.object
        Formatter
        Template
⋮ ⋮ ⋮

Nhập các mục cụ thể từ mô-đun thư viện để rút ngắn chương trình

  • Sử dụng
    help(string)
    
    8 để chỉ tải các mục cụ thể từ mô-đun thư viện
  • Sau đó tham khảo chúng trực tiếp mà không cần tên thư viện làm tiền tố

from string import ascii_letters

print('The ASCII letters are', ascii_letters)

The ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

Tạo bí danh cho mô-đun thư viện khi nhập nó để rút ngắn chương trình

  • Sử dụng
    help(string)
    
    9 để cung cấp cho thư viện một bí danh ngắn trong khi nhập thư viện
  • Sau đó tham khảo các mục trong thư viện bằng cách sử dụng tên rút gọn đó

import string as s

print(s.capwords('capitalise this sentence again please.'))

Capitalise This Sentence Again Please.

  • Thường dùng cho các thư viện thường xuyên sử dụng hoặc có tên dài
    • E. g. , Thư viện
      Help on module string:
      
      NAME
          string - A collection of string constants.
      
      MODULE REFERENCE
          https://docs.python.org/3.6/library/string
          
          The following documentation is automatically generated from the Python
          source files.  It may be incomplete, incorrect or include features that
          are considered implementation detail and may vary between Python
          implementations.  When in doubt, consult the module reference at the
          location listed above.
      
      DESCRIPTION
          Public module variables:
          
          whitespace -- a string containing all ASCII whitespace
          ascii_lowercase -- a string containing all ASCII lowercase letters
          ascii_uppercase -- a string containing all ASCII uppercase letters
          ascii_letters -- a string containing all ASCII letters
          digits -- a string containing all ASCII decimal digits
          hexdigits -- a string containing all ASCII hexadecimal digits
          octdigits -- a string containing all ASCII octal digits
          punctuation -- a string containing all ASCII punctuation characters
          printable -- a string containing all ASCII characters considered printable
      
      CLASSES
          builtins.object
              Formatter
              Template
      ⋮ ⋮ ⋮
      
      0 thường được đặt bí danh là
      Help on module string:
      
      NAME
          string - A collection of string constants.
      
      MODULE REFERENCE
          https://docs.python.org/3.6/library/string
          
          The following documentation is automatically generated from the Python
          source files.  It may be incomplete, incorrect or include features that
          are considered implementation detail and may vary between Python
          implementations.  When in doubt, consult the module reference at the
          location listed above.
      
      DESCRIPTION
          Public module variables:
          
          whitespace -- a string containing all ASCII whitespace
          ascii_lowercase -- a string containing all ASCII lowercase letters
          ascii_uppercase -- a string containing all ASCII uppercase letters
          ascii_letters -- a string containing all ASCII letters
          digits -- a string containing all ASCII decimal digits
          hexdigits -- a string containing all ASCII hexadecimal digits
          octdigits -- a string containing all ASCII octal digits
          punctuation -- a string containing all ASCII punctuation characters
          printable -- a string containing all ASCII characters considered printable
      
      CLASSES
          builtins.object
              Formatter
              Template
      ⋮ ⋮ ⋮
      
      1
  • Nhưng có thể làm cho chương trình khó hiểu hơn, vì người đọc phải tìm hiểu bí danh chương trình của bạn

Khám phá thư viện os

Thư viện os cung cấp cách truy cập chức năng của hệ điều hành

  1. Chức năng nào từ thư viện
    Help on module string:
    
    NAME
        string - A collection of string constants.
    
    MODULE REFERENCE
        https://docs.python.org/3.6/library/string
        
        The following documentation is automatically generated from the Python
        source files.  It may be incomplete, incorrect or include features that
        are considered implementation detail and may vary between Python
        implementations.  When in doubt, consult the module reference at the
        location listed above.
    
    DESCRIPTION
        Public module variables:
        
        whitespace -- a string containing all ASCII whitespace
        ascii_lowercase -- a string containing all ASCII lowercase letters
        ascii_uppercase -- a string containing all ASCII uppercase letters
        ascii_letters -- a string containing all ASCII letters
        digits -- a string containing all ASCII decimal digits
        hexdigits -- a string containing all ASCII hexadecimal digits
        octdigits -- a string containing all ASCII octal digits
        punctuation -- a string containing all ASCII punctuation characters
        printable -- a string containing all ASCII characters considered printable
    
    CLASSES
        builtins.object
            Formatter
            Template
    ⋮ ⋮ ⋮
    
    2 bạn có thể sử dụng để xác định thư mục làm việc hiện tại?

Giải pháp

  1. Sử dụng
    Help on module string:
    
    NAME
        string - A collection of string constants.
    
    MODULE REFERENCE
        https://docs.python.org/3.6/library/string
        
        The following documentation is automatically generated from the Python
        source files.  It may be incomplete, incorrect or include features that
        are considered implementation detail and may vary between Python
        implementations.  When in doubt, consult the module reference at the
        location listed above.
    
    DESCRIPTION
        Public module variables:
        
        whitespace -- a string containing all ASCII whitespace
        ascii_lowercase -- a string containing all ASCII lowercase letters
        ascii_uppercase -- a string containing all ASCII uppercase letters
        ascii_letters -- a string containing all ASCII letters
        digits -- a string containing all ASCII decimal digits
        hexdigits -- a string containing all ASCII hexadecimal digits
        octdigits -- a string containing all ASCII octal digits
        punctuation -- a string containing all ASCII punctuation characters
        printable -- a string containing all ASCII characters considered printable
    
    CLASSES
        builtins.object
            Formatter
            Template
    ⋮ ⋮ ⋮
    
    3, chúng tôi thấy rằng chúng tôi đã nhận được
    Help on module string:
    
    NAME
        string - A collection of string constants.
    
    MODULE REFERENCE
        https://docs.python.org/3.6/library/string
        
        The following documentation is automatically generated from the Python
        source files.  It may be incomplete, incorrect or include features that
        are considered implementation detail and may vary between Python
        implementations.  When in doubt, consult the module reference at the
        location listed above.
    
    DESCRIPTION
        Public module variables:
        
        whitespace -- a string containing all ASCII whitespace
        ascii_lowercase -- a string containing all ASCII lowercase letters
        ascii_uppercase -- a string containing all ASCII uppercase letters
        ascii_letters -- a string containing all ASCII letters
        digits -- a string containing all ASCII decimal digits
        hexdigits -- a string containing all ASCII hexadecimal digits
        octdigits -- a string containing all ASCII octal digits
        punctuation -- a string containing all ASCII punctuation characters
        printable -- a string containing all ASCII characters considered printable
    
    CLASSES
        builtins.object
            Formatter
            Template
    ⋮ ⋮ ⋮
    
    4 trả về một chuỗi đại diện cho thư mục làm việc hiện tại

Định vị đúng mô-đun

Với các biến

Help on module string:

NAME
    string - A collection of string constants.

MODULE REFERENCE
    https://docs.python.org/3.6/library/string
    
    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    Public module variables:
    
    whitespace -- a string containing all ASCII whitespace
    ascii_lowercase -- a string containing all ASCII lowercase letters
    ascii_uppercase -- a string containing all ASCII uppercase letters
    ascii_letters -- a string containing all ASCII letters
    digits -- a string containing all ASCII decimal digits
    hexdigits -- a string containing all ASCII hexadecimal digits
    octdigits -- a string containing all ASCII octal digits
    punctuation -- a string containing all ASCII punctuation characters
    printable -- a string containing all ASCII characters considered printable

CLASSES
    builtins.object
        Formatter
        Template
⋮ ⋮ ⋮
5,
Help on module string:

NAME
    string - A collection of string constants.

MODULE REFERENCE
    https://docs.python.org/3.6/library/string
    
    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    Public module variables:
    
    whitespace -- a string containing all ASCII whitespace
    ascii_lowercase -- a string containing all ASCII lowercase letters
    ascii_uppercase -- a string containing all ASCII uppercase letters
    ascii_letters -- a string containing all ASCII letters
    digits -- a string containing all ASCII decimal digits
    hexdigits -- a string containing all ASCII hexadecimal digits
    octdigits -- a string containing all ASCII octal digits
    punctuation -- a string containing all ASCII punctuation characters
    printable -- a string containing all ASCII characters considered printable

CLASSES
    builtins.object
        Formatter
        Template
⋮ ⋮ ⋮
6 và
Help on module string:

NAME
    string - A collection of string constants.

MODULE REFERENCE
    https://docs.python.org/3.6/library/string
    
    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    Public module variables:
    
    whitespace -- a string containing all ASCII whitespace
    ascii_lowercase -- a string containing all ASCII lowercase letters
    ascii_uppercase -- a string containing all ASCII uppercase letters
    ascii_letters -- a string containing all ASCII letters
    digits -- a string containing all ASCII decimal digits
    hexdigits -- a string containing all ASCII hexadecimal digits
    octdigits -- a string containing all ASCII octal digits
    punctuation -- a string containing all ASCII punctuation characters
    printable -- a string containing all ASCII characters considered printable

CLASSES
    builtins.object
        Formatter
        Template
⋮ ⋮ ⋮
7, bạn sẽ tạo một ngày ở định dạng iso tiêu chuẩn như thế nào

year = 2016
month = 10
day = 22

  1. Mô-đun thư viện tiêu chuẩn nào có thể giúp bạn?
  2. Bạn sẽ chọn chức năng nào từ mô-đun đó?
  3. Hãy thử viết một chương trình sử dụng chức năng

Giải pháp

Mô-đun datetime có vẻ như có thể giúp bạn

Bạn có thể sử dụng

Help on module string:

NAME
    string - A collection of string constants.

MODULE REFERENCE
    https://docs.python.org/3.6/library/string
    
    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    Public module variables:
    
    whitespace -- a string containing all ASCII whitespace
    ascii_lowercase -- a string containing all ASCII lowercase letters
    ascii_uppercase -- a string containing all ASCII uppercase letters
    ascii_letters -- a string containing all ASCII letters
    digits -- a string containing all ASCII decimal digits
    hexdigits -- a string containing all ASCII hexadecimal digits
    octdigits -- a string containing all ASCII octal digits
    punctuation -- a string containing all ASCII punctuation characters
    printable -- a string containing all ASCII characters considered printable

CLASSES
    builtins.object
        Formatter
        Template
⋮ ⋮ ⋮
8 để chuyển đổi ngày của mình

import datetime

iso_date = datetime.date(year, month, day).isoformat()
print(iso_date)

hoặc gọn hơn

The lower ascii letters are abcdefghijklmnopqrstuvwxyz
Capitalise This Sentence Please.
0

Khi nào có trợ giúp?

Khi một đồng nghiệp của bạn gõ

Help on module string:

NAME
    string - A collection of string constants.

MODULE REFERENCE
    https://docs.python.org/3.6/library/string
    
    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    Public module variables:
    
    whitespace -- a string containing all ASCII whitespace
    ascii_lowercase -- a string containing all ASCII lowercase letters
    ascii_uppercase -- a string containing all ASCII uppercase letters
    ascii_letters -- a string containing all ASCII letters
    digits -- a string containing all ASCII decimal digits
    hexdigits -- a string containing all ASCII hexadecimal digits
    octdigits -- a string containing all ASCII octal digits
    punctuation -- a string containing all ASCII punctuation characters
    printable -- a string containing all ASCII characters considered printable

CLASSES
    builtins.object
        Formatter
        Template
⋮ ⋮ ⋮
3, Python báo lỗi

The lower ascii letters are abcdefghijklmnopqrstuvwxyz
Capitalise This Sentence Please.
1

Đồng nghiệp của bạn đã quên làm gì?

Giải pháp

Nhập mô-đun hệ điều hành (

from string import ascii_letters

print('The ASCII letters are', ascii_letters)
0)

Nhập bằng bí danh

  1. Hãy điền vào chỗ trống để chương trình dưới đây in ra dòng chữ
    from string import ascii_letters
    
    print('The ASCII letters are', ascii_letters)
    
    1
  2. Viết lại chương trình để nó sử dụng
    help(string)
    
    0 mà không có
    from string import ascii_letters
    
    print('The ASCII letters are', ascii_letters)
    
    3
  3. Bạn thấy hình thức nào dễ đọc hơn?

The lower ascii letters are abcdefghijklmnopqrstuvwxyz
Capitalise This Sentence Please.
2

Giải pháp

The lower ascii letters are abcdefghijklmnopqrstuvwxyz
Capitalise This Sentence Please.
3

có thể được viết như

The lower ascii letters are abcdefghijklmnopqrstuvwxyz
Capitalise This Sentence Please.
4

Vì bạn mới viết mã và đã quen thuộc với nó, bạn có thể thực sự thấy phiên bản đầu tiên dễ đọc hơn. Nhưng khi cố gắng đọc một đoạn mã khổng lồ do người khác viết hoặc khi quay lại đoạn mã khổng lồ của chính bạn sau vài tháng, các tên không viết tắt thường dễ dàng hơn, hãy mong đợi nơi có các quy ước viết tắt rõ ràng

Có nhiều cách để nhập thư viện

Ghép các câu lệnh in sau với các lệnh gọi thư viện thích hợp

cuộc gọi thư viện

The lower ascii letters are abcdefghijklmnopqrstuvwxyz
Capitalise This Sentence Please.
5

Lệnh in

The lower ascii letters are abcdefghijklmnopqrstuvwxyz
Capitalise This Sentence Please.
6

Giải pháp

A2) Nhập

from string import ascii_letters

print('The ASCII letters are', ascii_letters)
4 từ
help(string)
3 cung cấp các phương thức
from string import ascii_letters

print('The ASCII letters are', ascii_letters)
4 B3) Nhập
help(string)
3 cung cấp các phương thức như
from string import ascii_letters

print('The ASCII letters are', ascii_letters)
8, nhưng yêu cầu cú pháp
from string import ascii_letters

print('The ASCII letters are', ascii_letters)
9. C1) Nhập
help(string)
3 với bí danh
The ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
1 cho phép
The ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
2

Nhập các mặt hàng cụ thể

  1. Điền vào chỗ trống để chương trình dưới đây in ra chữ
    The ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    
    3
  2. Bạn có thấy phiên bản này dễ đọc hơn những phiên bản trước không?
  3. Tại sao các lập trình viên không luôn sử dụng dạng
    help(string)
    
    0 này?

The lower ascii letters are abcdefghijklmnopqrstuvwxyz
Capitalise This Sentence Please.
7

Giải pháp

The lower ascii letters are abcdefghijklmnopqrstuvwxyz
Capitalise This Sentence Please.
8

Rất có thể bạn thấy phiên bản này dễ đọc hơn vì nó ít dày đặc hơn. Lý do chính không sử dụng hình thức nhập khẩu này là để tránh đụng độ tên. Chẳng hạn, bạn sẽ không nhập

The ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
5 theo cách này nếu bạn cũng muốn sử dụng tên
The ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
5 cho một biến hoặc hàm của riêng bạn. Hoặc nếu bạn cũng nhập một hàm có tên
The ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
5 từ một thư viện khác

Các ví dụ về thư viện Python là gì?

10 thư viện Python hàng đầu .
gấu trúc
NumPy
máy ảnh
TenorFlow
Tìm hiểu Scikit
khoa học viễn tưởng
PyTorch

Thư viện và mô-đun trong Python là gì?

Một mô-đun là một tập hợp mã hoặc chức năng sử dụng. py tiện ích mở rộng. Thư viện Python là một tập hợp các mô-đun hoặc gói có liên quan đi kèm với nhau . Nó được sử dụng bởi các lập trình viên cũng như các nhà phát triển. Nó chủ yếu được sử dụng bởi các thành viên cộng đồng, nhà phát triển và nhà nghiên cứu.

Tại sao thư viện Python được sử dụng?

Thư viện chứa các mô-đun dựng sẵn (được viết bằng C) cung cấp quyền truy cập vào chức năng hệ thống, chẳng hạn như tệp I/O mà các lập trình viên Python không thể truy cập được, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming.

Các thư viện trong Python ở đâu?

Thường nằm trong /lib/site-packages trong thư mục Python của bạn . (Ít nhất, trên Windows. ) Bạn có thể sử dụng sys. đường dẫn để tìm ra những thư mục được tìm kiếm cho các mô-đun.