Toán học có được bao gồm trong python không?

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(math)
    
    3 để 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(math)
    
    4
    • Python sử dụng
      help(math)
      
      5 có nghĩa là “một phần của”
  • Sử dụng
    help(math)
    
    6, một trong các mô-đun trong thư viện tiêu chuẩn

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
    • help(math)
      
      7 sẽ không hoạt động. tham chiếu đến
      help(math)
      
      8 không bằng cách nào đó “kế thừa” tham chiếu của hàm đến
      help(math)
      
      6

Sử dụng Help on module math: NAME math MODULE REFERENCE http://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

  • 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
    http://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
    Help on module math:
    
    NAME
        math
    
    MODULE REFERENCE
        http://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.
    ⋮ ⋮ ⋮
    
    1 để 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ố

help(math)
3

help(math)
4

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 on module math:
    
    NAME
        math
    
    MODULE REFERENCE
        http://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.
    ⋮ ⋮ ⋮
    
    2 để cung cấp cho thư viện một bí danh ngắn trong khi nhập 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 đó

help(math)
6

help(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ị
      Help on module math:
      
      NAME
          math
      
      MODULE REFERENCE
          http://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.
      ⋮ ⋮ ⋮
      
      3 thường có bí danh là
      Help on module math:
      
      NAME
          math
      
      MODULE REFERENCE
          http://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.
      ⋮ ⋮ ⋮
      
      4
  • 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

  1. Bạn có thể sử dụng chức năng nào từ mô-đun
    help(math)
    
    6 để tính căn bậc hai mà không cần sử dụng
    Help on module math:
    
    NAME
        math
    
    MODULE REFERENCE
        http://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?
  2. Vì thư viện chứa chức năng này, tại sao lại tồn tại
    Help on module math:
    
    NAME
        math
    
    MODULE REFERENCE
        http://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?

Dung dịch

  1. Sử dụng
    Help on module math:
    
    NAME
        math
    
    MODULE REFERENCE
        http://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.
    ⋮ ⋮ ⋮
    
    8, chúng tôi thấy rằng chúng tôi đã có
    Help on module math:
    
    NAME
        math
    
    MODULE REFERENCE
        http://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ài
    help(math)
    
    30, vì vậy chúng tôi có thể sử dụng
    help(math)
    
    31 để tìm căn bậc hai
  2. Hàm

    help(math)
    
    30 được cho là dễ đọc hơn hàm
    help(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ày

    Ngoài ra, thiết kế của thư viện

    help(math)
    
    6 của Python có nguồn gốc từ tiêu chuẩn C, bao gồm cả
    help(math)
    
    30 và
    Help on module math:
    
    NAME
        math
    
    MODULE REFERENCE
        http://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, do đó, một chút lịch sử lập trình được hiển thị trong tên hàm của Python

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

Bạn muốn chọn một ký tự ngẫu nhiên từ một chuỗi

help(math)
2

  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

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

help(math)
37 hoặc
help(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)
6

hoặc gọn hơn

pi is 3.141592653589793
cos(pi) is -1.0
0

Có lẽ bạn đã tìm thấy hàm

help(math)
40?

pi is 3.141592653589793
cos(pi) is -1.0
1

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

help(math)
41 thực hiện chính xác những gì chúng ta muốn

pi is 3.141592653589793
cos(pi) is -1.0
2

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

pi is 3.141592653589793
cos(pi) is -1.0
3

Dung dịch

pi is 3.141592653589793
cos(pi) is -1.0
4

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

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

Help on module math:

NAME
    math

MODULE REFERENCE
    http://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.
⋮ ⋮ ⋮
8, Python báo lỗi

pi is 3.141592653589793
cos(pi) is -1.0
5

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

Dung dịch

Nhập mô-đun toán học (

help(math)
43)

Nhập bằng bí danh

  1. Điền vào chỗ trống để chương trình dưới đây in ra dòng chữ
    help(math)
    
    44
  2. Viết lại chương trình để nó sử dụng
    help(math)
    
    3 mà không có
    help(math)
    
    46
  3. Bạn thấy hình thức nào dễ đọc hơn?

pi is 3.141592653589793
cos(pi) is -1.0
6

Dung dịch

pi is 3.141592653589793
cos(pi) is -1.0
7

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

pi is 3.141592653589793
cos(pi) is -1.0
8

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

  1. help(math)
    
    47
  2. help(math)
    
    48
  3. help(math)
    
    49

cuộc gọi thư viện

  1. help(math)
    
    60
  2. help(math)
    
    43
  3. help(math)
    
    62
  4. help(math)
    
    63

Dung dịch

  1. Thư viện cuộc gọi 1 và 4. Để trực tiếp đề cập đến
    help(math)
    
    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ệnh
    Help on module math:
    
    NAME
        math
    
    MODULE REFERENCE
        http://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.
    ⋮ ⋮ ⋮
    
    1. Trong khi cuộc gọi thư viện 1 nhập cụ thể hai chức năng
    help(math)
    
    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ô-đun
    help(math)
    
    6
  2. Thư viện cuộc gọi 3. Ở đây
    help(math)
    
    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áp
    Help on module math:
    
    NAME
        math
    
    MODULE REFERENCE
        http://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.
    ⋮ ⋮ ⋮
    
    2 - nó tạo bí danh cho
    help(math)
    
    6 ở dạng tên rút gọn
    help(math)
    
    42
  3. Cuộc gọi thư viện 2. Ở đây
    help(math)
    
    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ọi
    help(math)
    
    20 thông thường là đủ

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

help(math)
3 cho chúng ta biết rõ ràng rằng hàm
help(math)
64 được nhập từ mô-đun
help(math)
6, nhưng lệnh gọi thư viện 4 không chuyển tải thông tin này

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 dòng chữ
    help(math)
    
    44
  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(math)
    
    3 này?

pi is 3.141592653589793
cos(pi) is -1.0
9

Dung dịch

help(math)
0

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

help(math)
26 theo cách này nếu bạn cũng muốn sử dụng tên
help(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ên
help(math)
26 từ một thư viện khác

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.