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
3 để tải mô-đun thư viện vào bộ nhớ của chương trìnhhelp[math]
- Sau đó gọi những thứ từ mô-đun là
4help[math]
- Python sử dụng
5 có nghĩa là “một phần của”help[math]
- Python sử dụng
- Sử dụng
6, một trong các mô-đun trong thư viện tiêu chuẩnhelp[math]
import math
print['pi is', math.pi]
print['cos[pi] is', math.cos[math.pi]]
pi is 3.141592653589793
cos[pi] is -1.0
- Phải tham khảo từng mục với tên của mô-đun
7 sẽ không hoạt động. tham chiếu đếnhelp[math]
8 không bằng cách nào đó “kế thừa” tham chiếu của hàm đếnhelp[math]
6help[math]
Sử dụng Help on module math:
NAME
math
MODULE REFERENCE
//docs.python.org/3/library/math
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
This module is always available. It provides access to the
mathematical functions defined by the C standard.
FUNCTIONS
acos[x, /]
Return the arc cosine [measured in radians] of x.
⋮ ⋮ ⋮
0 để tìm hiểu về nội dung của mô-đun thư viện
Help on module math:
NAME
math
MODULE REFERENCE
//docs.python.org/3/library/math
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
This module is always available. It provides access to the
mathematical functions defined by the C standard.
FUNCTIONS
acos[x, /]
Return the arc cosine [measured in radians] of x.
⋮ ⋮ ⋮
- Hoạt động giống như trợ giúp cho một chức năng
help[math]
Help on module math:
NAME
math
MODULE REFERENCE
//docs.python.org/3/library/math
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
This module is always available. It provides access to the
mathematical functions defined by the C standard.
FUNCTIONS
acos[x, /]
Return the arc cosine [measured in radians] of x.
⋮ ⋮ ⋮
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
1 để chỉ tải các mục cụ thể từ mô-đun thư việnHelp on module math: NAME math MODULE REFERENCE //docs.python.org/3/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos[x, /] Return the arc cosine [measured in radians] of x. ⋮ ⋮ ⋮
- 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ố
help[math]
3help[math]
4Tạo bí danh cho mô-đun thư viện khi nhập nó để rút ngắn chương trình
- Sử dụng
2 để cung cấp cho thư viện một bí danh ngắn trong khi nhập nóHelp on module math: NAME math MODULE REFERENCE //docs.python.org/3/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos[x, /] Return the arc cosine [measured in radians] of x. ⋮ ⋮ ⋮
- 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 đó
help[math]
6help[math]
4- 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 vẽ đồ thị
3 thường có bí danh làHelp on module math: NAME math MODULE REFERENCE //docs.python.org/3/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos[x, /] Return the arc cosine [measured in radians] of x. ⋮ ⋮ ⋮
4Help on module math: NAME math MODULE REFERENCE //docs.python.org/3/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos[x, /] Return the arc cosine [measured in radians] of x. ⋮ ⋮ ⋮
- E. g. , thư viện vẽ đồ thị
- 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á mô-đun toán học
- Bạn có thể sử dụng chức năng nào từ mô-đun
6 để tính căn bậc hai mà không cần sử dụnghelp[math]
6?Help on module math: NAME math MODULE REFERENCE //docs.python.org/3/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos[x, /] Return the arc cosine [measured in radians] of x. ⋮ ⋮ ⋮
- Vì thư viện chứa chức năng này, tại sao lại tồn tại
6?Help on module math: NAME math MODULE REFERENCE //docs.python.org/3/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos[x, /] Return the arc cosine [measured in radians] of x. ⋮ ⋮ ⋮
Dung dịch
- Sử dụng
8, chúng tôi thấy rằng chúng tôi đã cóHelp on module math: NAME math MODULE REFERENCE //docs.python.org/3/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos[x, /] Return the arc cosine [measured in radians] of x. ⋮ ⋮ ⋮
9 ngoàiHelp on module math: NAME math MODULE REFERENCE //docs.python.org/3/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos[x, /] Return the arc cosine [measured in radians] of x. ⋮ ⋮ ⋮
30, vì vậy chúng tôi có thể sử dụnghelp[math]
31 để tìm căn bậc haihelp[math]
Hàm
30 được cho là dễ đọc hơn hàmhelp[math]
31 khi thực hiện các phương trình. Khả năng đọc là nền tảng của lập trình tốt, do đó, thật hợp lý khi cung cấp một chức năng đặc biệt cho trường hợp phổ biến cụ thể nàyhelp[math]
Ngoài ra, thiết kế của thư viện
6 của Python có nguồn gốc từ tiêu chuẩn C, bao gồm cảhelp[math]
30 vàhelp[math]
9, do đó, một chút lịch sử lập trình được hiển thị trong tên hàm của PythonHelp on module math: NAME math MODULE REFERENCE //docs.python.org/3/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos[x, /] Return the arc cosine [measured in radians] of x. ⋮ ⋮ ⋮
Định vị đúng mô-đun
Bạn muốn chọn một ký tự ngẫu nhiên từ một chuỗi
2help[math]
- 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
Dung dịch
Mô-đun ngẫu nhiên có vẻ như có thể giúp
Chuỗi có 11 ký tự, mỗi ký tự có chỉ số vị trí từ 0 đến 10. Bạn có thể sử dụng các hàm
37 hoặchelp[math]
38 để lấy một số nguyên ngẫu nhiên trong khoảng từ 0 đến 10, sau đó chọn ký tựhelp[math]
39 tại chỉ mục đóhelp[math]
6help[math]
hoặc gọn hơn
0pi is 3.141592653589793 cos[pi] is -1.0
Có lẽ bạn đã tìm thấy hàm
40?help[math]
1pi is 3.141592653589793 cos[pi] is -1.0
Lưu ý rằng chức năng này trả về một danh sách các giá trị. Chúng ta sẽ tìm hiểu về danh sách trong tập 11
Giải pháp đơn giản và ngắn gọn nhất là hàm
41 thực hiện chính xác những gì chúng ta muốnhelp[math]
2pi is 3.141592653589793 cos[pi] is -1.0
Jigsaw Puzzle [Bài toán Parson] Ví dụ lập trình
Sắp xếp lại các câu lệnh sau để một cơ sở DNA ngẫu nhiên được in và chỉ mục của nó trong chuỗi. Không phải tất cả các tuyên bố có thể cần thiết. Vui lòng sử dụng/thêm các biến trung gian
3pi is 3.141592653589793 cos[pi] is -1.0
Dung dịch
4pi is 3.141592653589793 cos[pi] is -1.0
Khi nào có trợ giúp?
Khi một đồng nghiệp của bạn gõ
8, Python báo lỗiHelp on module math: NAME math MODULE REFERENCE //docs.python.org/3/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos[x, /] Return the arc cosine [measured in radians] of x. ⋮ ⋮ ⋮
5pi is 3.141592653589793 cos[pi] is -1.0
Đồng nghiệp của bạn đã quên làm gì?
Dung dịch
Nhập mô-đun toán học [
43]help[math]
Nhập bằng bí danh
- Điền vào chỗ trống để chương trình dưới đây in ra dòng chữ
44help[math]
- Viết lại chương trình để nó sử dụng
3 mà không cóhelp[math]
46help[math]
- Bạn thấy hình thức nào dễ đọc hơn?
6pi is 3.141592653589793 cos[pi] is -1.0
Dung dịch
7pi is 3.141592653589793 cos[pi] is -1.0
có thể được viết như
8pi is 3.141592653589793 cos[pi] is -1.0
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, trừ khi 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
Lệnh in
47help[math]
48help[math]
49help[math]
cuộc gọi thư viện
60help[math]
43help[math]
62help[math]
63help[math]
Dung dịch
- Thư viện cuộc gọi 1 và 4. Để trực tiếp đề cập đến
64 vàhelp[math]
8 mà không có tên thư viện làm tiền tố, bạn cần sử dụng câu lệnhhelp[math]
1. Trong khi cuộc gọi thư viện 1 nhập cụ thể hai chức năngHelp on module math: NAME math MODULE REFERENCE //docs.python.org/3/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos[x, /] Return the arc cosine [measured in radians] of x. ⋮ ⋮ ⋮
64 vàhelp[math]
8, cuộc gọi thư viện 4 nhập tất cả các chức năng trong mô-đunhelp[math]
6help[math]
- Thư viện cuộc gọi 3. Ở đây
64 vàhelp[math]
8 được gọi với tên thư viện rút gọn làhelp[math]
42 thay vìhelp[math]
6. Cuộc gọi thư viện 3 thực hiện chính xác điều đó bằng cách sử dụng cú pháphelp[math]
2 - nó tạo bí danh choHelp on module math: NAME math MODULE REFERENCE //docs.python.org/3/library/math 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 This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos[x, /] Return the arc cosine [measured in radians] of x. ⋮ ⋮ ⋮
6 ở dạng tên rút gọnhelp[math]
42help[math]
- Cuộc gọi thư viện 2. Ở đây
64 vàhelp[math]
8 được gọi với tên thư viện thông thường làhelp[math]
6, vì vậy chỉ cần gọihelp[math]
20 thông thường là đủhelp[math]
Ghi chú. mặc dù cuộc gọi thư viện 4 hoạt động, việc nhập tất cả các tên từ một mô-đun bằng cách nhập ký tự đại diện không được khuyến nghị vì nó làm cho không rõ tên nào từ mô-đun được sử dụng trong mã. Nói chung, tốt nhất là làm cho quá trình nhập của bạn càng cụ thể càng tốt và chỉ nhập những gì mã của bạn sử dụng. Trong lệnh gọi thư viện 1, câu lệnh
3 cho chúng ta biết rõ ràng rằng hàmhelp[math]
64 được nhập từ mô-đunhelp[math]
6, nhưng lệnh gọi thư viện 4 không chuyển tải thông tin nàyhelp[math]
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 dòng chữ
44help[math]
- 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
3 này?help[math]
9pi is 3.141592653589793 cos[pi] is -1.0
Dung dịch
0help[math]
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
26 theo cách này nếu bạn cũng muốn sử dụng tênhelp[math]
26 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ênhelp[math]
26 từ một thư viện kháchelp[math]
Python có đi kèm với toán học không?
Python có một tập hợp các hàm toán học tích hợp sẵn, bao gồm mô-đun toán học mở rộng , cho phép bạn thực hiện các tác vụ toán học trên các số.Tên của thư viện toán học trong Python là gì?
Numpy . Thư viện numpy trong Python được sử dụng rộng rãi nhất để thực hiện các phép toán liên quan đến ma trận.