Hướng dẫn what is this := in python? - cái gì thế này: = trong python?

Cập nhật câu trả lời

Trong bối cảnh của câu hỏi, chúng tôi đang xử lý mã giả, nhưng bắt đầu trong Python 3.8,

procedure fizzbuzz
For i := 1 to 100 do
    set print_number to true;
    If i is divisible by 3 then
        print "Fizz";
        set print_number to false;
    If i is divisible by 5 then
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
end
4 thực sự là một toán tử hợp lệ cho phép gán các biến trong các biểu thức:

# Handle a matched regex
if [match := pattern.search[data]] is not None:
    # Do something with match

# A loop that can't be trivially rewritten using 2-arg iter[]
while chunk := file.read[8192]:
   process[chunk]

# Reuse a value that's expensive to compute
[y := f[x], y**2, y**3]

# Share a subexpression between a comprehension filter clause and its output
filtered_data = [y for x in data if [y := f[x]] is not None]

Xem PEP 572 để biết thêm chi tiết.

Câu trả lời ban đầu

Những gì bạn đã tìm thấy là mã giảpseudocode

Mã giả là một mô tả cấp cao không chính thức về nguyên tắc hoạt động của chương trình máy tính hoặc thuật toán khác. is an informal high-level description of the operating principle of a computer program or other algorithm.

procedure fizzbuzz
For i := 1 to 100 do
    set print_number to true;
    If i is divisible by 3 then
        print "Fizz";
        set print_number to false;
    If i is divisible by 5 then
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
end
4 thực sự là toán tử chuyển nhượng. Trong Python, điều này chỉ đơn giản là
procedure fizzbuzz
For i := 1 to 100 do
    set print_number to true;
    If i is divisible by 3 then
        print "Fizz";
        set print_number to false;
    If i is divisible by 5 then
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
end
6.

Để dịch mã giả này thành Python, bạn sẽ cần biết các cấu trúc dữ liệu được tham chiếu và thêm một chút về việc triển khai thuật toán.

Một số ghi chú về psuedocode: