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
0 để tải mô-đun thư viện vào bộ nhớ của chương trìnhhelp[string]
- Sau đó gọi những thứ từ mô-đun là
1help[string]
- Python sử dụng
2 có nghĩa là “một phần của”help[string]
- Python sử dụng
- Sử dụng
3, một trong các mô-đun trong thư viện tiêu chuẩnhelp[string]
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
4 sẽ không hoạt động. tham chiếu đếnhelp[string]
5 không bằng cách nào đó “kế thừa” tham chiếu của hàm tớihelp[string]
3help[string]
Sử dụng help[string]
7 để tìm hiểu về nội dung của mô-đun thư viện
help[string]
- 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
//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
8 để chỉ tải các mục cụ thể từ mô-đun thư việnhelp[string]
- 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
9 để cung cấp cho thư viện một bí danh ngắn trong khi nhập thư việnhelp[string]
- 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
0 thường được đặt bí danh làHelp on module string: NAME string - A collection of string constants. MODULE REFERENCE //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 ⋮ ⋮ ⋮
1Help on module string: NAME string - A collection of string constants. MODULE REFERENCE //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 ⋮ ⋮ ⋮
- E. g. , Thư viện
- 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
- Chức năng nào từ thư viện
2 bạn có thể sử dụng để xác định thư mục làm việc hiện tại?Help on module string: NAME string - A collection of string constants. MODULE REFERENCE //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 ⋮ ⋮ ⋮
Giải pháp
- Sử dụng
3, chúng tôi thấy rằng chúng tôi đã nhận đượcHelp on module string: NAME string - A collection of string constants. MODULE REFERENCE //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ạiHelp on module string: NAME string - A collection of string constants. MODULE REFERENCE //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 vị đúng mô-đun
Với các biến
5,Help on module string: NAME string - A collection of string constants. MODULE REFERENCE //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 //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àoHelp on module string: NAME string - A collection of string constants. MODULE REFERENCE //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 ⋮ ⋮ ⋮
year = 2016 month = 10 day = 22
- Mô-đun thư viện tiêu chuẩn nào có thể giúp bạn?
- Bạn sẽ chọn chức năng nào từ mô-đun đó?
- 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
8 để chuyển đổi ngày của mìnhHelp on module string: NAME string - A collection of string constants. MODULE REFERENCE //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 ⋮ ⋮ ⋮
import datetime iso_date = datetime.date[year, month, day].isoformat[] print[iso_date]
hoặc gọn hơn
0The lower ascii letters are abcdefghijklmnopqrstuvwxyz Capitalise This Sentence Please.
Khi nào có trợ giúp?
Khi một đồng nghiệp của bạn gõ
3, Python báo lỗiHelp on module string: NAME string - A collection of string constants. MODULE REFERENCE //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 ⋮ ⋮ ⋮
1The lower ascii letters are abcdefghijklmnopqrstuvwxyz Capitalise This Sentence Please.
Đồ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 [
0]from string import ascii_letters print['The ASCII letters are', ascii_letters]
Nhập bằng bí danh
- Hãy điền vào chỗ trống để chương trình dưới đây in ra dòng chữ
1from string import ascii_letters print['The ASCII letters are', ascii_letters]
- Viết lại chương trình để nó sử dụng
0 mà không cóhelp[string]
3from string import ascii_letters print['The ASCII letters are', ascii_letters]
- Bạn thấy hình thức nào dễ đọc hơn?
2The lower ascii letters are abcdefghijklmnopqrstuvwxyz Capitalise This Sentence Please.
Giải pháp
3The lower ascii letters are abcdefghijklmnopqrstuvwxyz Capitalise This Sentence Please.
có thể được viết như
4The lower ascii letters are abcdefghijklmnopqrstuvwxyz Capitalise This Sentence Please.
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
5The lower ascii letters are abcdefghijklmnopqrstuvwxyz Capitalise This Sentence Please.
Lệnh in
6The lower ascii letters are abcdefghijklmnopqrstuvwxyz Capitalise This Sentence Please.
Giải pháp
A2] Nhập
4 từfrom string import ascii_letters print['The ASCII letters are', ascii_letters]
3 cung cấp các phương thứchelp[string]
4 B3] Nhậpfrom string import ascii_letters print['The ASCII letters are', ascii_letters]
3 cung cấp các phương thức nhưhelp[string]
8, nhưng yêu cầu cú phápfrom string import ascii_letters print['The ASCII letters are', ascii_letters]
9. C1] Nhậpfrom string import ascii_letters print['The ASCII letters are', ascii_letters]
3 với bí danhhelp[string]
1 cho phépThe ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
2The ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
Nhập các mặt hàng cụ thể
- Điền vào chỗ trống để chương trình dưới đây in ra chữ
3The ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
- 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?
- Tại sao các lập trình viên không luôn sử dụng dạng
0 này?help[string]
7The lower ascii letters are abcdefghijklmnopqrstuvwxyz Capitalise This Sentence Please.
Giải pháp
8The lower ascii letters are abcdefghijklmnopqrstuvwxyz Capitalise This Sentence Please.
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
5 theo cách này nếu bạn cũng muốn sử dụng tênThe 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ênThe ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
5 từ một thư viện khácThe ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
Các ví dụ về thư viện Python là gì?
10 thư viện Python hàng đầu .gấu trúcNumPymáy ảnhTenorFlowTìm hiểu Scikitkhoa học viễn tưởngPyTorchThư 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.