Python và javascript giống nhau như thế nào?

Trong bài viết này, tôi cung cấp một so sánh các tính năng được sử dụng phổ biến nhất của Python và JavaScript. Các tính năng ít được sử dụng được bỏ qua

Chủ đềJavaScriptPythonchuẩnTài liệu ECMAScriptPython 3đánh giáđộngđộnghiệu suấtnhanhchậm trừ khi các thư viện gọi các hàm C/C++hướng dẫn phong cáchPrettierPEP 8, Thụt lề phổ biến nhất 2 dấu cách4 dấu cách [không phải tab]kiểu ép buộctiềm ẩnrõ ràng ngoại trừ giữa các loại số

Ghi chú. PEP là viết tắt của Đề xuất cải tiến Python

Một số điều kỳ lạ về định dạng mã Python bao gồm

  • để lại hai dòng trống trước và sau mỗi định nghĩa hàm và lớp
  • để lại hai khoảng trắng trước mỗi nhận xét xuất hiện trên cùng một dòng với mã

Ưu và nhược điểm

ưu

  • khả năng chạy trong trình duyệt web [máy khách] và từ dòng lệnh [máy chủ] bằng Node. js
  • hỗ trợ tuyệt vời cho mã không đồng bộ
  • hiệu suất
  • cú pháp nhỏ gọn cho lập trình chức năng [e. g. , 
        """Return the average of a sequence of numbers."""
    1 so với. công cụ chức năng]
  • có thể sử dụng TypeScript, một siêu bộ JavaScript, để thêm kiểm tra loại

Nhược điểm

  • vẫn đang trong quá trình chuyển đổi từ cú pháp
        """Return the average of a sequence of numbers."""
    2 sang
        """Return the average of a sequence of numbers."""
    3 trong Nút. js
  • ép buộc kiểu có thể dẫn đến kết quả đáng ngạc nhiên nếu không quen thuộc với chúng

ưu

  • nhắm mục tiêu vào kịch bản và phát triển ứng dụng nhanh chóng
  • số lượng và sự trưởng thành của các thư viện để phân tích dữ liệu và học máy
  • nhiều loại số, không chỉ float có độ chính xác kép
  • một số cú pháp dễ dàng hơn cho người mới bắt đầu
    • không có dấu ngoặc nhọn hoặc dấu chấm phẩy và ít dấu ngoặc đơn hơn
    •     """Return the average of a sequence of numbers."""
      4 thay vì
          """Return the average of a sequence of numbers."""
      5,
          """Return the average of a sequence of numbers."""
      6 thay vì
          """Return the average of a sequence of numbers."""
      7 và
          """Return the average of a sequence of numbers."""
      8 thay vì
          """Return the average of a sequence of numbers."""
      9
    • function myFunction[p1, p2] {
        console.log['myFunction: p1 =', p1, 'p2 =', p2];
      }
       
      // Call function above after one second.
      const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
      if [tiredOfWaiting] clearTimeout[id1];
      0 so với.
      function myFunction[p1, p2] {
        console.log['myFunction: p1 =', p1, 'p2 =', p2];
      }
       
      // Call function above after one second.
      const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
      if [tiredOfWaiting] clearTimeout[id1];
      1
  • có thể thêm các chức năng được triển khai trong C/C++ hoặc bất kỳ ngôn ngữ nào có thể gọi được từ C
  • có thể sử dụng gợi ý loại và các công cụ như mypy để thêm kiểm tra loại
  • thỏa mãn một cách kỳ lạ khi gõ ít ký tự hơn [e. g. , nhận xét bằng
    function myFunction[p1, p2] {
      console.log['myFunction: p1 =', p1, 'p2 =', p2];
    }
     
    // Call function above after one second.
    const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
    if [tiredOfWaiting] clearTimeout[id1];
    2 thay vì
    function myFunction[p1, p2] {
      console.log['myFunction: p1 =', p1, 'p2 =', p2];
    }
     
    // Call function above after one second.
    const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
    if [tiredOfWaiting] clearTimeout[id1];
    3]

Nhược điểm

  • Mặc dù quy ước của Python là phân tách các từ trong tên biến, hàm và phương thức nhiều từ bằng dấu gạch dưới, nhưng có nhiều ví dụ không sử dụng dấu tách. Điều này gây khó khăn cho việc đoán đúng tên
  • Tham chiếu từ điển để truy xuất và đặt giá trị dài dòng hơn tham chiếu đối tượng JavaScript. Thay vì sử dụng cú pháp dấu chấm như
    function myFunction[p1, p2] {
      console.log['myFunction: p1 =', p1, 'p2 =', p2];
    }
     
    // Call function above after one second.
    const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
    if [tiredOfWaiting] clearTimeout[id1];
    4, chúng ta phải sử dụng
    function myFunction[p1, p2] {
      console.log['myFunction: p1 =', p1, 'p2 =', p2];
    }
     
    // Call function above after one second.
    const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
    if [tiredOfWaiting] clearTimeout[id1];
    5 hoặc
    function myFunction[p1, p2] {
      console.log['myFunction: p1 =', p1, 'p2 =', p2];
    }
     
    // Call function above after one second.
    const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
    if [tiredOfWaiting] clearTimeout[id1];
    6 ngay cả khi khóa là một từ đơn
  • Hàm ẩn danh được giới hạn trong một biểu thức
  • Các hàm lambda dài dòng hơn các hàm mũi tên JavaScript [
    function myFunction[p1, p2] {
      console.log['myFunction: p1 =', p1, 'p2 =', p2];
    }
     
    // Call function above after one second.
    const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
    if [tiredOfWaiting] clearTimeout[id1];
    7 so với.
    function myFunction[p1, p2] {
      console.log['myFunction: p1 =', p1, 'p2 =', p2];
    }
     
    // Call function above after one second.
    const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
    if [tiredOfWaiting] clearTimeout[id1];
    8]
  • Toán tử bậc ba cổ điển sử dụng
    function myFunction[p1, p2] {
      console.log['myFunction: p1 =', p1, 'p2 =', p2];
    }
     
    // Call function above after one second.
    const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
    if [tiredOfWaiting] clearTimeout[id1];
    9 và
        """Return the average of a sequence of numbers."""
    90 không được hỗ trợ. Xem phần logic điều kiện để biết ví dụ
  • Không có cú pháp cho nhận xét nhiều dòng, vì vậy việc nhận xét một khối mã rất tẻ nhạt
  • Các phương thức ma thuật như
        """Return the average of a sequence of numbers."""
    91 sử dụng tên "dunder" [dành cho dấu gạch dưới kép], đây là một cách kỳ lạ và dài dòng để phân biệt các giá trị đặc biệt. Các ngôn ngữ lập trình khác thường sử dụng một tiền tố ký tự đặc biệt. Xem danh sách trong phần phương pháp ma thuật Python
  • Việc sử dụng quá tải toán tử [được hỗ trợ bởi các phương thức ma thuật] có thể gây nhầm lẫn
  • Đây không phải là hỗ trợ tích hợp cho mã không đồng bộ cho đến khi mô-đun
        """Return the average of a sequence of numbers."""
    92 được thêm vào Python 3. 4. Một số tính năng yêu cầu Python 3. 7+
  • Các chương trình Python có hiệu suất kém. Ví dụ, xem Trò chơi Điểm chuẩn Ngôn ngữ Máy tính
  • V3 chứa các thay đổi vi phạm, vì vậy cần phải cẩn thận khi đọc tài liệu và ví dụ để xác minh tính tương thích của phiên bản

Tập lệnh đang chạy

Các tệp nguồn JavaScript có phần mở rộng là

    """Return the average of a sequence of numbers."""
93 hoặc
    """Return the average of a sequence of numbers."""
94 [đối với các mô-đun ECMAScript]

Để chạy tập lệnh JavaScript bên ngoài trình duyệt web

Các tệp nguồn Python có phần mở rộng là

    """Return the average of a sequence of numbers."""
95

Nhiều từ trong tên tệp phải được phân tách bằng dấu gạch dưới chứ không phải dấu gạch nối vì tên tệp trở thành tên mô-đun và dấu gạch ngang không hợp lệ trong tên mô-đun. Tránh sử dụng tên tệp khớp với tên của mô-đun hiện có vì làm như vậy sẽ khiến bạn không thể nhập mô-đun hiện có

Để chạy tập lệnh Python

  • cài đặt trình thông dịch Python từ python. tổ chức
  • nhập
        """Return the average of a sequence of numbers."""
    96 hoặc
        """Return the average of a sequence of numbers."""
    97

Trong cả hai trường hợp, đối số dòng lệnh có thể được chuyển đến tập lệnh

  • Một tập lệnh JavaScript chạy trong Node. js có thể lấy đối số dòng lệnh từ mảng
        """Return the average of a sequence of numbers."""
    98. Phần tử đầu tiên là đường dẫn đến tệp thực thi
        """Return the average of a sequence of numbers."""
    99, phần tử thứ hai là đường dẫn đến tập lệnh đang chạy và các phần tử còn lại là đối số dòng lệnh
  • Tập lệnh Python có thể nhận đối số dòng lệnh từ
        """Return the average of a sequence of numbers."""
    900. Phần tử đầu tiên là đường dẫn đến tập lệnh đang chạy và các phần tử còn lại là đối số dòng lệnh

Để tạo tệp nguồn Python có thể thực thi trực tiếp trong hệ thống UNIX

  • Thêm phần này làm dòng đầu tiên.
        """Return the average of a sequence of numbers."""
    901
  • Làm cho tệp có thể thực thi được bằng cách nhập
        """Return the average of a sequence of numbers."""
    902
  • Để chạy nó từ một thiết bị đầu cuối, hãy nhập
        """Return the average of a sequence of numbers."""
    903

Để tự động khởi động lại tập lệnh khi tập lệnh hoặc tệp mà tập lệnh nhập bị sửa đổi

  1. Cài đặt nút. js
  2.     """Return the average of a sequence of numbers."""
    904
  3. Nếu chạy tập lệnh JavaScript, hãy nhập
        """Return the average of a sequence of numbers."""
    905
  4. Nếu chạy tập lệnh Python, hãy nhập
        """Return the average of a sequence of numbers."""
    906

Tìm sự giúp đỡ

Trong JavaScript, thực hiện các tìm kiếm trên web bắt đầu bằng "MDN" [đối với Mạng nhà phát triển Mozilla] theo sau là cụm từ tìm kiếm JavaScript. Ví dụ: "mdn regrec"

Để được trợ giúp về Python, hãy xem danh sách tài nguyên ở cuối bài viết này

Bạn cũng có thể nhập lệnh

    """Return the average of a sequence of numbers."""
907 để bắt đầu REPL và nhập
    """Return the average of a sequence of numbers."""
908 để bắt đầu tiện ích trợ giúp. Sau khi vào trong, hãy nhập tên của mô-đun để nhận trợ giúp về mô-đun đó. Để thoát khỏi tiện ích trợ giúp và quay lại REPL của Python, hãy nhập
    """Return the average of a sequence of numbers."""
909 hoặc chỉ
    """Return the average of a sequence of numbers."""
910

Ngoài ra, khi một mô-đun đã được nhập, bạn có thể nhận trợ giúp về mô-đun đó bằng cách chuyển mô-đun đó tới hàm

    """Return the average of a sequence of numbers."""
911. Ví dụ

    """Return the average of a sequence of numbers."""
1

quy ước đặt tên

KindJavaScriptPythonconstant
    """Return the average of a sequence of numbers."""
912samevariable
    """Return the average of a sequence of numbers."""
913
    """Return the average of a sequence of numbers."""
914function
    """Return the average of a sequence of numbers."""
913
    """Return the average of a sequence of numbers."""
914class
    """Return the average of a sequence of numbers."""
917samemethod
    """Return the average of a sequence of numbers."""
913
    """Return the average of a sequence of numbers."""
914public instance properties
    """Return the average of a sequence of numbers."""
913
    """Return the average of a sequence of numbers."""
914private instance propertiesno convention
    """Return the average of a sequence of numbers."""
922

Mặc dù Python sử dụng quy ước đặt tên để xác định các hằng số [tất cả đều là chữ hoa], chúng vẫn có thể được sửa đổi. Và quy ước đặt tên cho các biến thể hiện riêng [bắt đầu bằng dấu gạch dưới] không ngăn cản truy cập từ bên ngoài lớp

Nhiều thư viện Python, bao gồm cả thư viện tiêu chuẩn, khác với quy ước đặt tên Python. Cụ thể, người ta thường tìm thấy các tên hàm và phương thức có nhiều từ nhưng tất cả đều là chữ thường và không có dấu phân cách giữa các từ. Ví dụ: thư viện chuẩn

    """Return the average of a sequence of numbers."""
923 định nghĩa các hàm
    """Return the average of a sequence of numbers."""
924 [tuân theo quy ước] và
    """Return the average of a sequence of numbers."""
925 [không]

Các loại tích hợp

TypeJavaScriptPythonBoolean
    """Return the average of a sequence of numbers."""
926,
    """Return the average of a sequence of numbers."""
927
    """Return the average of a sequence of numbers."""
928,
    """Return the average of a sequence of numbers."""
929numberdefault is double precision float,
    """Return the average of a sequence of numbers."""
930
    """Return the average of a sequence of numbers."""
931,
    """Return the average of a sequence of numbers."""
932,
    """Return the average of a sequence of numbers."""
933characteruse stringssamestring
    """Return the average of a sequence of numbers."""
934 or
    """Return the average of a sequence of numbers."""
935samemulti-line string
    """Return the average of a sequence of numbers."""
936
    """Return the average of a sequence of numbers."""
937 or
    """Return the average of a sequence of numbers."""
938string interpolation
    """Return the average of a sequence of numbers."""
939
    """Return the average of a sequence of numbers."""
940array
    """Return the average of a sequence of numbers."""
941 class, literal syntax
    """Return the average of a sequence of numbers."""
942
    """Return the average of a sequence of numbers."""
943 module in standard librarylistuse
    """Return the average of a sequence of numbers."""
941 class
    """Return the average of a sequence of numbers."""
945 class with literal syntax
    """Return the average of a sequence of numbers."""
942
mutable and typically homogeneoustupleno equivalent
    """Return the average of a sequence of numbers."""
947 class with literal syntax
    """Return the average of a sequence of numbers."""
948
immutable and typically heterogeneousrangeno equivalent
    """Return the average of a sequence of numbers."""
949 class
    """Return the average of a sequence of numbers."""
950key/value pairs
    """Return the average of a sequence of numbers."""
951 class with literal syntax
    """Return the average of a sequence of numbers."""
952 and
    """Return the average of a sequence of numbers."""
953
    """Return the average of a sequence of numbers."""
954 class with literal syntax
    """Return the average of a sequence of numbers."""
955set
    """Return the average of a sequence of numbers."""
956 class;
create with
    """Return the average of a sequence of numbers."""
957
    """Return the average of a sequence of numbers."""
958 class with literal syntax
    """Return the average of a sequence of numbers."""
959
or
    """Return the average of a sequence of numbers."""
960functionsee "Functions" section belowsee "Functions" section belowclass
    """Return the average of a sequence of numbers."""
961
see "Classes" section below
    """Return the average of a sequence of numbers."""
962
see "Classes" section belowno value
    """Return the average of a sequence of numbers."""
963 or
    """Return the average of a sequence of numbers."""
964
    """Return the average of a sequence of numbers."""
965

Mọi thứ đều là một đối tượng trong Python, ngay cả các giá trị nguyên thủy trong JavaScript như Booleans, số và chuỗi

Python có "chuỗi", trong khi JavaScript có mảng. Các loại trình tự bao gồm chuỗi, danh sách, bộ dữ liệu, phạm vi, bộ và bộ đệm. Danh sách là một chuỗi các giá trị có thể thay đổi thường có cùng loại. Một bộ là một chuỗi các giá trị bất biến có thể có các loại khác nhau. Một phạm vi là một dãy số bất biến thường được sử dụng để lặp

Khóa đối tượng JavaScript phải là chuỗi, nhưng khóa

    """Return the average of a sequence of numbers."""
953 có thể là bất kỳ loại giá trị nào. Các khóa dict Python có thể là bất kỳ loại bất biến nào

Các giá trị được coi là sai khi được sử dụng trong ngữ cảnh Boolean được liệt kê bên dưới

LanguageFalse ValuesPython
    """Return the average of a sequence of numbers."""
929,
    """Return the average of a sequence of numbers."""
968,
    """Return the average of a sequence of numbers."""
969,
    """Return the average of a sequence of numbers."""
970 [phức tạp], chuỗi trống, chuỗi trống và
    """Return the average of a sequence of numbers."""
965JavaScript
    """Return the average of a sequence of numbers."""
927,
    """Return the average of a sequence of numbers."""
968,
    """Return the average of a sequence of numbers."""
974, chuỗi trống,
    """Return the average of a sequence of numbers."""
963 và
    """Return the average of a sequence of numbers."""
964

Trong JavaScript, các bộ sưu tập trống có giá trị là

    """Return the average of a sequence of numbers."""
926. Chúng bao gồm mảng, đối tượng, phiên bản
    """Return the average of a sequence of numbers."""
953 và phiên bản
    """Return the average of a sequence of numbers."""
956

Trong Python, giá trị "không phải là số"

    """Return the average of a sequence of numbers."""
980 ước tính thành
    """Return the average of a sequence of numbers."""
926

mô-đun

Trong cả JavaScript và Python, các mô-đun được xác định bởi một tệp nguồn duy nhất. Tệp nguồn có thể nhập các mô-đun khác và những tệp đó có thể nhập nhiều mô-đun hơn

Trong cả JavaScript và Python, mỗi mô-đun chỉ được nhập một lần. Nếu mã của nó bị sửa đổi, tập lệnh phải được chạy lại để giải thích các thay đổi

Các mô-đun JavaScript được nhập bằng đường dẫn tệp tương đối chỉ được tìm kiếm ở vị trí đó. Các mô-đun JavaScript được nhập chỉ với một tên được tìm kiếm trong thư mục

    """Return the average of a sequence of numbers."""
982 của dự án và thư mục toàn cầu
    """Return the average of a sequence of numbers."""
982

Python tìm kiếm các mô-đun theo thứ tự sau

  • mô-đun tích hợp
  • thư mục liên quan đến tệp nhập [sử dụng tên mô-đun chấm]
  • các thư mục được liệt kê trong biến môi trường
        """Return the average of a sequence of numbers."""
    984
  • thư mục cài đặt cụ thể

Để xem các thư mục sẽ được tìm kiếm, hãy

    """Return the average of a sequence of numbers."""
985 và thực thi
    """Return the average of a sequence of numbers."""
986

Một số chủ đề liên quan đến mô-đun được mô tả trong bảng sau

TopicJavaScriptPythona mô-đun được xác định bởi. nội dung của filesameexport
    """Return the average of a sequence of numbers."""
987mọi thứ đều được xuất tự động;
chỉ ra các giá trị riêng tư bằng cách bắt đầu tên bằng dấu gạch dưới. . //www. npmjs. com/
    """Return the average of a sequence of numbers."""
992 or
    """Return the average of a sequence of numbers."""
993import specific values
    """Return the average of a sequence of numbers."""
994 or
    """Return the average of a sequence of numbers."""
995
    """Return the average of a sequence of numbers."""
996 or
    """Return the average of a sequence of numbers."""
997import default and named
    """Return the average of a sequence of numbers."""
998not supportedopen source catalog//www.npmjs.com/
https. //www. npmjs. com/https. //pypi. org/
https. //pypi. org/tool ​​to install
    """Return the average of a sequence of numbers."""
999 [đã cài đặt với Node. js]
    """Return the average of a sequence of numbers."""
3000 [được cài đặt bằng Python] hoặc
    """Return the average of a sequence of numbers."""
3001 [được cài đặt bằng Anaconda]

Trong bí danh Python thường được gán cho các gói thường được sử dụng. Cộng đồng đã bắt đầu sử dụng các bí danh được hiển thị trong phần nhập bên dưới

PackageRecommended Importcollections
in standard library
    """Return the average of a sequence of numbers."""
3002functools
in standard library
    """Return the average of a sequence of numbers."""
3003itertools
in standard library
    """Return the average of a sequence of numbers."""
3004matplotlib
    """Return the average of a sequence of numbers."""
3005NumPy
    """Return the average of a sequence of numbers."""
3006pandas
    """Return the average of a sequence of numbers."""
3007TensorFlow
    """Return the average of a sequence of numbers."""
3008

gói

Gói JavaScript

Các "gói" JavaScript được quản lý bằng công cụ

    """Return the average of a sequence of numbers."""
999, được cài đặt khi Node. js đã được cài đặt. Để cho phép mỗi dự án sử dụng các phiên bản gói khác nhau và giúp các nhà phát triển khác dễ dàng cài đặt cùng một bộ gói, hãy tạo tệp
    """Return the average of a sequence of numbers."""
3010 trong mỗi dự án bằng cách nhập
    """Return the average of a sequence of numbers."""
3011 và trả lời một số câu hỏi

Gói Python

Các "gói" Python được quản lý bằng công cụ

    """Return the average of a sequence of numbers."""
3000, được cài đặt khi Python được cài đặt. Tên này là từ viết tắt của "Pip Installs Packages. " Nó cài đặt các gói từ Chỉ mục gói Python [pypi]. Để nâng cấp phiên bản
    """Return the average of a sequence of numbers."""
3000 đang được sử dụng, hãy nhập
    """Return the average of a sequence of numbers."""
3014

Để cho phép mỗi dự án sử dụng các phiên bản gói khác nhau và giúp các nhà phát triển khác dễ dàng cài đặt cùng một bộ gói, hãy tạo một môi trường ảo. Có một số công cụ có thể được sử dụng để làm điều này. Các tùy chọn bao gồm Anaconda,

    """Return the average of a sequence of numbers."""
3015 [chủ yếu dành cho Python 2] và
    """Return the average of a sequence of numbers."""
3016 [một tập hợp con của
    """Return the average of a sequence of numbers."""
3015 dành cho Python 3]

Để tạo một môi trường ảo bằng cách sử dụng

    """Return the average of a sequence of numbers."""
3016, hãy nhập
    """Return the average of a sequence of numbers."""
3019 trong thư mục gốc của dự án. Điều này tạo ra một thư mục con
    """Return the average of a sequence of numbers."""
3020

Để kích hoạt môi trường ảo này, hãy chạy tập lệnh

    """Return the average of a sequence of numbers."""
3021 trong thư mục
    """Return the average of a sequence of numbers."""
3022. Trong Windows, nhập
    """Return the average of a sequence of numbers."""
3023. Trong môi trường UNIX, nhập
    """Return the average of a sequence of numbers."""
3024. [Khi sử dụng Fish shell, hãy thêm phần mở rộng
    """Return the average of a sequence of numbers."""
3025. ]

Kích hoạt môi trường ảo sẽ thay đổi môi trường để sử dụng các phiên bản của công cụ và thư viện có trong thư mục

    """Return the average of a sequence of numbers."""
3020 của dự án thay vì toàn cầu. Lưu ý rằng thay vì bao gồm một bản sao của một phiên bản cụ thể của trình thông dịch Python, một liên kết tượng trưng đến nó sẽ được tạo. Điều này cũng thay đổi dấu nhắc trình bao để cho biết thư mục môi trường đang được sử dụng, trong trường hợp này là
    """Return the average of a sequence of numbers."""
3020

Để hủy kích hoạt môi trường ảo và quay lại sử dụng các phiên bản toàn cầu của công cụ và thư viện, hãy nhập

    """Return the average of a sequence of numbers."""
3028

Lưu ý rằng môi trường ảo Python phải được kích hoạt để có hiệu lực. Điều này khác với Nút. js khi chỉ cần thay đổi thư mục của dự án sẽ khiến các phiên bản phụ thuộc của nó được sử dụng dựa trên nội dung của thư mục con

    """Return the average of a sequence of numbers."""
982

Để biết chi tiết về cách sử dụng Anaconda để quản lý môi trường ảo, xem tại đây

Sử dụng gói

OperationJavaScriptPythonchuẩn bị dự án
    """Return the average of a sequence of numbers."""
3030
    """Return the average of a sequence of numbers."""
3019
trong đó
    """Return the average of a sequence of numbers."""
3020 là tên thư mục được sử dụng theo quy ướccài đặt gói trên toàn cầu
    """Return the average of a sequence of numbers."""
3033không kích hoạt môi trường
    """Return the average of a sequence of numbers."""
3034cài đặt gói cục bộ
    """Return the average of a sequence of numbers."""
3035với môi trường được kích hoạt,
    """Return the average of a sequence of numbers."""
3034install a specific version of a package
    """Return the average of a sequence of numbers."""
3037
    """Return the average of a sequence of numbers."""
3038update to latest version of a specific package
    """Return the average of a sequence of numbers."""
3039
    """Return the average of a sequence of numbers."""
3040see where global packages are installed
    """Return the average of a sequence of numbers."""
3041with no environment activated,
    """Return the average of a sequence of numbers."""
3042see where local packages are installed
    """Return the average of a sequence of numbers."""
3043with an environment activated,
    """Return the average of a sequence of numbers."""
3042location of local packages
    """Return the average of a sequence of numbers."""
3045
    """Return the average of a sequence of numbers."""
3046see list of locally installed packages
    """Return the average of a sequence of numbers."""
3047 or
open
    """Return the average of a sequence of numbers."""
3010 and see
    """Return the average of a sequence of numbers."""
3049 and
    """Return the average of a sequence of numbers."""
3050with environment activated,
    """Return the average of a sequence of numbers."""
3051create list of project package versionsautomatically maintained in
    """Return the average of a sequence of numbers."""
3010
    """Return the average of a sequence of numbers."""
3053install project package versions
    """Return the average of a sequence of numbers."""
3054
    """Return the average of a sequence of numbers."""
3055

Gói dự án Python

Gói Project Python cho phép nhập mô-đun từ thư mục con. Đây là những thư mục con có tên là tên gói. Các thư mục con phải chứa tệp

    """Return the average of a sequence of numbers."""
3056 có thể để trống hoặc chứa mã khởi tạo cho gói

Thêm tệp

    """Return the average of a sequence of numbers."""
95 trong thư mục gói xác định mô-đun. Sau đó, trong các tệp
    """Return the average of a sequence of numbers."""
95 muốn sử dụng một mô-đun trong một gói [nằm trong thư mục tổ tiên], hãy sử dụng
    """Return the average of a sequence of numbers."""
3059. Để nhập những thứ cụ thể từ các mô-đun gói, hãy sử dụng
    """Return the average of a sequence of numbers."""
3060

Cấu trúc thư mục có thể sâu tùy thích với mỗi thư mục con chứa một tệp

    """Return the average of a sequence of numbers."""
3056. Khi có các gói lồng nhau, quá trình nhập trông giống như
    """Return the average of a sequence of numbers."""
3062 hoặc
    """Return the average of a sequence of numbers."""
3063

Để biết thêm thông tin về các gói Python, hãy xem Các gói trong Hướng dẫn Python

In/Ghi nhật ký

OperationJavaScriptPythonghi các giá trị được phân tách bằng dấu cách vào thiết bị xuất chuẩn_______13064
    """Return the average of a sequence of numbers."""
3065ghi các giá trị được phân tách bằng dấu cách vào thiết bị xuất chuẩn
    """Return the average of a sequence of numbers."""
3066
    """Return the average of a sequence of numbers."""
985
    """Return the average of a sequence of numbers."""
3068ghi vào thiết bị xuất chuẩn với phép nội suy
    """Return the average of a sequence of numbers."""
3069];
    """Return the average of a sequence of numbers."""
3070ghi vào thiết bị xuất chuẩn mà không có dòng mới trong Nút. js
____13071
    """Return the average of a sequence of numbers."""
3072
    """Return the average of a sequence of numbers."""
3073

Biến và phép gán

Các biến JavaScript phải được khai báo bằng từ khóa

    """Return the average of a sequence of numbers."""
3074 hoặc
    """Return the average of a sequence of numbers."""
3075. Các biến Python không được khai báo và được tạo khi một giá trị được gán cho chúng

Các phép gán biến JavaScript có thể xuất hiện bên trong các biểu thức, chẳng hạn như điều kiện vòng lặp hoặc

    """Return the average of a sequence of numbers."""
3076, điều mà nhiều nhà phát triển cảm thấy khó hiểu. Điều này không được phép trong Python cho đến phiên bản 3. 8, bổ sung "toán tử hải mã" để gán giá trị cho các biến bên trong các biểu thức lớn hơn. Ví dụ

    """Return the average of a sequence of numbers."""
9

Công cụ linting pylint Python coi các biến cấp mô-đun là hằng số. Nó sẽ đưa ra các cảnh báo nếu các chức năng sửa đổi giá trị của chúng. Để tránh điều này, hãy liệt kê tất cả các biến như vậy sẽ được sửa đổi sau từ khóa

    """Return the average of a sequence of numbers."""
3077 bên trong các hàm sửa đổi chúng. Từ khóa liên quan
    """Return the average of a sequence of numbers."""
3078 cho phép các hàm truy cập các biến trong phạm vi tổ tiên không phải là toàn cầu

TopicJavaScriptPythonkhai báo hằng số
    """Return the average of a sequence of numbers."""
3079
    """Return the average of a sequence of numbers."""
3080khai báo biến
    """Return the average of a sequence of numbers."""
3081
    """Return the average of a sequence of numbers."""
3082lấy loại giá trị trong biến
    """Return the average of a sequence of numbers."""
3083 và
    """Return the average of a sequence of numbers."""
3084
    """Return the average of a sequence of numbers."""
3085phân công nhiều
    """Return the average of a sequence of numbers."""
3086
    """Return the average of a sequence of numbers."""
3087trình tự cấu trúc
    """Return the average of a sequence of numbers."""
3088
# of variables can differ from # of elements
    """Return the average of a sequence of numbers."""
3089
# of variables must match # of elementsdestructure object
    """Return the average of a sequence of numbers."""
3090not supportedun-declare variable
    """Return the average of a sequence of numbers."""
3091 - just removes value
    """Return the average of a sequence of numbers."""
3092addition
    """Return the average of a sequence of numbers."""
3093samesubtraction
    """Return the average of a sequence of numbers."""
3094samemultiplication
    """Return the average of a sequence of numbers."""
3095samedivision
    """Return the average of a sequence of numbers."""
3096sameexponentiation
    """Return the average of a sequence of numbers."""
3097samemod [remainder]
    """Return the average of a sequence of numbers."""
3098samelogical and
    """Return the average of a sequence of numbers."""
3099not supportedlogical or
    """Return the average of a sequence of numbers."""
3100not supportedlogical xor
    """Return the average of a sequence of numbers."""
3101not supportedbitwise

Phá hủy JavaScript có thể nắm bắt nhiều giá trị trong một biến mảng. Ví dụ

    """Return the average of a sequence of numbers."""
30

Biến

    """Return the average of a sequence of numbers."""
3109 ở trên được đặt thành một mảng. "Toán tử còn lại"
    """Return the average of a sequence of numbers."""
3110 phải xuất hiện trước biến cuối cùng

Python gọi thao tác này là "giải nén" và nó thậm chí còn có nhiều khả năng hơn

    """Return the average of a sequence of numbers."""
31

Biến

    """Return the average of a sequence of numbers."""
3109 ở trên được đặt thành danh sách

so sánh

TopicJavaScriptPythonequal
    """Return the average of a sequence of numbers."""
3112 [with coercion] or
    """Return the average of a sequence of numbers."""
3113 [without]
    """Return the average of a sequence of numbers."""
3112 [without coercion]not equal
    """Return the average of a sequence of numbers."""
3115 [with coercion] or
    """Return the average of a sequence of numbers."""
3116 [without]
    """Return the average of a sequence of numbers."""
3115 [without coercion]same object
    """Return the average of a sequence of numbers."""
3113
    """Return the average of a sequence of numbers."""
3119different object
    """Return the average of a sequence of numbers."""
3116
    """Return the average of a sequence of numbers."""
3121less than
    """Return the average of a sequence of numbers."""
3122sameless than or equal
    """Return the average of a sequence of numbers."""
3123samegreater than
    """Return the average of a sequence of numbers."""
3124samegreater than or equal
    """Return the average of a sequence of numbers."""
3125same

So sánh Python có thể được xâu chuỗi, nhưng so sánh JavaScript thì không thể. Ví dụ: để xác định xem giá trị của một biến có nằm trong khoảng từ 10 đến 20 hay không

  • trong JavaScript,
        """Return the average of a sequence of numbers."""
    3126
  • bằng Python,
        """Return the average of a sequence of numbers."""
    3127

khối mã

Khối mã JavaScript được bao quanh bởi dấu ngoặc nhọn [______23128]. Các câu lệnh dài có thể được chia thành nhiều dòng mà không bao gồm bất kỳ ký tự đặc biệt nào ở cuối các dòng ban đầu

Python sử dụng khoảng trắng ở đầu [thụt đầu dòng] để xác định dòng nào là một phần của cùng một khối. Tất cả các dòng liên tiếp có cùng thụt lề, bỏ qua các dòng trống, được coi là trong cùng một khối

Với mục đích xác định này, các ký tự tab được thay thế bằng khoảng trắng sao cho số lượng khoảng trắng là bội số của tám. Điều này rất quan trọng khi sử dụng hỗn hợp các tab và dấu cách, điều này không được khuyến nghị

Hướng dẫn kiểu Python khuyên bạn nên sử dụng bội số của bốn khoảng trắng để thụt đầu dòng và không sử dụng các ký tự tab. Câu lệnh dài có thể được chia thành nhiều dòng bằng cách thêm dấu gạch chéo ngược [

    """Return the average of a sequence of numbers."""
3129] vào cuối tất cả trừ dòng cuối cùng khi không rõ câu lệnh đang tiếp tục

Logic có điều kiện

Trong cú pháp JavaScript bên dưới,

    """Return the average of a sequence of numbers."""
3130 là viết tắt của "câu lệnh hoặc khối. " Nó có thể là một câu lệnh đơn hoặc một tập hợp các câu lệnh được bao quanh bởi dấu ngoặc nhọn

Câu lệnh JavaScript

    """Return the average of a sequence of numbers."""
3076 có thể chứa bất kỳ số lượng phần
    """Return the average of a sequence of numbers."""
3132 nào

Một câu lệnh

    """Return the average of a sequence of numbers."""
3076 của Python có thể chứa bất kỳ số lượng phần
    """Return the average of a sequence of numbers."""
3134 nào. Các khối Python phải bắt đầu trên một dòng mới và được thụt vào

Chủ đềJavaScriptPythonif
    """Return the average of a sequence of numbers."""
3135
    """Return the average of a sequence of numbers."""
3136if/else____23137
    """Return the average of a sequence of numbers."""
3138if/else if/else
    """Return the average of a sequence of numbers."""
3139
    """Return the average of a sequence of numbers."""
3140switch
    """Return the average of a sequence of numbers."""
3141không được hỗ trợternary
    """Return the average of a sequence of numbers."""
3142
    """Return the average of a sequence of numbers."""
3143

Đây là một ví dụ về việc sử dụng câu lệnh ternary Python

    """Return the average of a sequence of numbers."""
77

lặp lại

Như chúng ta sẽ thấy trong phần Bộ sưu tập khóa/giá trị sau này, JavaScript có thể lưu trữ các cặp khóa/giá trị trong các đối tượng đơn giản hoặc trong các phiên bản của lớp

    """Return the average of a sequence of numbers."""
953. Python sử dụng "từ điển" [hoặc dicts] để lưu trữ các cặp khóa/giá trị

TopicJavaScriptPythonclassic for loop
    """Return the average of a sequence of numbers."""
3145
    """Return the average of a sequence of numbers."""
3146over collection
    """Return the average of a sequence of numbers."""
3147
    """Return the average of a sequence of numbers."""
3148over object/dict keys
    """Return the average of a sequence of numbers."""
3149
or
    """Return the average of a sequence of numbers."""
3150
    """Return the average of a sequence of numbers."""
3151over object/dict values
    """Return the average of a sequence of numbers."""
3152
    """Return the average of a sequence of numbers."""
3153over object/dict keys and values
    """Return the average of a sequence of numbers."""
3154
    """Return the average of a sequence of numbers."""
3155top-tested
    """Return the average of a sequence of numbers."""
3156
    """Return the average of a sequence of numbers."""
3157bottom-tested
    """Return the average of a sequence of numbers."""
3158
    """Return the average of a sequence of numbers."""
3159break out of closest loop
    """Return the average of a sequence of numbers."""
3160samecontinue to next iteration
    """Return the average of a sequence of numbers."""
3161same

Chức năng

Trong JavaScript, các hàm có thể được định nghĩa theo hai cách

    """Return the average of a sequence of numbers."""
96

Nếu một hàm ẩn danh có chính xác một đối số được đặt tên, thì dấu ngoặc đơn xung quanh nó là tùy chọn. Nếu một hàm ẩn danh chỉ trả về giá trị của một biểu thức, thì các dấu ngoặc nhọn xung quanh phần thân và từ khóa

    """Return the average of a sequence of numbers."""
3162 là tùy chọn

Trong Python, hàm cũng có thể được định nghĩa theo hai cách

    """Return the average of a sequence of numbers."""
98

Các hàm lambda trong Python chỉ có thể trả về giá trị của một biểu thức duy nhất. Chúng không thể chứa các câu lệnh bổ sung

Các hàm có tên Python có thể có một chuỗi tài liệu làm dòng đầu tiên của chúng. Điều này được sử dụng bởi các công cụ tạo tài liệu từ mã. Nó thường được phân định bằng ba dấu ngoặc kép

Để biết hướng dẫn về nội dung của chuỗi tài liệu, hãy xem Chuỗi tài liệu" trong Hướng dẫn Python và chuỗi tài liệu PEP-8

Một chuỗi tài liệu tốt cho một chức năng trông giống như

    """Return the average of a sequence of numbers."""

JavaScript không hỗ trợ nạp chồng hàm khi cùng một tên hàm có thể được xác định nhiều lần với các số và/hoặc loại đối số khác nhau

Python hỗ trợ điều này theo nghĩa hạn chế bằng cách sử dụng trình trang trí

    """Return the average of a sequence of numbers."""
3163 được xác định trong thư viện chuẩn
    """Return the average of a sequence of numbers."""
923

TopicJavaScriptPythondefine named
    """Return the average of a sequence of numbers."""
3165
    """Return the average of a sequence of numbers."""
3166define anonymous
    """Return the average of a sequence of numbers."""
3167
    """Return the average of a sequence of numbers."""
3168define anonymous w/ single parameter
    """Return the average of a sequence of numbers."""
3169same as abovedefine anonymous w/ single expression
    """Return the average of a sequence of numbers."""
3170same as abovespecify default parameter values
    """Return the average of a sequence of numbers."""
3171
    """Return the average of a sequence of numbers."""
3172gather variable number of arguments
    """Return the average of a sequence of numbers."""
3173
    """Return the average of a sequence of numbers."""
3109 is set to an
    """Return the average of a sequence of numbers."""
941
    """Return the average of a sequence of numbers."""
3176
    """Return the average of a sequence of numbers."""
3109 is set to a tuplegather arguments as key/value pairsnot supported
    """Return the average of a sequence of numbers."""
3178
call with
    """Return the average of a sequence of numbers."""
3179
or
    """Return the average of a sequence of numbers."""
3180use named/keyword arguments
    """Return the average of a sequence of numbers."""
3181
pass an "options" objectsame as above;
any parameter can be specified by name;
important feature!
gọi với ____23179trả lại một giá trị
    """Return the average of a sequence of numbers."""
3183
    """Return the average of a sequence of numbers."""
3184giá trị trả về mặc định khi không có ____23162
    """Return the average of a sequence of numbers."""
963
    """Return the average of a sequence of numbers."""
965gọi
    """Return the average of a sequence of numbers."""
3188cùng số lượng đối số được yêu cầu
    """Return the average of a sequence of numbers."""
3189
    """Return the average of a sequence of numbers."""
3190
    """Return the average of a sequence of numbers."""
3191truyền ít đối số hơn
các tham số vị trí còn lại được gán cho
    """Return the average of a sequence of numbers."""
9
positional parameters with no gatheringall arguments are available in
    """Return the average of a sequence of numbers."""
3193 array-like objectresults in an errorget name
    """Return the average of a sequence of numbers."""
3194
    """Return the average of a sequence of numbers."""
3195get implementation code
    """Return the average of a sequence of numbers."""
3196
    """Return the average of a sequence of numbers."""
3197
    """Return the average of a sequence of numbers."""
3198create partial
    """Return the average of a sequence of numbers."""
3199
    """Return the average of a sequence of numbers."""
7700 can be
    """Return the average of a sequence of numbers."""
964
    """Return the average of a sequence of numbers."""
7702
    """Return the average of a sequence of numbers."""
7703call
    """Return the average of a sequence of numbers."""
7704
    """Return the average of a sequence of numbers."""
7700 can be
    """Return the average of a sequence of numbers."""
964
    """Return the average of a sequence of numbers."""
7707apply
    """Return the average of a sequence of numbers."""
7708
    """Return the average of a sequence of numbers."""
7700 can be
    """Return the average of a sequence of numbers."""
964
    """Return the average of a sequence of numbers."""
7711spread array to positional arguments
    """Return the average of a sequence of numbers."""
7712
    """Return the average of a sequence of numbers."""
7713spread object to keyword argumentsnot supported
    """Return the average of a sequence of numbers."""
7714

Trong Python

  • Các tham số hàm có giá trị mặc định phải tuân theo các tham số không có giá trị mặc định
  • Các tham số chức năng được liệt kê sau tham số bắt đầu bằng
        """Return the average of a sequence of numbers."""
    7715 phải được chỉ định theo tên
  • Đối số từ khóa thường được viết tắt là "kwargs" trong tài liệu
  • Chỉ có thể sử dụng hàm
        """Return the average of a sequence of numbers."""
    7716 [hiển thị trong bảng trên] trên các hàm. Đối với các phương thức của một lớp, hãy sử dụng
        """Return the average of a sequence of numbers."""
    924
  • Độ sâu đệ quy tối đa mặc định là 1000. Để thay đổi điều này, hãy gọi
        """Return the average of a sequence of numbers."""
    7718

Thực hiện sau hoặc trong khoảng thời gian

Trong JavaScript, hàm

    """Return the average of a sequence of numbers."""
7719 đăng ký một hàm sẽ được gọi sau một vài mili giây trong tương lai. Ví dụ

function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];

Trong Python, điều tương tự có thể được thực hiện như sau

    """Return the average of a sequence of numbers."""
9

Trong JavaScript, hàm

    """Return the average of a sequence of numbers."""
7720 đăng ký một hàm được gọi lặp lại sau mỗi vài mili giây. Ví dụ

    """Return the average of a sequence of numbers."""
90

Trong Python không có tương đương đơn giản. Nhưng điều tương tự cũng có thể được thực hiện nếu chúng ta định nghĩa và sử dụng một lớp như sau

    """Return the average of a sequence of numbers."""
91

Chức năng không đồng bộ

Trong Trăn 3. 4+, các chức năng không đồng bộ được hỗ trợ bởi thư viện

    """Return the average of a sequence of numbers."""
92

TopicJavaScriptPythondefine hàm có tên không đồng bộ
    """Return the average of a sequence of numbers."""
7722
    """Return the average of a sequence of numbers."""
7723define hàm ẩn danh không đồng bộ
    """Return the average of a sequence of numbers."""
7724không được hỗ trợ cuộc gọi không đồng bộ với
    """Return the average of a sequence of numbers."""
7725
    """Return the average of a sequence of numbers."""
7726
thường được bao bọc trong một khối
    """Return the average of a sequence of numbers."""
7727
    """Return the average of a sequence of numbers."""
7728cuộc gọi không đồng bộ với
    """Return the average of a sequence of numbers."""
7729 và
    """Return the average of a sequence of numbers."""
7730
    """Return the average of a sequence of numbers."""
7731
  
    """Return the average of a sequence of numbers."""
7732
  
    """Return the average of a sequence of numbers."""
7733n/a

Trong JavaScript, các hàm async trả về một đối tượng

    """Return the average of a sequence of numbers."""
7734

Dưới đây là một ví dụ về các tác vụ đang chạy cần một lượng thời gian mô phỏng để hoàn thành. Cái đầu tiên mất 3 giây, cái thứ hai mất 2 giây và cái thứ ba mất 1. Mỗi tác vụ xuất thông báo "bắt đầu" ngay lập tức. Các thông báo "kết thúc" xuất hiện theo thứ tự ngược lại do thời lượng ngủ khác nhau của chúng

    """Return the average of a sequence of numbers."""
92

đầu ra là

    """Return the average of a sequence of numbers."""
93

Trong Trăn 3. 4, thư viện asyncio đã được thêm vào. Nó có thể được sử dụng để tạo các coroutine tương tự như JavaScript Promises. Ngôn ngữ Python không cung cấp tương đương với lời hứa JavaScript, nhưng các thư viện thì có

Đây là một triển khai của ví dụ JavaScript trước đó trong Python tạo ra cùng một đầu ra

    """Return the average of a sequence of numbers."""
94

Các lớp học

TopicJavaScriptPythondefine
    """Return the average of a sequence of numbers."""
7735
    """Return the average of a sequence of numbers."""
7736inheritance
    """Return the average of a sequence of numbers."""
7737
chỉ hỗ trợ một kế thừa duy nhất
    """Return the average of a sequence of numbers."""
7738
hỗ trợ nhiều kế thừaconstructor
    """Return the average of a sequence of numbers."""
7739
    """Return the average of a sequence of numbers."""
7740khởi tạo [tạo phiên bản]
    """Return the average of a sequence of numbers."""
7741
    """Return the average of a sequence of numbers."""
7742khai báo thuộc tính cá thể không được khai báo;
    """Return the average of a sequence of numbers."""
7756class/static method call
    """Return the average of a sequence of numbers."""
7757
    """Return the average of a sequence of numbers."""
7757 or
    """Return the average of a sequence of numbers."""
7759

Ngoài trình trang trí

    """Return the average of a sequence of numbers."""
7755, Python cũng hỗ trợ trình trang trí
    """Return the average of a sequence of numbers."""
7761. Sự khác biệt là các phương thức được định nghĩa với cái sau được truyền vào lớp làm đối số đầu tiên

Đây là một ví dụ về một lớp JavaScript

    """Return the average of a sequence of numbers."""
95

đầu ra là

    """Return the average of a sequence of numbers."""
96

Đây là cùng một lớp được triển khai trong Python

    """Return the average of a sequence of numbers."""
97

Đầu ra giống như trên

Lưu ý cách trong Python, tham số đầu tiên trong tất cả các phương thức mẫu phải là

    """Return the average of a sequence of numbers."""
7744

Đây là một hàm JavaScript nhận một lớp và in hệ thống phân cấp thừa kế của nó

    """Return the average of a sequence of numbers."""
98

Đây là một hàm Python để làm điều tương tự

    """Return the average of a sequence of numbers."""
99

Chức năng tích hợp sẵn

JavaScript cung cấp một số lượng nhỏ [9] hàm dựng sẵn. Python cung cấp nhiều hơn nữa [68]. Các bảng dưới đây tóm tắt những

Thường thì một trong các ngôn ngữ không có chức năng tương đương với ngôn ngữ kia, vì vậy ngôn ngữ thay thế gần nhất được hiển thị thay thế

Mô tả bên dưới bắt đầu bằng "xác định nếu" có nghĩa là giá trị Boolean được trả về

Hàm tích hợp PythonDescriptionJavaScript gần nhất tương đương
    """Return the average of a sequence of numbers."""
7763trả về giá trị tuyệt đối
    """Return the average of a sequence of numbers."""
7764
    """Return the average of a sequence of numbers."""
7765xác định xem tất cả các phần tử có phải là ____2928
trong ngữ cảnh Boolean
    """Return the average of a sequence of numbers."""
7767
    """Return the average of a sequence of numbers."""
7768xác định xem có phần tử nào là ____2928
trong ngữ cảnh Boolean
    """Return the average of a sequence of numbers."""
7770_______27771like
drops into debugger
    """Return the average of a sequence of numbers."""
7777
    """Return the average of a sequence of numbers."""
7778returns a new array of bytesuse
    """Return the average of a sequence of numbers."""
7779 with typed array classes
    """Return the average of a sequence of numbers."""
7780returns a
    """Return the average of a sequence of numbers."""
7781 objectuse
    """Return the average of a sequence of numbers."""
7779 with typed array classes
    """Return the average of a sequence of numbers."""
7783determines if x is callable [a function]
    """Return the average of a sequence of numbers."""
7784
    """Return the average of a sequence of numbers."""
7785returns string representation
of a Unicode code point
    """Return the average of a sequence of numbers."""
7786
    """Return the average of a sequence of numbers."""
7787compiles source into a code/AST object
which can be passed to
    """Return the average of a sequence of numbers."""
7788 or
    """Return the average of a sequence of numbers."""
7789not supported
    """Return the average of a sequence of numbers."""
7790creates a complex number
from real and imaginary partsnot supported
    """Return the average of a sequence of numbers."""
7791deletes an attribute from an object
    """Return the average of a sequence of numbers."""
7792
    """Return the average of a sequence of numbers."""
7793creates a dictionary
    """Return the average of a sequence of numbers."""
7794 or
    """Return the average of a sequence of numbers."""
7795
    """Return the average of a sequence of numbers."""
7796returns a list of defined names
in current scope or an object
    """Return the average of a sequence of numbers."""
7797
    """Return the average of a sequence of numbers."""
7798return tuple of quotient and remainder
of
    """Return the average of a sequence of numbers."""
7799 divided by
    """Return the average of a sequence of numbers."""
9600
    """Return the average of a sequence of numbers."""
9601 and
    """Return the average of a sequence of numbers."""
9602
    """Return the average of a sequence of numbers."""
9603return list of tuples each containing
an index and value from an iterable
    """Return the average of a sequence of numbers."""
9604
    """Return the average of a sequence of numbers."""
9605evaluates a single code expression
    """Return the average of a sequence of numbers."""
9605
    """Return the average of a sequence of numbers."""
9607execute any number of lines of code
    """Return the average of a sequence of numbers."""
9605
    """Return the average of a sequence of numbers."""
9609returns iterator over values in iterable
where predicate function returns true
    """Return the average of a sequence of numbers."""
9610
    """Return the average of a sequence of numbers."""
9611returns floating point number
created from a number or string
    """Return the average of a sequence of numbers."""
9612
    """Return the average of a sequence of numbers."""
9613returns string created by formatting a value
using a format stringuse template literals
    """Return the average of a sequence of numbers."""
9614returns
    """Return the average of a sequence of numbers."""
9615 [immutable set] object
created from iterable
    """Return the average of a sequence of numbers."""
9616
    """Return the average of a sequence of numbers."""
9617returns attribute value
    """Return the average of a sequence of numbers."""
9618
    """Return the average of a sequence of numbers."""
9619returns dictionary containing the
current global symbol tablenot supported
    """Return the average of a sequence of numbers."""
9620determines if object has a given attribute
    """Return the average of a sequence of numbers."""
9621
    """Return the average of a sequence of numbers."""
9622returns hash value of objectnot supported
    """Return the average of a sequence of numbers."""
9623invoke the built-in Python help system,
typically in the REPLnot supported
    """Return the average of a sequence of numbers."""
9624converts integer to hex
    """Return the average of a sequence of numbers."""
9625
    """Return the average of a sequence of numbers."""
9626returns identity of an objectnot supported
    """Return the average of a sequence of numbers."""
9627read from stdout with optional promptuse the Node
    """Return the average of a sequence of numbers."""
9628 module
    """Return the average of a sequence of numbers."""
9629 method
    """Return the average of a sequence of numbers."""
9630returns an integer created from
a number or string
    """Return the average of a sequence of numbers."""
9631
    """Return the average of a sequence of numbers."""
9632determines if an object is
an instance of a given class
    """Return the average of a sequence of numbers."""
9633
    """Return the average of a sequence of numbers."""
9634determines if
    """Return the average of a sequence of numbers."""
9635 is
a subclass of
    """Return the average of a sequence of numbers."""
9636
    """Return the average of a sequence of numbers."""
9637
    """Return the average of a sequence of numbers."""
9638returns an iterator over the elements
of a collection [see
    """Return the average of a sequence of numbers."""
9639]
    """Return the average of a sequence of numbers."""
9640
    """Return the average of a sequence of numbers."""
9641returns number of items in a collection
or characters in a string
    """Return the average of a sequence of numbers."""
9642
    """Return the average of a sequence of numbers."""
9643constructs a list from an iterable
    """Return the average of a sequence of numbers."""
9644
    """Return the average of a sequence of numbers."""
9645returns dictionary containing the
current local symbol tablenot supported
    """Return the average of a sequence of numbers."""
9646returns iterator over values returned by
calling function on each iterable element
    """Return the average of a sequence of numbers."""
9647
    """Return the average of a sequence of numbers."""
9648 or
    """Return the average of a sequence of numbers."""
9649returns largest value of arguments
or in an iterable
    """Return the average of a sequence of numbers."""
9650 or
    """Return the average of a sequence of numbers."""
9651
    """Return the average of a sequence of numbers."""
9652returns a
    """Return the average of a sequence of numbers."""
9653 for an object
that support the buffer protocolnot supported
    """Return the average of a sequence of numbers."""
9654 or
    """Return the average of a sequence of numbers."""
9655returns largest value of arguments
or in an iterable
    """Return the average of a sequence of numbers."""
9656 or
    """Return the average of a sequence of numbers."""
9657
    """Return the average of a sequence of numbers."""
9658get next item from an iterator
    """Return the average of a sequence of numbers."""
9659
    """Return the average of a sequence of numbers."""
9660create an empty, featureless object;
can't add properties; Why is this useful?
    """Return the average of a sequence of numbers."""
7794 is similar, but CAN add properties
    """Return the average of a sequence of numbers."""
9662converts integer to octal
    """Return the average of a sequence of numbers."""
9663
    """Return the average of a sequence of numbers."""
9664opens a file for
reading, writing, or appendingsee the Node fs module
    """Return the average of a sequence of numbers."""
9665returns the Unicode code point
for a Unicode character
    """Return the average of a sequence of numbers."""
9666
    """Return the average of a sequence of numbers."""
9667 or
    """Return the average of a sequence of numbers."""
9668return base raised to exp power
    """Return the average of a sequence of numbers."""
9667 or
    """Return the average of a sequence of numbers."""
9670
    """Return the average of a sequence of numbers."""
9671print space-separated expression values
    """Return the average of a sequence of numbers."""
9672
    """Return the average of a sequence of numbers."""
9673returns a property attribute that encapsulates
get, set and delete functionsnot supported
    """Return the average of a sequence of numbers."""
9674 or
    """Return the average of a sequence of numbers."""
950returns a
    """Return the average of a sequence of numbers."""
949 object
which is an immutable sequencenot supported
    """Return the average of a sequence of numbers."""
9677returns a string representation
of an object for developers
    """Return the average of a sequence of numbers."""
9678
    """Return the average of a sequence of numbers."""
9679returns an iterator for iterating
over a sequence in reverse ordernot supported
    """Return the average of a sequence of numbers."""
9680
returns
    """Return the average of a sequence of numbers."""
932 or
    """Return the average of a sequence of numbers."""
931returns a number rounded to
some number of decimal points
    """Return the average of a sequence of numbers."""
9683
returns string
    """Return the average of a sequence of numbers."""
9684creates a
    """Return the average of a sequence of numbers."""
958 object
    """Return the average of a sequence of numbers."""
957
    """Return the average of a sequence of numbers."""
9687sets an attribute of an object
    """Return the average of a sequence of numbers."""
9688
    """Return the average of a sequence of numbers."""
9689 or
    """Return the average of a sequence of numbers."""
9690returns a
    """Return the average of a sequence of numbers."""
9691 object that
describes a set of indexes;
used to retrieve data at those indexesnot supported
    """Return the average of a sequence of numbers."""
9692returns a sorted version
of an iterable as a
    """Return the average of a sequence of numbers."""
945
    """Return the average of a sequence of numbers."""
9694
sorts in place
    """Return the average of a sequence of numbers."""
9695returns a human-readable string
representation of an object
    """Return the average of a sequence of numbers."""
9678
    """Return the average of a sequence of numbers."""
9697returns the sum of
numbers in an iterable
    """Return the average of a sequence of numbers."""
9698
    """Return the average of a sequence of numbers."""
9699returns a proxy object for
calling superclass methods
    """Return the average of a sequence of numbers."""
9800 keyword
    """Return the average of a sequence of numbers."""
9801creates a tuple, optionally
populated from an iterablenot supported
    """Return the average of a sequence of numbers."""
9802 returns class objectreturns the type of a value
    """Return the average of a sequence of numbers."""
3083 returns string
    """Return the average of a sequence of numbers."""
9804 is constructor function
    """Return the average of a sequence of numbers."""
9805returns a
    """Return the average of a sequence of numbers."""
954 view of
the attributes in an objectnot supported
    """Return the average of a sequence of numbers."""
9807returns an iterator that aggregates
elements from multiple iterablesnot built-in;
can use Lodash
    """Return the average of a sequence of numbers."""
9808 functionJavaScript built-in functionDescriptionClosest Python equivalent
    """Return the average of a sequence of numbers."""
9809decodes a URL [opposite of
    """Return the average of a sequence of numbers."""
9810]
    """Return the average of a sequence of numbers."""
9811
    """Return the average of a sequence of numbers."""
9812decodes a component of a URI
[opposite of
    """Return the average of a sequence of numbers."""
9813]
    """Return the average of a sequence of numbers."""
9811
    """Return the average of a sequence of numbers."""
9815encodes a URI, replacing certain characters
[not /, #, ?, =, and others]
    """Return the average of a sequence of numbers."""
9816
    """Return the average of a sequence of numbers."""
9817encodes a component of a URI,
replacing certain characters
    """Return the average of a sequence of numbers."""
9818
    """Return the average of a sequence of numbers."""
9605execute any number of lines of code
    """Return the average of a sequence of numbers."""
9607
    """Return the average of a sequence of numbers."""
9821determines if x is a finite number
    """Return the average of a sequence of numbers."""
9822
    """Return the average of a sequence of numbers."""
9823determines of x in the "not a number" value
    """Return the average of a sequence of numbers."""
9824
    """Return the average of a sequence of numbers."""
9612returns floating point number
created from a number or string
    """Return the average of a sequence of numbers."""
9611
    """Return the average of a sequence of numbers."""
9631returns an integer created from
a number or string
    """Return the average of a sequence of numbers."""
9630

Các hàm JavaScript

    """Return the average of a sequence of numbers."""
9829 và
    """Return the average of a sequence of numbers."""
9830 có thể xử lý các chuỗi chứa các ký tự bổ sung sau các ký tự trong số. Ví dụ:
    """Return the average of a sequence of numbers."""
9831 trả về số
    """Return the average of a sequence of numbers."""
9832. Các hàm
    """Return the average of a sequence of numbers."""
932 và
    """Return the average of a sequence of numbers."""
931 của Python không hỗ trợ điều này

Hoạt động Boolean

Hoạt độngJavaScriptPythonand
    """Return the average of a sequence of numbers."""
9835
    """Return the average of a sequence of numbers."""
9836or
    """Return the average of a sequence of numbers."""
9837
    """Return the average of a sequence of numbers."""
9838not
    """Return the average of a sequence of numbers."""
9839
    """Return the average of a sequence of numbers."""
9840bitwise and
    """Return the average of a sequence of numbers."""
9841samebitwise or
    """Return the average of a sequence of numbers."""
9842samebitwise not
    """Return the average of a sequence of numbers."""
9843samebitwise xor
    """Return the average of a sequence of numbers."""
9844same

Phép toán số

OperationJavaScriptPythonbasic
    """Return the average of a sequence of numbers."""
9845,
    """Return the average of a sequence of numbers."""
9846,
    """Return the average of a sequence of numbers."""
7715,
    """Return the average of a sequence of numbers."""
9848sameexponentiation
    """Return the average of a sequence of numbers."""
9849sameincrement
    """Return the average of a sequence of numbers."""
9850 [pre] or
    """Return the average of a sequence of numbers."""
9851 [post]
    """Return the average of a sequence of numbers."""
9852decrement
    """Return the average of a sequence of numbers."""
9853 [pre] or
    """Return the average of a sequence of numbers."""
9854 [post]
    """Return the average of a sequence of numbers."""
9855mod [remainder]
    """Return the average of a sequence of numbers."""
9856sameconvert to string
    """Return the average of a sequence of numbers."""
9857
    """Return the average of a sequence of numbers."""
9858convert from string to integer
    """Return the average of a sequence of numbers."""
9859 or
    """Return the average of a sequence of numbers."""
9860
    """Return the average of a sequence of numbers."""
9861convert from string to float
    """Return the average of a sequence of numbers."""
9859 or
    """Return the average of a sequence of numbers."""
9863
    """Return the average of a sequence of numbers."""
9864convert to string with fixed decimals [ex. 2]
    """Return the average of a sequence of numbers."""
9865
    """Return the average of a sequence of numbers."""
9866chuyển đổi thành hex
    """Return the average of a sequence of numbers."""
9625
    """Return the average of a sequence of numbers."""
9624chuyển đổi từ hex
    """Return the average of a sequence of numbers."""
9869
    """Return the average of a sequence of numbers."""
9870constantssee
    """Return the average of a sequence of numbers."""
9871 và
    """Return the average of a sequence of numbers."""
9872 các đối tượng toàn cầuxem các chức năng mô-đun
    """Return the average of a sequence of numbers."""
9873xem
    """Return the average of a sequence of numbers."""
9871 và các đối tượng toàn cầu
    """Return the average of a sequence of numbers."""
9872xem mô-đun
    """Return the average of a sequence of numbers."""
9873

phép toán

Các hằng số và hàm ít được sử dụng hơn được bỏ qua trong bảng bên dưới

Để sử dụng các hàm Python,

    """Return the average of a sequence of numbers."""
9877

Hoạt độngJavaScriptPythonagiá trị tuyệt đối
    """Return the average of a sequence of numbers."""
7764
    """Return the average of a sequence of numbers."""
9879arc cosin
    """Return the average of a sequence of numbers."""
9880
    """Return the average of a sequence of numbers."""
9881arc sin
    """Return the average of a sequence of numbers."""
9882
    """Return the average of a sequence of numbers."""
9883arc tangent
    """Return the average of a sequence of numbers."""
9884
    """Return the average of a sequence of numbers."""
9885ceiling
    """Return the average of a sequence of numbers."""
9886
    """Return the average of a sequence of numbers."""
9887closenot built-in
    """Return the average of a sequence of numbers."""
9888sự kết hợp của k mục từ nnot tích hợp sẵn;

JavaScript có thể sử dụng các hàm sau để tính toán một số giá trị được đánh dấu là "không tích hợp sẵn" trong bảng trên

    """Return the average of a sequence of numbers."""
300

Mô-đun Python

    """Return the average of a sequence of numbers."""
53 cũng cung cấp

  •     """Return the average of a sequence of numbers."""
    54 trả về một phần tử ngẫu nhiên từ một chuỗi
  •     """Return the average of a sequence of numbers."""
    55 xáo trộn một chuỗi tại chỗ
  • và hơn thế nữa

Hoạt động chuỗi

OperationJavaScriptPythonliteral single line
    """Return the average of a sequence of numbers."""
934 or
    """Return the average of a sequence of numbers."""
935sameliteral multi-line
    """Return the average of a sequence of numbers."""
936
    """Return the average of a sequence of numbers."""
937 or
    """Return the average of a sequence of numbers."""
938length
    """Return the average of a sequence of numbers."""
61
    """Return the average of a sequence of numbers."""
62concatenate
    """Return the average of a sequence of numbers."""
63
    """Return the average of a sequence of numbers."""
64 or
    """Return the average of a sequence of numbers."""
65 with only a space between themlowercase
    """Return the average of a sequence of numbers."""
66
    """Return the average of a sequence of numbers."""
67uppercase
    """Return the average of a sequence of numbers."""
68
    """Return the average of a sequence of numbers."""
69substring
    """Return the average of a sequence of numbers."""
70
    """Return the average of a sequence of numbers."""
71 or
    """Return the average of a sequence of numbers."""
72 or
    """Return the average of a sequence of numbers."""
73slicelike
    """Return the average of a sequence of numbers."""
74, but supports negative indexessame as abovesplit
    """Return the average of a sequence of numbers."""
75 returns array
    """Return the average of a sequence of numbers."""
75 returns liststarts with
    """Return the average of a sequence of numbers."""
77 returns
    """Return the average of a sequence of numbers."""
89
returns negative, zero, or positiveremove prefixnot supported
    """Return the average of a sequence of numbers."""
90remove suffixnot supported
    """Return the average of a sequence of numbers."""
91replace first
    """Return the average of a sequence of numbers."""
92
    """Return the average of a sequence of numbers."""
93replace all
    """Return the average of a sequence of numbers."""
94
    """Return the average of a sequence of numbers."""
95trim start
    """Return the average of a sequence of numbers."""
96
    """Return the average of a sequence of numbers."""
97trim end
    """Return the average of a sequence of numbers."""
98
    """Return the average of a sequence of numbers."""
99trim both ends
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
00
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
01repeat n times
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
02
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
03 or
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
04

trình tự

JavaScript lưu trữ các chuỗi giá trị trong mảng

Python chủ yếu sử dụng bốn loại trình tự, 

    """Return the average of a sequence of numbers."""
945,
    """Return the average of a sequence of numbers."""
947,
    """Return the average of a sequence of numbers."""
949 và
    """Return the average of a sequence of numbers."""
958, cho mục đích này

  • Danh sách Python có thể thay đổi và thường đồng nhất [các phần tử có cùng loại]
  • Các bộ dữ liệu Python là bất biến và thường không đồng nhất [các phần tử có thể có các loại khác nhau]
  • Phạm vi Python là dãy số bất biến và thường được sử dụng trong vòng lặp
    function myFunction[p1, p2] {
      console.log['myFunction: p1 =', p1, 'p2 =', p2];
    }
     
    // Call function above after one second.
    const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
    if [tiredOfWaiting] clearTimeout[id1];
    09
  • Các bộ Python là các chuỗi có thể thay đổi không cho phép các giá trị trùng lặp và thường đồng nhất

Để tạo một mảng JavaScript

    """Return the average of a sequence of numbers."""
301

Để tạo một Python

    """Return the average of a sequence of numbers."""
945

    """Return the average of a sequence of numbers."""
302

Để tạo một Python

    """Return the average of a sequence of numbers."""
947

    """Return the average of a sequence of numbers."""
303

Để tạo một Python

    """Return the average of a sequence of numbers."""
949

    """Return the average of a sequence of numbers."""
304

Để tạo một Python

    """Return the average of a sequence of numbers."""
958

    """Return the average of a sequence of numbers."""
305

Tất cả các loại này có thể được lồng vào nhau. Ví dụ: các phần tử của danh sách có thể là các bộ và các phần tử của một bộ có thể là các phạm vi. Một ngoại lệ là các phần tử của một phạm vi chỉ có thể là số nguyên

Một bộ dữ liệu được đặt tên cung cấp tên cho một loại bộ dữ liệu và hỗ trợ truy cập các phần tử trong các phiên bản theo tên và chỉ mục. Ví dụ

    """Return the average of a sequence of numbers."""
306

OperationJavaScriptPythonis mảng/trình tự

function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
14_______915
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
16
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
17
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
18
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
19thêm vào
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
22 to add more than oneremove from end
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
23
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
24add to start
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
25
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
26remove from start
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
27
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
28insert
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
29
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
30remove item at index
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
31
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
32 - only for listsremove items at index range
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
33
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
34 - only for listsremove value
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
35
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
36 - error if not foundremove all
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
37
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
38change
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
39combine
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
40 and
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
41 abovelength
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
42
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
43lookup
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
44
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
45subset
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
46
can omit end and start and
can use negative indexes to count from end
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
47
can omit start and/or end and
can use negative indexes to count from endconcatenate
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
48
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
49copy [shallow]
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
50 or
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
51
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
52 - only for listsfind
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
53
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
54 - see note below this tablefind index
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
55
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
56 - see note below this tableiterate over
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
57 or
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
58
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
59 or
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
60iterate over in reverseiterate over
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
61
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
62iterate over in sorted ordercreate a sorted copy and iterate over it
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
63includes [Boolean]
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
64
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
65not includes [Boolean]
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
66
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
67index of
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
68
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
69last index of
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
70not built-in; have to reverse listcount occurrences
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
71
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
72join
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
73 returns string
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
74map
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
75
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
76filter
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
77
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
78reduce
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
79
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
80
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
81some/any [Boolean]
    """Return the average of a sequence of numbers."""
7770 - short circuits
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
83 - no short circuitevery/all [Boolean]
    """Return the average of a sequence of numbers."""
7767 - short circuits
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
85 - no short circuitsort
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
86
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
87 is a function that compares two elements
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
88
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
89 is an attribute name or a function that takes
an element and returns a value to sort onreverse
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
61
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
91 - only for listsdestructure/unpack
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
92
# of variables on left can differ from # of array elements
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
93
# of variables on left must match # of sequence elements
which limits usefulness

Python không có cách đơn giản, tích hợp sẵn để tìm mục đầu tiên trong danh sách phù hợp với một số tiêu chí. Cách tiếp cận ngây thơ này có lẽ là hiệu quả nhất

    """Return the average of a sequence of numbers."""
307

Các hàm

function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
94 và
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
95 của Python rất lười biếng, có nghĩa là chúng không được thực thi cho đến khi cần kết quả của chúng. Để nhận các giá trị từ chúng, hãy lặp lại chuyển kết quả cho
    """Return the average of a sequence of numbers."""
9639 hoặc chuyển kết quả cho một hàm như
    """Return the average of a sequence of numbers."""
945 hoặc
    """Return the average of a sequence of numbers."""
958 để nhận tất cả các giá trị

    """Return the average of a sequence of numbers."""
308

Phương thức chuỗi

function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
99 có thể lặp lại trên các chuỗi. Để nối các giá trị không phải chuỗi, hãy sử dụng hàm
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
95 và hàm
    """Return the average of a sequence of numbers."""
901 để chuyển đổi giá trị thành chuỗi. Ví dụ

    """Return the average of a sequence of numbers."""
309

JavaScript có thể triển khai các hàm lười biếng bằng cách sử dụng các hàm tạo [xem phần Danh sách hiểu], nhưng không có hàm tạo tích hợp nào được cung cấp

Sắp xếp

Giả sử chúng ta có một chuỗi các đối tượng đại diện cho mọi người và chúng ta muốn sắp xếp chuỗi theo họ theo sau tên

Đây là cách điều này có thể được thực hiện trong JavaScript

    """Return the average of a sequence of numbers."""
310

Đây là cách điều này có thể được thực hiện trong Python

    """Return the average of a sequence of numbers."""
311

Liệt kê các hiểu biết

Python hỗ trợ khả năng hiểu danh sách để tạo danh sách, nhưng JavaScript thì không. Dưới đây là một số ví dụ

    """Return the average of a sequence of numbers."""
312

Các hàm tạo JavaScript có thể được sử dụng để làm điều tương tự, nhưng một số hàm tạo tiện ích phải được viết

    """Return the average of a sequence of numbers."""
313

Python cũng hỗ trợ các hàm tạo và từ khóa

    """Return the average of a sequence of numbers."""
902. Ví dụ JavaScript ở trên có thể được triển khai như sau trong Python

    """Return the average of a sequence of numbers."""
314

bộ

Bộ là bộ sưu tập không có thứ tự không có giá trị trùng lặp

OperationJavaScriptPythonis set
    """Return the average of a sequence of numbers."""
903
function myFunction[p1, p2] {
  console.log['myFunction: p1 =', p1, 'p2 =', p2];
}
 
// Call function above after one second.
const id1 = setTimeout[[] => myFunction['arg1', 'arg2'], 1000];
if [tiredOfWaiting] clearTimeout[id1];
19create
    """Return the average of a sequence of numbers."""
905 - không thể chỉ định các phần tử
    """Return the average of a sequence of numbers."""
906 hoặc
    """Return the average of a sequence of numbers."""
907add
    """Return the average of a sequence of numbers."""
908sameremove
    """Return the average of a sequence of numbers."""
909
    """Return the average of a sequence of numbers."""
910remove all
    """Return the average of a sequence of numbers."""
911samelength
    """Return the average of a sequence of numbers."""
912
    """Return the average of a sequence of numbers."""
62includes
    """Return the average of a sequence of numbers."""
914
    """Return the average of a sequence of numbers."""
915 lặp lại
    """Return the average of a sequence of numbers."""
916
    """Return the average of a sequence of numbers."""
917chuyển đổi thành mảng/danh sách ___19________

Đặt mức độ hiểu

Python hỗ trợ khả năng hiểu tập hợp để tạo tập hợp, nhưng JavaScript thì không. Đây là một ví dụ

    """Return the average of a sequence of numbers."""
315

Bộ sưu tập khóa/giá trị

JavaScript sử dụng các đối tượng đơn giản hoặc các phiên bản của lớp

    """Return the average of a sequence of numbers."""
953 để lưu trữ các liên kết giữa các khóa và giá trị. Khóa trong đối tượng JavaScript phải là chuỗi, số nguyên hoặc ký hiệu, nhưng khóa trong phiên bản
    """Return the average of a sequence of numbers."""
953 có thể là bất kỳ loại nào

OperationJavaScript ObjectJavaScript Mapis object/dict
    """Return the average of a sequence of numbers."""
922
    """Return the average of a sequence of numbers."""
923create
    """Return the average of a sequence of numbers."""
924
có thể bao gồm các cặp khóa/giá trị ban đầu
    """Return the average of a sequence of numbers."""
925
không thể chỉ định độ dài các cặp khóa/giá trị ban đầu
    """Return the average of a sequence of numbers."""
926
    """Return the average of a sequence of numbers."""
927đặt giá trị của khóa
    """Return the average of a sequence of numbers."""
928 hoặc
    """Return the average of a sequence of numbers."""
929
    """Return the average of a sequence of numbers."""
930lấy giá trị của
use 2nd form if key contains special characters
    """Return the average of a sequence of numbers."""
933get all keys
    """Return the average of a sequence of numbers."""
7797
    """Return the average of a sequence of numbers."""
935get all values
    """Return the average of a sequence of numbers."""
936
    """Return the average of a sequence of numbers."""
937get all pairs
    """Return the average of a sequence of numbers."""
938
returns array of arrays containing a key and value
    """Return the average of a sequence of numbers."""
939
returns the sametest if key present
    """Return the average of a sequence of numbers."""
940 or
    """Return the average of a sequence of numbers."""
941
    """Return the average of a sequence of numbers."""
942delete pair
    """Return the average of a sequence of numbers."""
943 or
    """Return the average of a sequence of numbers."""
944
    """Return the average of a sequence of numbers."""
945delete all pairs
    """Return the average of a sequence of numbers."""
946
    """Return the average of a sequence of numbers."""
947iterate over keys
    """Return the average of a sequence of numbers."""
3150 or
    """Return the average of a sequence of numbers."""
3149
    """Return the average of a sequence of numbers."""
950

Python sử dụng từ điển để lưu trữ các liên kết giữa khóa và giá trị. Các khóa phải là các loại bất biến như chuỗi, số và bộ dữ liệu chứa các khóa này

OperationPythonis dict
    """Return the average of a sequence of numbers."""
951create
    """Return the average of a sequence of numbers."""
952
    """Return the average of a sequence of numbers."""
953
    """Return the average of a sequence of numbers."""
954length
    """Return the average of a sequence of numbers."""
955đặt giá trị của khóa
    """Return the average of a sequence of numbers."""
956nhận giá trị của khóa
    """Return the average of a sequence of numbers."""
957 hoặc
    """Return the average of a sequence of numbers."""
958
lỗi nếu khóa không
    """Return the average of a sequence of numbers."""
961 to get keys in sorted orderget all values
    """Return the average of a sequence of numbers."""
962get all pairs
    """Return the average of a sequence of numbers."""
963
returns a live view that provides a
sequence of tuples containing a key and valuetest if key present
    """Return the average of a sequence of numbers."""
964test if key not present
    """Return the average of a sequence of numbers."""
965delete pair
    """Return the average of a sequence of numbers."""
966delete all pairs
    """Return the average of a sequence of numbers."""
967iterate over
    """Return the average of a sequence of numbers."""
968

Trăn 3. 9 đã thêm các toán tử

    """Return the average of a sequence of numbers."""
969 và
    """Return the average of a sequence of numbers."""
970 để hợp nhất các đối tượng dict

Tạo một đối tượng từ điển

Có bốn cách để tạo từ điển Python

    """Return the average of a sequence of numbers."""
316

Biểu thức chính quy

Trong JavaScript, biểu thức chính quy là một loại tích hợp sẵn. Một thể hiện có thể được tạo theo hai cách

    """Return the average of a sequence of numbers."""
317

Trong Python, nhập mô-đun

    """Return the average of a sequence of numbers."""
971 để sử dụng các biểu thức chính quy. Những cái sẽ được sử dụng nhiều lần nên được biên dịch. Nếu không, chúng có thể được sử dụng nội tuyến

Ví dụ sau minh họa hai cách mà mọi phương thức biểu thức chính quy có thể được sử dụng, gọi nó trên mô-đun

    """Return the average of a sequence of numbers."""
971 hoặc trên một biểu thức chính quy được biên dịch

    """Return the average of a sequence of numbers."""
318

OperationJavaScriptPythoncreate

    """Return the average of a sequence of numbers."""
973 hoặc
    """Return the average of a sequence of numbers."""
974
    """Return the average of a sequence of numbers."""
975
    """Return the average of a sequence of numbers."""
976kiểm tra xem một chuỗi có khớp với
    """Return the average of a sequence of numbers."""
977
    """Return the average of a sequence of numbers."""
978
trả về một đối tượng khớp hoặc
    """Return the average of a sequence of numbers."""
965 nếu không khớplấy trước
returns an iterable over match objectssplit string on re
    """Return the average of a sequence of numbers."""
984
returns an array of strings
    """Return the average of a sequence of numbers."""
985
returns a list of strings

Các đối tượng đối sánh Python hỗ trợ các phương thức sau

  •     """Return the average of a sequence of numbers."""
    986 – trả về chuỗi phù hợp
  •     """Return the average of a sequence of numbers."""
    987 – trả về chỉ số bắt đầu của trận đấu [bao gồm]
  •     """Return the average of a sequence of numbers."""
    988 – trả về chỉ số kết thúc của trận đấu [độc quyền]
  •     """Return the average of a sequence of numbers."""
    989 – trả về một bộ chứa chỉ mục bắt đầu và kết thúc

Để biết thêm thông tin về hỗ trợ biểu thức chính quy trong Python, hãy xem Tài liệu thư viện chuẩn Python

Xử lý lỗi

Python coi lỗi là ngoại lệ

Hoạt độngJavaScriptPythonthrow_______1990
    """Return the average of a sequence of numbers."""
991bắt
    """Return the average of a sequence of numbers."""
992
    """Return the average of a sequence of numbers."""
993rethrow
    """Return the average of a sequence of numbers."""
994
    """Return the average of a sequence of numbers."""
995

trong JavaScript

    """Return the average of a sequence of numbers."""
319

Trong Python

    """Return the average of a sequence of numbers."""
770

Để bỏ qua một ngoại lệ trong Python, hãy bao gồm câu lệnh

    """Return the average of a sequence of numbers."""
996 trong khối
    """Return the average of a sequence of numbers."""
997

Có nhiều lớp ngoại lệ Python tích hợp. Lớp cơ sở của tất cả chúng là

    """Return the average of a sequence of numbers."""
998. Tên của các lớp ngoại lệ tích hợp kết thúc bằng "Lỗi. " Để biết danh sách chúng, hãy xem Ngoại lệ tích hợp

Tên của các lớp ngoại lệ tùy chỉnh cũng phải kết thúc bằng "Lỗi. " Đây là một ví dụ về cách xác định một

    """Return the average of a sequence of numbers."""
771

Thoát với trạng thái

Cả hai chương trình JavaScript chạy trong Node. js và các chương trình Python có thể thoát và đặt mã trạng thái một cách rõ ràng

trong nút. js, phương thức

    """Return the average of a sequence of numbers."""
999 của đối tượng toàn cầu
    """Return the average of a sequence of numbers."""
9000 được sử dụng

    """Return the average of a sequence of numbers."""
772

Trong Python, phương pháp

    """Return the average of a sequence of numbers."""
999 của mô-đun
    """Return the average of a sequence of numbers."""
9002 được sử dụng

    """Return the average of a sequence of numbers."""
773

Trong cả hai trường hợp, nếu không có mã trạng thái nào được cung cấp, nó sẽ mặc định bằng 0

JSON

Trong Python, để sử dụng các phương thức JSON bao gồm

    """Return the average of a sequence of numbers."""
9003

OperationJavaScriptPythoncreate
    """Return the average of a sequence of numbers."""
9004
    """Return the average of a sequence of numbers."""
9005parse
    """Return the average of a sequence of numbers."""
9006
    """Return the average of a sequence of numbers."""
9007

Viết và đọc tệp

Nút. js và Python đều có thể đọc và ghi các tệp chứa dữ liệu văn bản hoặc nhị phân

Một trường hợp cần xem xét là khi tệp có thể dễ dàng nằm gọn trong bộ nhớ. Chúng ta sẽ xem một ví dụ sử dụng JSON thể hiện điều này. Một trường hợp khác là khi nó không thể và do đó phải được xử lý bằng các luồng, có thể là một dòng tại một thời điểm. Chúng ta sẽ xem một ví dụ sử dụng CSV thể hiện điều này

Viết và đọc các tệp nhỏ

Hãy viết một tệp JSON và sau đó đọc lại để xác minh rằng nó đã hoạt động

Đây là một phiên bản JavaScript. Lưu ý rằng nó sử dụng các trạng thái chờ cấp cao nhất, yêu cầu mã phải nằm trong mô-đun ES. Một cách để đáp ứng điều này là cung cấp cho tệp một phần mở rộng tệp

    """Return the average of a sequence of numbers."""
94

    """Return the average of a sequence of numbers."""
774

Và đây là phiên bản Python

    """Return the average of a sequence of numbers."""
775

Viết và đọc các tệp lớn

Hãy viết một tệp CSV và sau đó đọc lại để xác minh rằng nó đã hoạt động. Thay vì viết toàn bộ tệp cùng một lúc, chúng tôi sẽ viết từng dòng một. Tương tự như vậy, chúng tôi sẽ đọc tệp một dòng tại một thời điểm

Đây là một phiên bản JavaScript

    """Return the average of a sequence of numbers."""
776

Và đây là phiên bản Python

    """Return the average of a sequence of numbers."""
777

Lệnh Shell

Mã JavaScript chạy trong Node. js có thể thực thi các lệnh shell, cung cấp đầu vào cho chúng và ghi đầu ra được ghi vào thiết bị xuất chuẩn và thiết bị xuất chuẩn

Trong mã ví dụ bên dưới, chúng tôi thực thi lệnh

    """Return the average of a sequence of numbers."""
9009 để lấy trạng thái của tất cả các quy trình hiện đang chạy và sau đó xuất id quy trình [pid] và thời gian chạy của tất cả các quy trình "nút"

    """Return the average of a sequence of numbers."""
778

Đây là phiên bản Python làm tương tự

    """Return the average of a sequence of numbers."""
779

người trang trí

Python hỗ trợ các trình trang trí, là các chú thích được đặt trước các hàm và lớp để thay đổi hành vi của chúng. Ủy ban TC39 kiểm soát tiêu chuẩn ECMAScript cho JavaScript đã thảo luận về việc thêm các trình trang trí trong nhiều năm, nhưng chúng vẫn chưa được thêm vào

Dưới đây là một ví dụ đơn giản về trình trang trí ghi lại các đối số và giá trị trả về của mọi giá trị lệnh gọi của các hàm mà nó được áp dụng

    """Return the average of a sequence of numbers."""
960

Các trình trang trí Python tích hợp bao gồm

  •     """Return the average of a sequence of numbers."""
    7761 Điều này biến đổi một phương thức thành một phương thức lớp nhận đối tượng lớp làm tham số đầu tiên và có thể sử dụng nó để truy cập trạng thái lớp
  •     """Return the average of a sequence of numbers."""
    9011 Điều này xác định các phương thức getter, setter và deleter cho một thuộc tính thể hiện của lớp
  •     """Return the average of a sequence of numbers."""
    7755 Điều này biến đổi một phương thức thành một phương thức tĩnh không nhận đối tượng lớp làm tham số đầu tiên và không thể truy cập trạng thái lớp. Nó rất hữu ích cho các chức năng tiện ích

Để biết thêm thông tin, hãy xem Real Python Primer trên Python Decorators

Kiểm tra chạy như chính

Một số tệp nguồn có thể được sử dụng làm điểm bắt đầu của tập lệnh và mô-đun do người khác nhập vào. Để bao gồm mã chỉ chạy khi tệp được thực thi dưới dạng tập lệnh, hãy gói mã đó vào một trong các câu lệnh sau đây

    """Return the average of a sequence of numbers."""
3076

trong nút. js, sử dụng

    """Return the average of a sequence of numbers."""
9014

Trong Python, sử dụng

    """Return the average of a sequence of numbers."""
9015

Máy chủ HTTP

Máy chủ HTTP có thể được triển khai trong cả Node. js và Python

  • trong nút. js, một tùy chọn phổ biến là sử dụng gói Express
  • Trong Python, các tùy chọn phổ biến bao gồm Flask và FastAPI

Để chứng minh các tùy chọn này, chúng tôi sẽ triển khai các máy chủ HTTP

  • phục vụ các tệp tĩnh trong thư mục "công khai"
  • triển khai các dịch vụ REST cung cấp các thao tác CRUD trên bộ sưu tập chó

Bộ sưu tập chó có thể được duy trì trong cơ sở dữ liệu, nhưng chúng tôi sẽ chỉ giữ chúng trong bộ nhớ trong bộ sưu tập khóa/giá trị trong đó khóa là id chó và giá trị là đối tượng chó có thuộc tính id, giống và tên

Chúng tôi muốn các máy chủ

  • cung cấp nhật ký yêu cầu
  • hỗ trợ chia sẻ tài nguyên nguồn gốc chéo [CORS], vì vậy các ứng dụng web được cung cấp từ một miền khác có thể gọi chúng
  • xử lý các yêu cầu
        """Return the average of a sequence of numbers."""
    9016 bằng cách trả về tất cả các chú chó dưới dạng JSON
  • xử lý yêu cầu
        """Return the average of a sequence of numbers."""
    9017 bằng cách thêm con chó được mô tả trong nội dung yêu cầu
  • xử lý các yêu cầu
        """Return the average of a sequence of numbers."""
    9018 bằng cách cập nhật con chó với id đã cho bằng cách sử dụng dữ liệu trong phần thân yêu cầu
  • xử lý các yêu cầu
        """Return the average of a sequence of numbers."""
    9019 bằng cách xóa con chó có id đã cho

Máy chủ JavaScript Express REST

  1. Tạo một thư mục cho dự án và cd vào nó

  2. Tạo tệp

        """Return the average of a sequence of numbers."""
    3010 cho dự án bằng cách nhập
        """Return the average of a sequence of numbers."""
    3011 và trả lời các câu hỏi mà nó yêu cầu

  3. Cài đặt các phụ thuộc cần thiết bằng cách nhập

        """Return the average of a sequence of numbers."""
    9022 và
        """Return the average of a sequence of numbers."""
    9023

  4. Thay thế tập lệnh "thử nghiệm" trong

        """Return the average of a sequence of numbers."""
    3010 bằng
        """Return the average of a sequence of numbers."""
    9025. Lệnh
        """Return the average of a sequence of numbers."""
    9026 cung cấp chế độ xem tệp tự động và khởi động lại máy chủ

  5. Tạo tệp

        """Return the average of a sequence of numbers."""
    9027 chứa thông tin sau

    """Return the average of a sequence of numbers."""
961

  1. Chạy máy chủ bằng cách nhập
        """Return the average of a sequence of numbers."""
    9028

Máy chủ REST Flask của Python

Lợi ích chính của Flask là

  • khả năng triển khai API REST
  • tạo HTML phía máy chủ
  • cung cấp ghi nhật ký yêu cầu
  • cung cấp tập tin xem và khởi động lại máy chủ
  1. Cài đặt các phụ thuộc cần thiết bằng cách nhập
        """Return the average of a sequence of numbers."""
    9029. Flask-CORS hỗ trợ bật CORS trong máy chủ Flask
  2. Nếu chạy trong môi trường UNIX, hãy tạo tệp script
        """Return the average of a sequence of numbers."""
    9030 được hiển thị bên dưới và làm cho nó có thể thực thi được bằng cách nhập
        """Return the average of a sequence of numbers."""
    9031

    """Return the average of a sequence of numbers."""
962

  1. Đặt
        """Return the average of a sequence of numbers."""
    9032 thành
        """Return the average of a sequence of numbers."""
    9033 cung cấp chế độ xem tệp tự động và khởi động lại máy chủ
  2. Nếu chạy trong Windows, hãy tạo một tệp
        """Return the average of a sequence of numbers."""
    9034 tương tự
  1. Tạo tệp
        """Return the average of a sequence of numbers."""
    9035 chứa thông tin sau

    """Return the average of a sequence of numbers."""
963

  1. Chạy máy chủ bằng cách nhập
        """Return the average of a sequence of numbers."""
    9036

Máy chủ REST API FastAPI của Python

Lợi ích chính của FastAPI là

  • xác thực loại nội dung yêu cầu/phản hồi và tham số đường dẫn/truy vấn
  • tự động tạo tài liệu API mở
  • cung cấp ghi nhật ký yêu cầu
  • cung cấp phần mềm trung gian CORS
  • cung cấp tập tin xem và khởi động lại máy chủ
  1. Cài đặt các phụ thuộc cần thiết bằng cách nhập
        """Return the average of a sequence of numbers."""
    9037
  2. Tạo tệp
        """Return the average of a sequence of numbers."""
    9035 chứa thông tin sau

    """Return the average of a sequence of numbers."""
964

  1. Chạy máy chủ bằng cách nhập
        """Return the average of a sequence of numbers."""
    9039. Bao gồm tùy chọn
        """Return the average of a sequence of numbers."""
    9040 cung cấp khả năng xem tệp tự động và khởi động lại máy chủ
  2. Kiểm tra các tài liệu Open API được cung cấp miễn phí. Duyệt máy chủ cục bộ. 1919/docs để xem tài liệu API và thử từng API từ trình duyệt

Máy khách HTTP

Các ứng dụng JavaScript thường sử dụng Fetch API để gửi các yêu cầu HTTP. Điều này được tích hợp vào các trình duyệt web hiện đại và có thể được sử dụng trong các ứng dụng Node bằng cách cài đặt gói

    """Return the average of a sequence of numbers."""
9041 với
    """Return the average of a sequence of numbers."""
9042

Đây là một ví dụ khi chúng tôi tìm nạp hình ảnh của một giống chó nhất định

    """Return the average of a sequence of numbers."""
965

Các ứng dụng Python thường sử dụng gói

    """Return the average of a sequence of numbers."""
9043 để gửi các yêu cầu HTTP. Nó có thể được cài đặt bằng cách nhập
    """Return the average of a sequence of numbers."""
9044

Đây là ví dụ tương tự sử dụng Python

    """Return the average of a sequence of numbers."""
966

Phương pháp ma thuật Python

Các phương thức ma thuật của Python hỗ trợ nhiều thao tác trên các lớp và các thể hiện của lớp. Chúng bao gồm quá tải toán tử

Bảng sau đây cung cấp danh sách một phần, được phân loại của các phương thức ma thuật mà một lớp Python có thể triển khai

Thông số phương thứcMục đíchVòng đời đối tượng
    """Return the average of a sequence of numbers."""
9045cls,. tạo một đối tượng mới
    """Return the average of a sequence of numbers."""
9046self,. khởi tạo một đối tượng mới
    """Return the average of a sequence of numbers."""
9047selfhủy một đối tượng
    """Return the average of a sequence of numbers."""
9048self, têncó thể được sử dụng để triển khai mẫu "phương thức bị thiếu"
[xem ví dụ bên dưới]Biểu diễn chuỗi
    """Return the average of a sequence of numbers."""
9049selftrả lại một chuỗi đại diện hữu ích cho nhà phát triển
    """Return the average of a sequence of numbers."""
9050selftrả lại một chuỗi đại diện hữu ích cho người dùngSo sánh
    """Return the average of a sequence of numbers."""
9051self, otherxác định nếu điều này . xử lý một đối tượng như một chức năng;
class must inherit from
    """Return the average of a sequence of numbers."""
954Math Operations
    """Return the average of a sequence of numbers."""
9065self, otheradds an object to another
    """Return the average of a sequence of numbers."""
9066self, othersubtracts an object from another
    """Return the average of a sequence of numbers."""
9067self, othermultiplies an object by another
    """Return the average of a sequence of numbers."""
9068self, otherdivides an object by another
    """Return the average of a sequence of numbers."""
9069self, othermods an object by anotherPickling [serialization]
    """Return the average of a sequence of numbers."""
9070selfpickles an object
    """Return the average of a sequence of numbers."""
9071selfunpickles an objectOther
    """Return the average of a sequence of numbers."""
9072self, ...treats an object as a function; can change state

Mẫu "phương thức bị thiếu" hỗ trợ gọi các phương thức không tồn tại trên một đối tượng và suy ra ý nghĩa trong thời gian chạy. Điều này thường được sử dụng trong khung "Ruby on Rails" khi triển khai "Bản ghi hoạt động. "

Ví dụ: giả sử chúng ta muốn triển khai một lớp có các đối tượng hỗ trợ các phương thức có tên bắt đầu bằng "add" và kết thúc bằng một số. Các phương thức này chấp nhận một số khác làm đối số và trả về tổng của các số. Điều này có thể được thực hiện như sau

    """Return the average of a sequence of numbers."""
967

Trong hầu hết các trường hợp, sử dụng các phương thức và tham số thông thường thay vì phương thức

    """Return the average of a sequence of numbers."""
9073 sẽ dẫn đến mã dễ hiểu và dễ bảo trì hơn

JavaScript có thể làm điều gì đó tương tự bằng cách sử dụng "proxy. "

    """Return the average of a sequence of numbers."""
968

các loại

Để kiểm tra kiểu cho JavaScript, hãy sử dụng trình biên dịch TypeScript. TypeScript là một siêu bộ JavaScript có thêm các loại

Hai công cụ phổ biến cung cấp tính năng kiểm tra kiểu trên tệp nguồn Python là mypy và Pyright

Phần còn lại của phần này tập trung vào kiểm tra kiểu Python

Thông số kỹ thuật loại Python được gọi là "gợi ý loại. " Trình thông dịch

    """Return the average of a sequence of numbers."""
907 bỏ qua gợi ý loại, nhưng chúng khiến thời gian khởi động lâu hơn một chút. Chúng hữu ích như tài liệu, ngay cả khi không sử dụng trình kiểm tra loại. IDE có thể sử dụng chúng để đánh dấu các vấn đề về loại

Các loại nguyên thủy được hỗ trợ là

Loại TênÝ nghĩa
    """Return the average of a sequence of numbers."""
9075Boolean
    """Return the average of a sequence of numbers."""
7781chuỗi số nguyên bất biến từ 0 đến 255
    """Return the average of a sequence of numbers."""
933số phức với
    """Return the average of a sequence of numbers."""
932 phần thực và phần ảo
    """Return the average of a sequence of numbers."""
932số dấu phẩy động chính xác kép
    """Return the average of a sequence of numbers."""
931số nguyên chính xác không giới hạn
    """Return the average of a sequence of numbers."""
901chuỗi

Các loại bộ sưu tập được hỗ trợ là

Nhập Tên Ý nghĩa
    """Return the average of a sequence of numbers."""
9082dict với các khóa thuộc loại KT và các giá trị thuộc loại VT
    """Return the average of a sequence of numbers."""
9083danh sách có các phần tử thuộc loại T
    """Return the average of a sequence of numbers."""
9084bất kỳ loại chuỗi nào có tất cả các phần tử thuộc loại T
    """Return the average of a sequence of numbers."""
9085bộ có các phần tử thuộc loại T
    """Return the average of a sequence of numbers."""
9086bộ có các phần tử được chỉ định loại

Các loại khác được hỗ trợ là

Type NameMeaning_______29087bất kỳ giá trị bất kỳ tên lớp nào thể hiện của lớp hoặc thể hiện của một lớp con
    """Return the average of a sequence of numbers."""
9088hàm nhận tham số của các loại P1, P2,. và trả về hàm loại RT
    """Return the average of a sequence of numbers."""
9089 nhận bất kỳ tham số nào và trả về hàm tạo loại RT
    """Return the average of a sequence of numbers."""
9090;
    """Return the average of a sequence of numbers."""
9091 và
    """Return the average of a sequence of numbers."""
9092 có thể là bộ dữ liệu tên là
    """Return the average of a sequence of numbers."""
965
    """Return the average of a sequence of numbers."""
9094 trong đó các phần tử có loại T1, T2,.
    """Return the average of a sequence of numbers."""
9095khớp với
    """Return the average of a sequence of numbers."""
965 hoặc loại T
giống như
    """Return the average of a sequence of numbers."""
9097
    """Return the average of a sequence of numbers."""
9098khớp với một đối tượng lớp cho lớp C hoặc một phân lớp
    """Return the average of a sequence of numbers."""
9099khớp với bất kỳ loại nào được chỉ định

Tất cả các loại trên có tên bắt đầu bằng chữ in hoa phải được nhập từ mô-đun "đánh máy". Ví dụ:

    """Return the average of a sequence of numbers."""
9100. Trăn 3. 9 được cho là không cần thiết, nhưng có lẽ mypy chưa hỗ trợ cú pháp kiểu mới

Loại

    """Return the average of a sequence of numbers."""
9101 có thể được sử dụng trong các loại tập hợp để cho phép các phần tử có một tập hợp các loại

Bí danh có thể được xác định cho các mô tả loại dài. Điều này hữu ích khi mô tả cùng loại được sử dụng ở nhiều nơi. Ví dụ:

    """Return the average of a sequence of numbers."""
9102

Để thêm "gợi ý kiểu" vào một biến hoặc tham số hàm, hãy làm theo tên của nó với dấu hai chấm, dấu cách và kiểu

Để thêm một gợi ý kiểu trả về vào một hàm, hãy làm theo dấu ngoặc đơn bên phải của danh sách đối số với

    """Return the average of a sequence of numbers."""
9103 và kiểu

Ví dụ, nếu

    """Return the average of a sequence of numbers."""
9104 là một lớp chúng ta đã định nghĩa

    """Return the average of a sequence of numbers."""
969

Mô-đun

    """Return the average of a sequence of numbers."""
9105 cũng xác định hàm
    """Return the average of a sequence of numbers."""
9106. Đây là một ví dụ về việc sử dụng nó

    """Return the average of a sequence of numbers."""
980

mypy

mypy là một công cụ kiểm tra kiểu Python được triển khai trong Python. Bắt đầu phát triển vào năm 2014

Để cài đặt mypy, hãy nhập

    """Return the average of a sequence of numbers."""
9107. Trên máy Mac, hãy thêm thư mục sau vào biến môi trường
    """Return the average of a sequence of numbers."""
9108.
    """Return the average of a sequence of numbers."""
9109

Để chạy mypy trên tệp nguồn và tất cả các tệp mà nó nhập, hãy nhập

    """Return the average of a sequence of numbers."""
9110

mypy không thể thực hiện kiểm tra kiểu trên các đối số hàm tương ứng với các tham số có giá trị mặc định hoặc các tham số thu thập các đối số biến đổi trong một bộ hoặc dict

Pyright

Pyright là một công cụ kiểm tra kiểu Python được triển khai trong TypeScript. Bắt đầu phát triển vào năm 2019. Nó được sử dụng bởi tiện ích mở rộng Mã VS Pylance và cũng có thể được chạy từ dòng lệnh

Để cài đặt Pyright, hãy cài đặt Node. js và nhập

    """Return the average of a sequence of numbers."""
9111

Để chạy Pyright trên tệp nguồn và tất cả các tệp mà nó nhập, hãy nhập

    """Return the average of a sequence of numbers."""
9112. Để chạy ở chế độ đồng hồ, hãy nhập
    """Return the average of a sequence of numbers."""
9113. Xem vấn đề này

Để định cấu hình các tùy chọn cho Pyright, hãy tạo tệp

    """Return the average of a sequence of numbers."""
9114 trong thư mục gốc của dự án. Ví dụ: nội dung sau định cấu hình kiểm tra loại nghiêm ngặt cho tất cả các tệp trong thư mục hiện tại

Để trả lời cho câu hỏi "Kế hoạch dài hạn cho Pyright là gì?"

"Pyright là một dự án phụ không có đội ngũ chuyên dụng. Không có gì đảm bảo cho việc tiếp tục phát triển dự án. Nếu bạn thấy nó hữu ích, hãy sử dụng nó và đóng góp cho cơ sở mã. "

tập tin sơ khai

Các loại có thể được chỉ định trong "tệp sơ khai" với phần mở rộng

    """Return the average of a sequence of numbers."""
9115 thay vì trực tiếp trong tệp
    """Return the average of a sequence of numbers."""
95

Có thể tải xuống các tệp sơ khai cho các thư viện Python phổ biến từ typeshed. Chúng được bao gồm như một mô hình con của mypy. Xem liên kết đã nhập để được hướng dẫn cài đặt chúng

Lưu ý rằng số lượng thư viện được trình bày ở đây hiện ít và nó không chứa các tệp sơ khai cho nhiều thư viện phổ biến, bao gồm cả mathplotlib, numpy và pandas. Loại tệp sơ khai cho matplotlib, numpy và pandas có thể được tìm thấy tại data-science-types

Khi tạo các tệp sơ khai của riêng bạn, hãy tạo các tệp

    """Return the average of a sequence of numbers."""
9115 có cùng tên với các tệp
    """Return the average of a sequence of numbers."""
95 có loại mà chúng mô tả. Các tệp sơ khai có thể được đặt trong cùng thư mục với định nghĩa mô-đun hoặc trong một thư mục riêng có tên như "sơ khai. " Nếu chúng nằm trong một thư mục riêng biệt, hãy xác định biến môi trường
    """Return the average of a sequence of numbers."""
9119 để trỏ đến thư mục

Nếu có một tệp sơ khai cho một mô-đun, các định nghĩa kiểu của nó sẽ ghi đè bất kỳ tệp nào được tìm thấy trong tệp

    """Return the average of a sequence of numbers."""
95 tương ứng

Để xác định loại biến trong tệp sơ khai, hãy sử dụng cú pháp

    """Return the average of a sequence of numbers."""
9121

Để xác định các loại cho một hàm trong tệp sơ khai, hãy sử dụng cú pháp

    """Return the average of a sequence of numbers."""
9122. Lưu ý rằng
    """Return the average of a sequence of numbers."""
3110 là cú pháp thực tế

Một tệp sơ khai có thể được tạo tự động từ các tệp

    """Return the average of a sequence of numbers."""
95 bằng cách sử dụng lệnh
    """Return the average of a sequence of numbers."""
9125 được cài đặt với mypy. Theo mặc định, nó đặt các tệp sơ khai được tạo trong thư mục con có tên là
    """Return the average of a sequence of numbers."""
9126, nhưng điều này có thể được thay đổi bằng cách sử dụng tùy chọn
    """Return the average of a sequence of numbers."""
9127. Ví dụ:
    """Return the average of a sequence of numbers."""
9128 tạo
    """Return the average of a sequence of numbers."""
9129 trong thư mục hiện tại. Nhiều loại sẽ là
    """Return the average of a sequence of numbers."""
9087, vì vậy nên chỉnh sửa thủ công các tệp được tạo để làm cho các loại nghiêm ngặt hơn

Đây là một mô-đun ví dụ trong

    """Return the average of a sequence of numbers."""
9131 bỏ qua gợi ý loại

    """Return the average of a sequence of numbers."""
981

Đây là một tệp sơ khai trong

    """Return the average of a sequence of numbers."""
9129 cung cấp các gợi ý về loại

    """Return the average of a sequence of numbers."""
982

Đây là mã sử dụng mô-đun này

    """Return the average of a sequence of numbers."""
983

Chạy báo cáo lệnh

    """Return the average of a sequence of numbers."""
9133

    """Return the average of a sequence of numbers."""
984

Công cụ/Thư viện/Khung phổ biến

Chủ đềJavaScriptPythondòng lệnhNode. js
    """Return the average of a sequence of numbers."""
99 command_______2907 commandutilitiesLodash, Ramdapydashweb serverExpressFlask, FastAPIweb frameworkReact, Svelte, VueFlaskdates và timesdate. fns, Khoảnh khắc. js, Temporaldatetime [trong thư viện chuẩn]lintingESLintpylint, flake8unit testsJest, Mocha, Chai, @testing-librarypytest, unittest [trong thư viện chuẩn], thử nghiệm từ đầu đến cuối nose2Cypresssamemathmathjsmath [trong thư viện chuẩn]trình chỉnh sửa phổ biếnVS CodeVS Code, PyCharm [JetBrains]

vải lót

Để lint JavaScript bằng ESLint

  •     """Return the average of a sequence of numbers."""
    9136
  •     """Return the average of a sequence of numbers."""
    9137

Để định cấu hình các quy tắc được sử dụng bởi ESLint, hãy tạo tệp

    """Return the average of a sequence of numbers."""
9138 trong thư mục dự án hoặc trong thư mục chính của người dùng

Ví dụ

    """Return the average of a sequence of numbers."""
985

Để lint mã Python bằng pylint

  •     """Return the average of a sequence of numbers."""
    9139
  •     """Return the average of a sequence of numbers."""
    9140

Để định cấu hình các quy tắc được sử dụng bởi pylint, hãy tạo tệp dự án

    """Return the average of a sequence of numbers."""
9141 hoặc một tệp trong thư mục chính của người dùng [
    """Return the average of a sequence of numbers."""
9142 hoặc
    """Return the average of a sequence of numbers."""
9143]. Để tạo phiên bản nhận xét của tệp này nhằm tìm hiểu về định dạng và các tùy chọn có sẵn, hãy nhập
    """Return the average of a sequence of numbers."""
9144. Tập tin này sau đó có thể được sửa đổi hoặc chỉ được sử dụng để tham khảo. Ví dụ

    """Return the average of a sequence of numbers."""
986

Ngoài ra, các quy tắc có thể bị vô hiệu hóa trong các tệp nguồn bằng cách thêm các nhận xét đặc biệt. Ví dụ

    """Return the average of a sequence of numbers."""
987

Để lint mã Python bằng flake8

Để định cấu hình các quy tắc được sử dụng bởi flake8, hãy tạo tệp dự án

    """Return the average of a sequence of numbers."""
9145 hoặc một tệp trong thư mục chính của người dùng [
    """Return the average of a sequence of numbers."""
9146 cho Windows và
    """Return the average of a sequence of numbers."""
9147 cho macOS và Linux]. Ví dụ

    """Return the average of a sequence of numbers."""
988

định dạng

Để định dạng mã JavaScript, hãy sử dụng Prettier

Để định dạng mã Python, hãy sử dụng autopep8 hoặc Black

Để cài đặt và chạy autopep8

    """Return the average of a sequence of numbers."""
989

Một số thay đổi được thực hiện bởi autopep8 bao gồm

  • thực thi có hai dòng trống trước mỗi định nghĩa chức năng
  • ngắt dòng dài hơn 79 ký tự

Có thể thay đổi độ dài dòng tối đa bằng cách thêm tùy chọn

    """Return the average of a sequence of numbers."""
9148

Để cài đặt và chạy Black

    """Return the average of a sequence of numbers."""
0

Một số thay đổi được thực hiện bởi Đen bao gồm

  • thực thi có hai dòng trống trước mỗi định nghĩa chức năng
  • thay đổi tất cả các chuỗi ký tự để được phân tách bằng dấu ngoặc kép thay vì dấu ngoặc đơn
  • thêm dấu phẩy sau các mục danh sách cuối cùng khi danh sách không vừa trên một dòng
  • ngắt dòng dài hơn 88 ký tự

Có thể thay đổi độ dài dòng tối đa bằng cách thêm tùy chọn

    """Return the average of a sequence of numbers."""
9149

Mã VS

Tiện ích mở rộng Mã VS "Python" của Microsoft bổ sung "IntelliSense, linting, gỡ lỗi, điều hướng mã, định dạng mã, hỗ trợ máy tính xách tay Jupyter, tái cấu trúc, trình khám phá biến, trình khám phá thử nghiệm, đoạn trích, v.v. "

Điểm tương đồng giữa Python và JavaScript là gì?

Tập hợp con "dựa trên đối tượng" của Python gần tương đương với JavaScript . Giống như JavaScript [và không giống như Java], Python hỗ trợ phong cách lập trình sử dụng các hàm và biến đơn giản mà không cần tham gia vào các định nghĩa lớp.

Python liên quan đến JavaScript như thế nào?

JavaScript và Python khác nhau ở chỗ. JavaScript thường được sử dụng cho giao diện người dùng trong khi Python được sử dụng để phát triển phụ trợ . JavaScript hầu như chỉ được sử dụng để xây dựng các công nghệ chạy trên trình duyệt web trong khi Python có thể được sử dụng cho các ứng dụng khoa học và chuyên ngành.

Python và JavaScript có giống nhau không?

Python và JavaScript là những ngôn ngữ rất mạnh với các ứng dụng khác nhau trong thế giới thực . Python có thể được sử dụng để phát triển web và cho nhiều ứng dụng, bao gồm cả mục đích khoa học. JavaScript chủ yếu được sử dụng để phát triển web [front-end và back-end] và để phát triển ứng dụng di động.

JavaScript có dễ so với Python không?

Câu trả lời. JavaScript khó thành thạo hơn Python . Python thường là lựa chọn dành cho người mới bắt đầu, đặc biệt đối với những người chưa có bất kỳ kinh nghiệm lập trình nào trước đó. Mã Python nổi tiếng là dễ đọc hơn, nghĩa là dễ hiểu [và viết] hơn.

Chủ Đề