Python js

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ủ đềJavaScriptPythontiêu chuẩ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 Node. 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 so với 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à
    import threading
     
    def my_function[p1, p2]:
        print['my_function: p1 =', p1, 'p2 =', p2]
     
    # The Timer function takes the number of seconds to wait,
    # the function to call, and a tuple of arguments to be passed.
    t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
    t.start[]
    if tired_of_waiting:
        t.cancel[]
    0 không được hỗ trợ. Xem phần cho mộ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ư
    import threading
     
    def my_function[p1, p2]:
        print['my_function: p1 =', p1, 'p2 =', p2]
     
    # The Timer function takes the number of seconds to wait,
    # the function to call, and a tuple of arguments to be passed.
    t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
    t.start[]
    if tired_of_waiting:
        t.cancel[]
    1 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 duy nhất. Xem danh sách trong phần
  • 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
    import threading
     
    def my_function[p1, p2]:
        print['my_function: p1 =', p1, 'p2 =', p2]
     
    # The Timer function takes the number of seconds to wait,
    # the function to call, and a tuple of arguments to be passed.
    t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
    t.start[]
    if tired_of_waiting:
        t.cancel[]
    2 đượ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à

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
3 hoặc
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
4 [đố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à

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
5

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
    import threading
     
    def my_function[p1, p2]:
        print['my_function: p1 =', p1, 'p2 =', p2]
     
    # The Timer function takes the number of seconds to wait,
    # the function to call, and a tuple of arguments to be passed.
    t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
    t.start[]
    if tired_of_waiting:
        t.cancel[]
    6 hoặc
    import threading
     
    def my_function[p1, p2]:
        print['my_function: p1 =', p1, 'p2 =', p2]
     
    # The Timer function takes the number of seconds to wait,
    # the function to call, and a tuple of arguments to be passed.
    t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
    t.start[]
    if tired_of_waiting:
        t.cancel[]
    7

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 trên Node. js có thể lấy đối số dòng lệnh từ mảng
    import threading
     
    def my_function[p1, p2]:
        print['my_function: p1 =', p1, 'p2 =', p2]
     
    # The Timer function takes the number of seconds to wait,
    # the function to call, and a tuple of arguments to be passed.
    t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
    t.start[]
    if tired_of_waiting:
        t.cancel[]
    8. Phần tử đầu tiên là đường dẫn đến tệp thực thi
    import threading
     
    def my_function[p1, p2]:
        print['my_function: p1 =', p1, 'p2 =', p2]
     
    # The Timer function takes the number of seconds to wait,
    # the function to call, and a tuple of arguments to be passed.
    t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
    t.start[]
    if tired_of_waiting:
        t.cancel[]
    9, 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ừ
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    00. 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 trên hệ thống UNIX

  • Thêm phần này làm dòng đầu tiên.
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    01
  • Làm cho tệp có thể thực thi được bằng cách nhập
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    02
  • Để chạy nó từ một thiết bị đầu cuối, hãy nhập
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    03

Để 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. from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    04
  3. Nếu chạy tập lệnh JavaScript, hãy nhập
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    05
  4. Nếu chạy tập lệnh Python, hãy nhập
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    06

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 phần

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
07 để bắt đầu REPL và nhập
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
08 để 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
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
09 hoặc chỉ
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
10

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
11. Ví dụ

import re # for regular expressions
help[re]

quy ước đặt tên

KindJavaScriptPythonconstant
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
12samevariable
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
13
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
14function
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
13
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
14class
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
17samemethod
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
13
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
14public instance properties
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
13
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
14private instance propertiesno convention
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
22

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
23 định nghĩa các hàm
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
24 [tuân theo quy ước] và
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
25 [không]

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

TypeJavaScriptPythonBoolean
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
26,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
27
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
28,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
29numberdefault is double precision float,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
30
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
31,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
32,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
33characteruse stringssamestring
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
34 or
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
35samemulti-line string
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
36
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
37 or
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
38string interpolation
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
39
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
40array
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
41 class, literal syntax
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
42
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
43 module in standard librarylistuse
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
41 class
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
45 class with literal syntax
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
42
tương đương bộ dữ liệu có thể thay đổi và thường đồng nhất lớp ____29_______47 với cú pháp theo nghĩa đen
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
48
không thay đổi và điển hình không đồng nhấtphạm vitương đương_______29_______49 lớp
Lớp
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
50key/value_______29_______51 với cú pháp chữ
Lớp
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
52 và
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
53
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
54 với cú pháp chữ
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
55 bộ_______29_______56 lớp;
tạo với lớp
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
57_______29_______58 với cú pháp chữ
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
59
hoặc
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
60chức năngxem phần "Chức năng" bên dướixem phần "Chức năng" bên dướilớp
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
61
xem phần "Lớp học" bên dưới_______29_______62
xem phần "Các loại" bên dưới không có giá trị_______29_______63 hoặc
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
64
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
65

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. Tuple 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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
53 có thể là bất kỳ loại giá trị nào. Python cho biết các khóa 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
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
29,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
68,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
69,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
70 [phức tạp], chuỗi trống, chuỗi trống và
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
65JavaScript
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
27,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
68,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
74, chuỗi trống,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
63 và
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
64

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
26. Chúng bao gồm mảng, đối tượng, trường hợp
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
53 và trường hợp
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
56

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
80 ước tính thành
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
26

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
82 của dự án và thư mục toàn cầu
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
82

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
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    84
  • thư mục cài đặt cụ thể

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
85 và thực thi
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
86

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

Mô-đun TopicJavaScriptPython được xác định bởi. nội dung của fileameexport
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
87mọi thứ được xuất tự động;
chỉ ra các giá trị riêng tư bằng cách bắt đầu tên với dấu gạch dưới export
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
88không được hỗ trợimport default
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
89không được hỗ trợnhập toàn bộ mô-đun
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
90
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
91 hoặc
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
92 hoặc
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
93nhập các giá trị cụ thể_______29_______94 hoặc
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
95
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
96 hoặc
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
97Nhập mặc định và được đặt tên
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
98không được hỗ trợ danh mục mã nguồn mởhttps. //www. npmjs. com/
https. //www. npmjs. com/https. // đường ống. tổ chức/
https. // đường ống. org/tool ​​to install
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
99 [đã cài đặt Node. js]
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
00 [được cài đặt bằng Python] hoặc
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
01 [được cài đặt với 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

Bộ sưu tập nhập khẩu được đề xuất gói
trong thư viện tiêu chuẩn_______130_______02functools
trong thư viện tiêu chuẩn_______130_______03itertools
in standard library
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
04matplotlib
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
05NumPy
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
06pandas
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
07TensorFlow
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
08

gói

Gói JavaScript

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
99, đượ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
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
10 trong mỗi dự án bằng cách nhập
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
11 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ụ

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
00, đượ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
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
00 đang được sử dụng, hãy nhập
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
14

Để 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,

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
15 [chủ yếu dành cho Python 2] và
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
16 [một tập hợp con của
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
15 dành cho Python 3]

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

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
16, hãy nhập
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
19 trong thư mục gốc của dự án. Điều này tạo ra một thư mục con
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
20

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

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
21 trong thư mục
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
22. Trong Windows, nhập
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
23. Trong môi trường UNIX, nhập
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
24. [Khi sử dụng Fish shell, hãy thêm phần mở rộng
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
25. ]

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ông cụ và thư viện có trong thư mục

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
20 của dự án thay vì phiên bản 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à
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
20

Để 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

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
28

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 dự án được sử dụng dựa trên nội dung của thư mục con

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
82

Để 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

OperationJavaScriptPythonprepare a project
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
30
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
19
trong đó
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
20 là tên thư mục được sử dụng theo quy ước, cài đặt một gói trên toàn cầu
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
33không kích hoạt môi trường,
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
34cài đặt gói cục bộ_______130_______35với môi trường được kích hoạt,
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
34cài đặt một phiên bản gói cụ thể
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
37
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
38cập nhật lên phiên bản mới nhất của gói cụ thể
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
39
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
40xem vị trí các gói toàn cầu được cài đặt
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
41không kích hoạt môi trường,
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
42xem nơi các gói cục bộ được cài đặt
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
43với môi trường được kích hoạt,
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
42vị trí của các gói cục bộ
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
45
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
46xem danh sách các gói được cài đặt cục bộ
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
47 hoặc
mở
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
10 và xem
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
49 và
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
50với môi trường được kích hoạt,
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
51tạo danh sách các phiên bản gói dự án được duy trì tự động trong
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
10
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
53cài đặt các phiên bản gói dự án
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
54
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
55

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

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
56 có thể để trống hoặc chứa mã khởi tạo cho gói

Thêm các tệp

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
5 trong thư mục gói xác định mô-đun. Sau đó, trong các tệp
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
5 muốn sử dụng một mô-đun trong một gói [nằm trong các thư mục tổ tiên], hãy sử dụng
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
59. Để nhập những thứ cụ thể từ mô-đun gói, hãy sử dụng
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
60

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

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
56. Khi có các gói lồng nhau, quá trình nhập trông giống như
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
62 hoặc
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
63

Để biết thêm thông tin về các gói Python, hãy xem 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 thành thiết bị xuất chuẩn_______130_______64
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
65ghi các giá trị được phân tách bằng dấu cách thành thiết bị xuất chuẩn_______130_______66
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
85
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
68write to stdout with interpolation
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
69];
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
70write to stdout without newlinein Node. js
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
71
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
72
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
73

Biến và phép gán

JavaScript variables should be declared using either the

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
74 or
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
75 keyword. Python variables are not declared and are created when a value is assigned to them

JavaScript variable assignments can appear inside expressions such as an

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
76 or loop condition, which many developers find confusing. Đ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ụ

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday

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

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
77 bên trong các hàm sửa đổi chúng. Từ khóa liên quan
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
78 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

TopicJavaScriptPythonconstant declaration
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
79
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
80variable declaration
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
81
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
82get type of value in variable
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
83 and
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
84
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
85multiple assignment
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
86
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
87destructure sequence
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
88
# biến có thể khác với # phần tử____130_______89
# of variables must match # of elementsdestructure object
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
90not supportedun-declare variable
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
91 - just removes value
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
92addition
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
93samesubtraction
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
94samemultiplication
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
95samedivision
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
96sameexponentiation
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
97samemod [remainder]
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
98samelogical and
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
99not supportedlogical or
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
00not supportedlogical xor
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
01not supportedbitwise and
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
02samebitwise or
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
03samebitwise xor
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
01samesigned bit shift
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
05 [left],
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
06 [right]sameunsigned bit shift
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
07 [left],
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
08 [right ] không được hỗ trợ

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

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]

Biến

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
09 ở trên được đặt thành một mảng. "Toán tử còn lại"
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
10 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

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]

Biến

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
09 ở trên được đặt thành danh sách

so sánh

TopicJavaScriptPythonequal
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
12 [with coercion] or
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
13 [without]
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
12 [without coercion]not equal
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
15 [with coercion] or
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
16 [without]
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
15 [without coercion]same object
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
13
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
19different object
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
16
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
21less than
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
22sameless than or equal
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
23samegreater than
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
24samegreater than or equal
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
25same

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,
    seq = [1, 2, 3, 4] # using a tuple, but could also be a list
    a, b, *rest = seq # a=1, b=2, rest=[3, 4]
    a, *rest, b = seq # a=1, rest=[2, 3], b=4
    a, b, *rest = seq # a=1, b=2, rest=[3, 4]
    26
  • bằng Python,
    seq = [1, 2, 3, 4] # using a tuple, but could also be a list
    a, b, *rest = seq # a=1, b=2, rest=[3, 4]
    a, *rest, b = seq # a=1, rest=[2, 3], b=4
    a, b, *rest = seq # a=1, b=2, rest=[3, 4]
    27

khối mã

Khối mã JavaScript được bao quanh bởi dấu ngoặc nhọn [

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
28]. 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 [

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
29] 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 dưới đây,

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
30 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

Một câu lệnh JavaScript

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
76 có thể chứa bất kỳ số lượng
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
32 phần nào

Một câu lệnh

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
76 của Python có thể chứa bất kỳ số lượng
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
34 phần 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

TopicJavaScriptPythonif
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
35
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
36if/else
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
37
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
38if/else if/else
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
39
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
40switch
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
41not supportedternary
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
42
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
43

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

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'

lặp lại

Như chúng ta sẽ thấy trong phần sau, 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 thể hiện của lớp

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
53. Python sử dụng "từ điển" [hoặc dicts] để lưu trữ các cặp khóa/giá trị

TopicJavaScriptPythonclassic for loop
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
45
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
46over collection
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
47
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
48over object/dict keys
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
49
or
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
50
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
51over object/dict values
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
52
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
53over object/dict keys and values
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
54
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
55top-tested
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
56
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
57bottom-tested
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
58
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
59break out of closest loop
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
60samecontinue to next iteration
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
61same

Chức năng

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

// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};

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 bao quanh phần thân và từ khóa

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
62 là tùy chọn

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

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression

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 trong Hướng dẫn Python và

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í

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
63 được xác định trong thư viện chuẩn

Chủ đềJavaScriptPythondefine có tên
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
65
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
66xác định ẩn danh
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
67
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
68xác định ẩn danh với tham số duy nhất
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
69giống như trênxác định ẩn danh với biểu thức duy nhất
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
70giống như trên chỉ định giá trị tham số mặc định
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
71
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
72thu thập số biến của đối số_______7231_______3
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
09 được đặt thành
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
41
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
76
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
09 được đặt thành đối số bộ thu thập dữ liệu dưới dạng các cặp khóa/giá trị không được hỗ trợ
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
78
gọi với
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
79
hoặc
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
80sử dụng các đối số có tên/từ khóa
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
81
vượt qua một đối tượng "tùy chọn" như trên;
bất kỳ tham số nào có thể được chỉ định theo tên;
tính năng quan trọng
call with
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
79return a value
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
83
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
84default return value when no
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
62
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
63
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
65call
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
88sameget required argument count
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
89
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
90
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
91chuyển ít đối số hơn
positional parametersremaining are assigned
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
63results in an errorpassing more arguments than
tham số vị trí không có tập hợp tất cả các đối số đều có sẵn trong đối tượng kiểu mảng
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
93 kết quả có lỗilấy tên
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
94
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
95lấy mã triển khai
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
96
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
97
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
98create partial
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
99
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
00 có thể là
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
64
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
02
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
03cuộc gọi
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
04
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
00 can be
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
64
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
07apply
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
08
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
00 có thể là
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
64
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
11mảng trải rộng cho đối số vị trí
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
12
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
13đối tượng trải rộng cho đối số từ khóakhông được hỗ trợ
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
14

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 một tham số bắt đầu bằng
    import sys
     
    name = sys.argv[1] if len[sys.argv] > 1 else 'World'
     
    # This is a nice alternative.
    name = sys.argv[1] or 'World'
     
    # This syntax is NOT supported.
    name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
    15 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
    import sys
     
    name = sys.argv[1] if len[sys.argv] > 1 else 'World'
     
    # This is a nice alternative.
    name = sys.argv[1] or 'World'
     
    # This syntax is NOT supported.
    name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
    16 [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
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    24
  • Độ sâu đệ quy tối đa mặc định là 1000. Để thay đổi điều này, hãy gọi
    import sys
     
    name = sys.argv[1] if len[sys.argv] > 1 else 'World'
     
    # This is a nice alternative.
    name = sys.argv[1] or 'World'
     
    # This syntax is NOT supported.
    name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
    18

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

Trong JavaScript, hàm

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
19 đăng ký một hàm sẽ được gọi sau một số 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

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]

Trong JavaScript, hàm

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
20 đăng ký một hàm được gọi lặp lại sau mỗi vài mili giây. Ví dụ

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
0

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
1

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

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
2

Chủ đềJavaScriptPythondefine hàm có tên không đồng bộ
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
22
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
23xác định hàm ẩn danh không đồng bộ
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
24không được hỗ trợ cuộc gọi đồng bộ với
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
25
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
26
thường được bao bọc trong một khối
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
27
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
28cuộc gọi không đồng bộ với
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
29 và
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
30
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
31
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
32
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
33n/a

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

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
34

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
2

đầu ra là

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
3

Trong Trăn 3. 4, thư viện async đã đượ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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
4

các lớp học

TopicJavaScriptPythondefine
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
35
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
36inheritance
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
37
chỉ thừa kế duy nhất được hỗ trợ
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
38
đa thừa kế được hỗ trợconstructor
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
39
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
40khởi tạo [tạo cá thể]
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
41
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
42khai báo tài sản cá thểkhông được khai báo;
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
56cuộc gọi phương thức lớp/tĩnh
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
57
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
57 hoặc
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
59

Ngoài trình trang trí

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
55, Python cũng hỗ trợ trình trang trí
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
61. 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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
_5

đầu ra là

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
6

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
7

Đầ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 cá thể phải là

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
44

Đâ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ó

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
8

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
9

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

Often one of the languages does not have an equivalent function to the other, so the closest alternative is shown instead

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 Python Mô tả Tương đương với JavaScript gần nhất
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
63trả về giá trị tuyệt đối
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
64
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
65xác định xem tất cả các phần tử có phải là
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
28
trong ngữ cảnh Boolean
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
67
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
68 xác định xem có phần tử nào là
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
28
trong ngữ cảnh Boolean
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
70
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
71giống như
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
72, nhưng thoát khỏi các ký tự không phải ASCIIkhông được hỗ trợ
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
73chuyển đổi số nguyên thành chuỗi nhị phânkhông được hỗ trợ
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
74chuyển đổi giá trị thành Boolean
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
75
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
76phá vỡ thực thi và
rơi vào trình gỡ lỗi
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
77
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
78trả về một mảng mới bytesuse
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
79 với các lớp mảng đã nhập
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
80trả về một đối tượng
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
81
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
79 với các lớp mảng đã nhập
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
83xác định xem x có thể gọi được không [một hàm]
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
84
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
85trả về biểu diễn chuỗi
của một điểm mã Unicode_______277_______86
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
87biên dịch nguồn thành mã/đối tượng AST
mà có thể được chuyển đến
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
88 hoặc
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
89không được hỗ trợ
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
90tạo một số phức
từ các phần thực và ảokhông được hỗ trợ
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
91xóa thuộc tính khỏi đối tượng
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
92
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
93tạo từ điển
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
94 hoặc
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
95
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
96trả về danh sách các tên đã xác định
trong phạm vi hiện tại hoặc một đối tượng
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
97
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
98return tuple của thương và phần còn lại
của
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
99 chia cho
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
00
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
01 và
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
02
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
03trả về danh sách các bộ chứa mỗi bộ chứa
một chỉ mục và giá trị từ một lần lặp
trong đó hàm vị ngữ trả về true_______296_______10
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
11trả về số dấu phẩy động
được tạo từ một số hoặc chuỗi
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
12____296_______13trả về chuỗi được tạo bằng cách định dạng một giá trị
sử dụng một chuỗi định dạng sử dụng mẫu văn bản ____296_______14 trả về đối tượng
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
15 [bộ bất biến]
created from iterable
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
16
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
17returns attribute value
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
18
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
19returns dictionary containing the
bảng biểu tượng toàn cầu hiện tại không được hỗ trợ
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
20xác định xem đối tượng có thuộc tính đã cho
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
21
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
22trả về giá trị băm của đối tượngkhông được hỗ trợ
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
23gọi hệ thống trợ giúp Python tích hợp,
thông thường trong REPLkhông được hỗ trợ
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
24chuyển đổi số nguyên thành hex
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
25
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
26trả về danh tính của một đối tượngkhông được hỗ trợ
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
27đọc từ thiết bị xuất chuẩn với dấu nhắc tùy chọn sử dụng mô-đun Node
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
28
Phương thức
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
29
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
30trả về một số nguyên được tạo từ
một số hoặc chuỗi_______296_______31
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
32xác định xem một đối tượng có
một thể hiện của một lớp nhất định
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
33
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
34xác định xem
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
35 có phải là
một lớp con của
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
36
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
37
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
38 trả về một trình vòng lặp trên các phần tử
của một bộ sưu tập [xem
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
39]
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
40
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
41trả về số mục trong một bộ sưu tập
hoặc các ký tự trong một chuỗi
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
42
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
43xây dựng một danh sách từ một iterable
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
44
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
45trả về một từ điển chứa
bảng ký hiệu cục bộ hiện tại không được hỗ trợ
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
46trả về trình vòng lặp trên các giá trị được trả về bởi
chức năng gọi trên mỗi phần tử có thể lặp lại_______296_______47
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
48 hoặc
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
49trả về giá trị lớn nhất của đối số
hoặc trong một iterable
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
_50 hoặc
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
51
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
52 trả về một
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
53 cho một đối tượng
hỗ trợ giao thức bộ đệmkhông được hỗ trợ
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
54 hoặc
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
55trả về giá trị lớn nhất của đối số
hoặc trong một iterable
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
_56 hoặc
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
57
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
58 lấy mục tiếp theo từ trình vòng lặp
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
59
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
60 tạo một đối tượng trống, không có gì đặc biệt;
không thể thêm thuộc tính;
đọc, viết hoặc nối thêmxem mô-đun Node fs
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
65trả về điểm mã Unicode
đối với ký tự Unicode
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
66
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
67 hoặc
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
68cơ số trả về được nâng lên thành lũy thừa kinh nghiệm
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
67 hoặc
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
70_______296_______71in các giá trị biểu thức được phân tách bằng dấu cách
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
72
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
73trả về một thuộc tính thuộc tính đóng gói
nhận, thiết lập và xóa các chức năng không được hỗ trợ_______296_______74 hoặc
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
50trả về một đối tượng
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
49
đó là một chuỗi bất biến không được hỗ trợ
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
77trả về một chuỗi đại diện
của một đối tượng dành cho nhà phát triển
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
78
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
79trả về một trình vòng lặp để lặp lại
qua một chuỗi theo thứ tự đảo ngược không được hỗ trợ
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
80
trả về
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
32 hoặc
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
31 trả về một số được làm tròn thành
một số điểm thập phân
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
83
trả về chuỗi
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
84tạo đối tượng
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
58
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
57
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
87đặt thuộc tính của đối tượng
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
88
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
89 hoặc
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
90trả về một đối tượng
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
91 mà
mô tả một tập hợp các chỉ số;
được sử dụng để truy xuất dữ liệu tại các chỉ mục không được hỗ trợ
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
92trả về một phiên bản đã sắp xếp
của một iterable dưới dạng
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
45_______296_______94
sắp xếp tại chỗ
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
95trả về một chuỗi mà con người có thể đọc được
đại diện của một đối tượng
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
78
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
97trả về tổng của
số trong một iterable
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
98
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
99trả về một đối tượng proxy cho
gọi các phương thức siêu lớp
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
00 từ khóa
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
01tạo một tuple, tùy chọn
được điền từ một iterablekhông được hỗ trợ
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
02 trả về đối tượng lớp trả về loại giá trị
const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
83 trả về chuỗi
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
04 là hàm tạo_______298_______05trả về chế độ xem
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
54 của
các thuộc tính trong một đối tượng không được hỗ trợ_______298_______07trả về một trình vòng lặp tổng hợp
các phần tử từ nhiều lần lặp lại không được tích hợp sẵn;
có thể sử dụng chức năng Lodash
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
08 Chức năng tích hợp JavaScript Mô tả Tương đương với Python gần nhất
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
09giải mã một URL [ngược lại với
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
10]
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
11
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
12giải mã một thành phần của URI
[đối diện với
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
13]
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
11
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
15 mã hóa một URI, thay thế các ký tự nhất định
[không phải /, #, ?, =, và những thứ khác]
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
16
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
17mã hóa một thành phần của URI,
thay thế các ký tự nhất định
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
18
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
05thực hiện bất kỳ số dòng mã nào
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
07
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
21xác định nếu x là một số hữu hạn
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
22
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
23xác định x trong giá trị "không phải là số"
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
24
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
12trả về số dấu phẩy động
được tạo từ một số hoặc chuỗi
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
11
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
31trả về một số nguyên được tạo từ
một số hoặc chuỗi______296_______30

Các hàm JavaScript

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
29 và
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
30 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ụ,
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
31 trả về số
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
32. Các hàm
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
32 và
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
31 của Python không hỗ trợ điều này

Hoạt động Boolean

OperationJavaScriptPythonand
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
35
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
36or
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
37
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
38not
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
39
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
40bitwise and
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
41samebitwise or
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
42samebitwise not
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
43samebitwise xor
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
44same

Hoạt động số

OperationJavaScriptPythonbasic
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
45,
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
46,
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
15,
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
48sameexponentiation
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
49sameincrement
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
50 [pre] or
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
51 [post]
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
52decrement
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
53 [pre] or
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
54 [post]
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
55mod [remainder]
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
56sameconvert to string
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
57
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
58convert from string to integer
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
59 or
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
60
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
61convert from string to float
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
59 or
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
63
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
64convert to string with fixed decimals [ex. 2]
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
65
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
66convert to hex
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
25
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
24convert from hex
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
69
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
70constantssee
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
71 and
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
72 global objectssee
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
73 modulefunctionssee
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
71 and
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
72 global objectssee
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
73 module

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,

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
77

OperationJavaScriptPythonagiá trị tuyệt đối
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
64
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
79arc cosin
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
80
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
81arc sin
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
82
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
83arc tangent
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
84
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
85ceiling
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
86
# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
87closenot98 của các mục không cài sẵn_______82;

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

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
0

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 trình tự tại chỗ
  • và nhiều hơn nữa

Hoạt động chuỗi

OperationJavaScriptPythonliteral single line
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
34 or
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
35sameliteral multi-line
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
36
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
37 or
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
38length
    """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 Boolean
    """Return the average of a sequence of numbers."""
78 returns Booleanends with
    """Return the average of a sequence of numbers."""
79 returns Boolean
    """Return the average of a sequence of numbers."""
80 returns Booleancontains
    """Return the average of a sequence of numbers."""
81 returns Boolean
    """Return the average of a sequence of numbers."""
82 returns Booleanindex of
    """Return the average of a sequence of numbers."""
83 returns number
    """Return the average of a sequence of numbers."""
84 returns intlast index of
    """Return the average of a sequence of numbers."""
85 returns number
    """Return the average of a sequence of numbers."""
86 returns intcompare
    """Return the average of a sequence of numbers."""
87 returns - 1, 0, hoặc 1____0_______88
    """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ự,

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
45,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
47,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
49 và
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
58, 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

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
1

Để tạo một Python

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
45

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
2

Để tạo một Python

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
47

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
3

Để tạo một Python

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
49

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
4

Để tạo một Python ________29____58

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
5

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ụ

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
6

OperationJavaScriptPythonis array/sequence

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
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];
15
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 cuố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];
20
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];
21 để thêm một 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];
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
có thể bỏ qua kết thúc và bắt đầu và
có thể sử dụng chỉ số âm để đếm từ cuố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];
47
có thể bỏ qua bắt đầu và/hoặc kết thúc và
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_______9_______59 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];
60lặp qua đảo ngượclặp lại trên
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];
62lặp lại theo thứ tự đã sắp xếptạo một bản sao được sắp xếp và lặp lại trên nó
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];
63bao gồm [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];
65không bao gồm [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];
67chỉ mục của
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];
69chỉ mục cuối cùng của_______9_______70không được tích hợp sẵn;
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];
81một số/bất kỳ [Boolean]
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
70 - đoản mạch
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 - không đoản mạchmọi/tất cả [Boolean]
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
67 - đoản mạch
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 - phân loại không đoản mạch
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 là hàm so sánh hai phần 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];
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 là tên thuộc tính hoặc chức năng nhận
một phần tử và trả về một giá trị để sắp xếp ngược lại_______9_______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 - chỉ dành cho cấu trúc danh sách/giải nén
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
# biến bên trái có thể khác với # phần tử mả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];
93
# biến bên trái phải khớp với # phần tử dãy
giới hạn tính hữu ích

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

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
7

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 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 liên tục chuyển kết quả cho
// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
39 hoặc chuyển kết quả cho một hàm như
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
45 hoặc
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
58 để nhận tất cả các giá trị

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
8

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
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
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
01 để chuyển giá trị thành chuỗi. Ví dụ

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
9

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 này], nhưng không có hàm tạo tích hợp nào được cung cấp

Sorting

Suppose we have a sequence of objects that represent people, and we wish to sort the sequence by last name following by first name

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

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
0

Here is how this can be done in Python

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
1

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ụ

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
2

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

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
3

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

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
02. Ví dụ JavaScript ở trên có thể được triển khai như sau trong Python

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
4

bộ

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

OperationJavaScriptPythonis set
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
03
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
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
05 - cannot specify elements
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
06 or
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
07add
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
08sameremove
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
09
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
10remove all
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
11samelength
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
12
    """Return the average of a sequence of numbers."""
62includes
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
14
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
15iterate over
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
16
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
17convert to array/list
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
18
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
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ụ

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
5

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
53 để lưu trữ các liên kết giữa 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
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
53 có thể là bất kỳ loại nào

OperationJavaScript ObjectJavaScript Mapis object/dict
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
22
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
23create
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
24
có thể bao gồm các cặp khóa/giá trị ban đầu_______19_______25
không thể chỉ định cặp khóa/giá trị ban đầu, độ dài_______19_______26
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
27đặt giá trị của khóa
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
28 hoặc
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
29
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
30lấy giá trị của khóa
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
31 hoặc
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
32
sử dụng biểu mẫu thứ 2 nếu khóa chứa các ký tự đặc biệt_______19_______33lấy tất cả các khóa
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
97
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
35lấy tất cả các giá trị
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
36
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
37lấy tất cả các cặp
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
38
trả về một mảng các mảng chứa khóa và giá trị____19_______39
trả về cùng một bài kiểm tra nếu khóa hiện tại_______19_______40 hoặc
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
41_______19_______42xóa cặp
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
43 hoặc
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
44
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
45xóa tất cả các cặp
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
46
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
47lặp lại các khóa
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
50 hoặc
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
49_______19_______50

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
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
51create
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
52
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
_53
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
54độ dài
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
55đặt giá trị của khóa
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
56lấy giá trị của khóa
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
57 hoặc
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
58
lỗi nếu khóa không tồn tại tất cả các khóa_______19_______59 hoặc
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
60 hoặc
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
61 để lấy khóa theo thứ tự đã sắp xếplấy tất cả các giá trị
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
62lấy tất cả các cặp
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
63
trả về một chế độ xem trực tiếp cung cấp một
chuỗi các bộ chứa khóa và giá trịkiểm tra nếu có khóa
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
64kiểm tra nếu không có khóa
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
65xóa cặp
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
66xóa tất cả các cặp
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
67lặp lại
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
68

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

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
69 và
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
70 để 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

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
6

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

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
7

Trong Python, nhập mô-đun

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
71 để 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

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
71 hoặc trên một biểu thức chính quy được biên dịch

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
8

OperationJavaScriptPythoncreate

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
73 or
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
74_______19_______75
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
76test if a string matches
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
77
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
78
trả về một đối tượng khớp hoặc
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
65 nếu không khớp nhận kết quả khớp đầu tiên_______19_______80giống như trên nhận tất cả kết quả khớp
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
81 hoặc
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
82
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
83
trả về một chuỗi đối sánh có thể lặp lại đối sánh trên re
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
84
trả về một mảng các chuỗi
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
85
trả về một danh sách các chuỗi

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

  • import threading
     
    def my_function[p1, p2]:
        print['my_function: p1 =', p1, 'p2 =', p2]
     
    # The Timer function takes the number of seconds to wait,
    # the function to call, and a tuple of arguments to be passed.
    t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
    t.start[]
    if tired_of_waiting:
        t.cancel[]
    86 – trả về chuỗi phù hợp
  • import threading
     
    def my_function[p1, p2]:
        print['my_function: p1 =', p1, 'p2 =', p2]
     
    # The Timer function takes the number of seconds to wait,
    # the function to call, and a tuple of arguments to be passed.
    t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
    t.start[]
    if tired_of_waiting:
        t.cancel[]
    87 – trả về chỉ số bắt đầu của trận đấu [bao gồm]
  • import threading
     
    def my_function[p1, p2]:
        print['my_function: p1 =', p1, 'p2 =', p2]
     
    # The Timer function takes the number of seconds to wait,
    # the function to call, and a tuple of arguments to be passed.
    t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
    t.start[]
    if tired_of_waiting:
        t.cancel[]
    88 – trả về chỉ số kết thúc của trận đấu [độc quyền]
  • import threading
     
    def my_function[p1, p2]:
        print['my_function: p1 =', p1, 'p2 =', p2]
     
    # The Timer function takes the number of seconds to wait,
    # the function to call, and a tuple of arguments to be passed.
    t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
    t.start[]
    if tired_of_waiting:
        t.cancel[]
    89 – 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ệ

OperationJavaScriptPythonthrow
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
90
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
91catch
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
92
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
93rethrow
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
94
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
95

trong JavaScript

seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
9

Trong Python

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
0

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

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
96 trong khối
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
97

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à

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
98. 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

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
1

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

Cả hai chương trình JavaScript chạy trên 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

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
99 của đối tượng toàn cầu
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
000 được sử dụng

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
2

Trong Python, phương thức

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
99 của mô-đun
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
002 được sử dụng

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
3

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
003

OperationJavaScriptPythoncreate
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
004
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
005parse
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
006
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
007

Viết và đọc tệp

Nút. js and Python can both read and write files containing text or binary data

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. One way to satisfy this is to give the file a

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
4 file extension

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
4

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

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
5

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

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
6

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

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
7

Lệnh Shell

JavaScript code running in 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 hiện lệnh

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
009 để 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"

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
8

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

import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
9

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

// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
0

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

  • import sys
     
    name = sys.argv[1] if len[sys.argv] > 1 else 'World'
     
    # This is a nice alternative.
    name = sys.argv[1] or 'World'
     
    # This syntax is NOT supported.
    name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
    61 Đ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 một đối tượng lớp làm tham số đầu tiên của nó và có thể sử dụng nó để truy cập trạng thái lớp
  • from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    011 Điều này định nghĩa các phương thức getter, setter và delete cho một thuộc tính thể hiện của lớp
  • import sys
     
    name = sys.argv[1] if len[sys.argv] > 1 else 'World'
     
    # This is a nice alternative.
    name = sys.argv[1] or 'World'
     
    # This syntax is NOT supported.
    name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
    55 Đ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, xem

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ỉ được 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

const arr = [1, 2, 3, 4];
[a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
76 sau

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
014

Trong Python, sử dụng

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
015

Máy chủ HTTP

Máy chủ HTTP có thể được triển khai trong cả hai Nút. 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

  • lưu 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
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    016 bằng cách trả lại tất cả các con chó dưới dạng JSON
  • xử lý yêu cầu
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    017 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
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    018 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
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    019 bằng cách xóa con chó có id đã cho

JavaScript Express REST Server

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

  2. Create a

    const arr = [1, 2, 3, 4];
    [a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
    10 file for the project by entering
    const arr = [1, 2, 3, 4];
    [a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
    11 and answering the questions it asks

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

    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    022 và
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    023

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

    const arr = [1, 2, 3, 4];
    [a, b, ...rest] = arr; // a=1, b=2, rest=[3, 4]
    10 bằng
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    025. Lệnh
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    026 cung cấp chế độ xem tệp tự động và khởi động lại máy chủ

  5. Tạo tệp

    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    027 chứa thông tin sau

// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
1

  1. Chạy máy chủ bằng cách nhập
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    028

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
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    029. Flask-CORS hỗ trợ bật CORS trên máy chủ Flask
  2. Nếu chạy trong môi trường UNIX, hãy tạo tệp script
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    030 đượ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
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    031

// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
2

  1. Đặt
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    032 thành
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    033 cung cấp chế độ xem tệp tự động và khởi động lại máy chủ
  2. If running in Windows, create a similar
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    034 file
  1. Tạo tệp
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    035 chứa thông tin sau

// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
3

  1. Chạy máy chủ bằng cách nhập
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    036

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
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    037
  2. Tạo tệp
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    035 chứa thông tin sau

// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
4

  1. Chạy máy chủ bằng cách nhập
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    039. Bao gồm tùy chọn
    from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    040 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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
041 với
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
042

Đâ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

// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
5

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
043 để gửi các yêu cầu HTTP. Nó có thể được cài đặt bằng cách nhập
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
044

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

// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
6

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

MethodParametersPurposeObject Lifecycle
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
045cls,. tạo một đối tượng mới
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
046self,. khởi tạo một đối tượng mới
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
047selfhủy một đối tượng
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
048self, tên có thể được sử dụng để triển khai mẫu "phương thức bị thiếu"
[see example below]String Representation
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
049selfreturns a string representations useful to developers
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
050selfreturns a string representation useful to usersComparisons
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
051self, otherdetermines if this object is equal to another
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
052self, otherdetermines if this object is not equal to another
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
053self, otherdetermines if this object is < another
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
054self, otherdetermines if this object is another
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
056self, otherdetermines if this object is >= to anotheralso see
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
057List-like Operations
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
058self, keygets an item from a list by index
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
059self, key, valuesets an item in a list by index
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
060self, keydeletes an item from a list by index
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
061selfreturns an iterator
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
062self, itemdetermines if a given item is containedDict Operations
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
063self, keyreturns value to use when key is not present
class must inherit from
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
54Math Operations
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
065self, otheradds an object to another
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
066self, othersubtracts an object from another
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
067self, othermultiplies an object by another
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
068self, otherdivides an object by another
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
069self, othermods an object by anotherPickling [serialization]
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
070selfpickles an object
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
071selfunpickles an objectOther
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
072self,. xử lý một đối tượng như một chức năng;

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

// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
7

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
073 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. "

// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
8

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
07 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ợ

Kiểu TênÝ nghĩa
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
075Boolean
import sys
 
name = sys.argv[1] if len[sys.argv] > 1 else 'World'
 
# This is a nice alternative.
name = sys.argv[1] or 'World'
 
# This syntax is NOT supported.
name = len[sys.argv] > 1 ? sys.argv[1] : 'World'
81dãy số nguyên bất biến từ 0 đến 255
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
33số phức với
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
32 phần thực và phần ảo
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
32số dấu phẩy động chính xác kép
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
31số nguyên chính xác không giới hạn
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
01chuỗi

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

Loại TênÝ nghĩa
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
082dict với các khóa thuộc loại KT và các giá trị thuộc loại VT_______29_______083danh sách có các phần tử thuộc loại T
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
084bất kỳ loại dãy nào có tất cả các phần tử thuộc loại T
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
085bộ có các phần tử thuộc loại T
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
086bộ có các phần tử được chỉ định loại

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

Loại Tên Ý nghĩa_______29_______087bấ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
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
088hàm nhận tham số của các loại P1, P2,. và trả về hàm loại RT_______29_______089 nhận bất kỳ tham số nào và trả về hàm trình tạo loại RT_______29_______090;
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
091 và
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
092 có thể là
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
65
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
094named Tuple trong đó các phần tử có kiểu T1, T2,.
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
095matches
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
65 or the type T
tương tự như
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
097_______29_______098khớp với một đối tượng lớp cho lớp C hoặc lớp con
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
099khớ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ụ,

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
100. 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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
101 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 cùng một loại mô tả được sử dụng ở nhiều nơi. Ví dụ,

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
102

Để 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 danh sách đối số trong ngoặc đơn bên phải với

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
103 và kiểu

Ví dụ, nếu

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
104 là một lớp chúng ta đã định nghĩa

// Named function
function myFn[p1, p2, ...] {
  body
}
 
// Anonymous function [a.k.a. arrow function]
const myFn = [p1, p2, ...] => {
  body
};
9

Mô-đun

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
105 cũng xác định hàm
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
106. Đây là một ví dụ về việc sử dụng nó

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
0

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
107. Trên máy Mac, hãy thêm thư mục sau vào biến môi trường
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
108.
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
109

Để 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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
110

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

pyrit

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
111

Để 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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
112. Để chạy ở chế độ đồng hồ, hãy nhập
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
113. Xem vấn đề này

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
114 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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
115 thay vì trực tiếp trong tệp
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
5

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 để biết 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 cò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 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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
115 có cùng tên với tệp
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
5 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
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
119 để 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

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
5 tương ứng

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
121

Để 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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
122. Lưu ý rằng cú pháp
seq = [1, 2, 3, 4] # using a tuple, but could also be a list
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
a, *rest, b = seq # a=1, rest=[2, 3], b=4
a, b, *rest = seq # a=1, b=2, rest=[3, 4]
10 là thực tế

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

import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
5 bằng cách sử dụng lệnh
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
125 đượ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à
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
126, nhưng điều này có thể được thay đổi bằng cách sử dụng tùy chọn
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
127. Ví dụ,
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
128 tạo ra
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
129 trong thư mục hiện tại. Nhiều loại sẽ là
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
087, 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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
131 bỏ qua các gợi ý về loại

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
1

Here is a stub file in

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
129 that provides type hints

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
2

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

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
3

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
133

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
4

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

Chủ đềJavaScriptPythondòng lệnhNode. js
import threading
 
def my_function[p1, p2]:
    print['my_function: p1 =', p1, 'p2 =', p2]
 
# The Timer function takes the number of seconds to wait,
# the function to call, and a tuple of arguments to be passed.
t = threading.Timer[1.0, my_function, ['arg1', 'arg2']]
t.start[]
if tired_of_waiting:
    t.cancel[]
9 command
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
07 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 toàn diện mũi2Cypresssamemathmathjsmath [trong thư viện chuẩn] trình soạn thảo phổ biếnVS CodeVS Code, PyCharm [JetBrains]

vải lót

Để lint JavaScript bằng ESLint

  • from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    136
  • from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    137

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
138 trong thư mục dự án hoặc trong thư mục chính của người dùng

Ví dụ

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
_5

Để lint mã Python bằng pylint

  • from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    139
  • from calendar import day_name
    from datetime import date
     
    def get_day[]:
        index = date.today[].weekday[]
        return day_name[index]
     
    if [day := get_day[]] == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
     
    print['day =', day] # Tuesday
     
    # Typically the code is easier to read when
    # the walrus operator is not used.  For example:
    day = get_day[]
    if day == 'Tuesday':
        print['Tacos!'] # Tacos! if it's a Tuesday
    140

Để đị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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
141 hoặc một tệp trong thư mục chính của người dùng [_______29_______142 hoặc
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
143]. Để 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
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
144. Tập tin này sau đó có thể được sửa đổi hoặc chỉ được sử dụng để tham khảo. Ví dụ

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
6

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ụ

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
7

Để 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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
145 hoặc một tệp trong thư mục chính của người dùng [
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
146 cho Windows và
from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
147 cho macOS và Linux]. Ví dụ

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
8

đị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

# Named function
def myFn:
  body
 
# Lambda function
lambda args: expression
9

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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
148

Để 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

from calendar import day_name
from datetime import date
 
def get_day[]:
    index = date.today[].weekday[]
    return day_name[index]
 
if [day := get_day[]] == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
 
print['day =', day] # Tuesday
 
# Typically the code is easier to read when
# the walrus operator is not used.  For example:
day = get_day[]
if day == 'Tuesday':
    print['Tacos!'] # Tacos! if it's a Tuesday
149

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. "

Tôi có thể sử dụng JS với Python không?

JS2PY hoạt động bằng cách dịch trực tiếp JavaScript sang Python . Nó chỉ ra rằng bạn có thể chạy JS trực tiếp từ mã Python mà không cần cài đặt các công cụ lớn bên ngoài như V8. Để sử dụng mô-đun, trước tiên mô-đun phải được cài đặt vào hệ thống, vì mô-đun không được tích hợp sẵn.

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

Tập hợp con "dựa trên đối tượng" của Python gần tương đương với JavaScript . Like JavaScript [and unlike Java], Python supports a programming style that uses simple functions and variables without engaging in class definitions. Tuy nhiên, đối với JavaScript, đó là tất cả.

Is Python good with JavaScript?

JavaScript tốt hơn cho việc phát triển trang web . Additionally, using Python to build a website is a complicated process. Các khung tồn tại để thực hiện nhiệm vụ dễ dàng hơn – như Django và Flask – nhưng ngay cả như vậy, sử dụng Python không phải là tối ưu.

Cái nào tốt hơn JS hay Python?

You can start with Javascript, which is used for front-end programming, if you are a newbie web programmer. Mặt khác, Python là lựa chọn tốt nhất nếu bạn muốn học một ngôn ngữ chủ yếu để lập trình back-end

Chủ Đề