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:

  • 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 là toán tử chuyển nhượng hoặc
    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 trong Python
  • 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 là toán tử bình đẳng hoặc
    void function fizzbuzz
    For (i = 1; i <= 100; i++) {
        set print_number to true;
        If i is divisible by 3
            print "Fizz";
            set print_number to false;
        If i is divisible by 5
            print "Buzz";
            set print_number to false;
        If print_number, print i;
        print a newline;
    }
    
    0 trong Python
  • Có một số kiểu nhất định và số dặm của bạn có thể thay đổi:

Pascal-style

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

C-style

void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}

Lưu ý sự khác biệt trong sử dụng nẹp và toán tử chuyển nhượng.

Nguồn: Unplash

Các nhà điều hành Walrus

Rất gần đây, Python 3.8 đã giới thiệu việc sử dụng ‘đại tràng bằng (

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), tương tự như toán tử bằng (
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). Việc sử dụng toán tử này cho phép tăng tốc và rút ngắn mã, và nó chắc chắn có giá trị để hiểu.

Ký hiệu này xuất phát từ một khiếu nại bắt nguồn từ toán học. Khi chúng ta viết phương trình trong toán học, chúng ta có thể viết

Các nhà khai thác trong Python là gì?

Các nhà khai thác là các biểu tượng đặc biệt trong Python thực hiện tính toán số học hoặc logic. Giá trị mà toán tử hoạt động được gọi là toán hạng.

Ví dụ:

>>> 2+3
5

Ở đây,

void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
3 là nhà điều hành thực hiện bổ sung.
void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
4 và
void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
5 là các toán hạng và
void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
6 là đầu ra của hoạt động.


Toán tử số học

Các toán tử số học được sử dụng để thực hiện các hoạt động toán học như bổ sung, trừ, nhân, v.v.

Nhà điều hànhNghĩaThí dụ
+Thêm hai toán hạng hoặc Unary Plusx + y + 2
-Trừ toán hạng bên phải từ bên trái hoặc không trừx - y- 2
*Nhân hai toán hạngx * y
/Chia toán hạng bên trái cho một bên phải (luôn luôn dẫn thành float)x / y
Phần trămMô đun - Phần còn lại của bộ phận của Vật lý bên trái bên phảix % y (phần còn lại của x/y)
//Phân chia sàn - Bộ phận kết quả thành toàn bộ số được điều chỉnh ở bên trái trong dòng sốx // y
**Số mũ - toán hạng bên trái được nâng lên sức mạnh của bên phảix ** y (x cho sức mạnh y)

Ví dụ 1: Người vận hành số học trong Python

x = 15
y = 4

# Output: x + y = 19
print('x + y =',x+y)

# Output: x - y = 11
print('x - y =',x-y)

# Output: x * y = 60
print('x * y =',x*y)

# Output: x / y = 3.75
print('x / y =',x/y)

# Output: x // y = 3
print('x // y =',x//y)

# Output: x ** y = 50625
print('x ** y =',x**y)

Đầu ra

x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625

Toán tử so sánh

Các toán tử so sánh được sử dụng để so sánh các giá trị. Nó trả về

void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
7 hoặc
void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
8 theo điều kiện.

Nhà điều hànhNghĩaThí dụ
+Thêm hai toán hạng hoặc Unary Plusx + y + 2
-Trừ toán hạng bên phải từ bên trái hoặc không trừx - y- 2
*Nhân hai toán hạngx * y
/Chia toán hạng bên trái cho một bên phải (luôn luôn dẫn thành float)x / y
Phần trămMô đun - Phần còn lại của bộ phận của Vật lý bên trái bên phảix % y (phần còn lại của x/y)
//Phân chia sàn - Bộ phận kết quả thành toàn bộ số được điều chỉnh ở bên trái trong dòng sốx // y

**

x = 10
y = 12

# Output: x > y is False
print('x > y is',x>y)

# Output: x < y is True
print('x < y is',x= y is False
print('x >= y is',x>=y)

# Output: x <= y is True
print('x <= y is',x<=y)

Đầu ra

x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True

Toán tử so sánh

Các toán tử so sánh được sử dụng để so sánh các giá trị. Nó trả về

void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
7 hoặc
void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
8 theo điều kiện.

Nhà điều hànhNghĩaThí dụ
+Thêm hai toán hạng hoặc Unary Plusx + y + 2
-Trừ toán hạng bên phải từ bên trái hoặc không trừx - y- 2
*Nhân hai toán hạngx * y

/

x = True
y = False

print('x and y is',x and y)

print('x or y is',x or y)

print('not x is',not x)

Đầu ra

x and y is False
x or y is True
not x is False

Toán tử so sánh


Các toán tử so sánh được sử dụng để so sánh các giá trị. Nó trả về void function fizzbuzz For (i = 1; i <= 100; i++) { set print_number to true; If i is divisible by 3 print "Fizz"; set print_number to false; If i is divisible by 5 print "Buzz"; set print_number to false; If print_number, print i; print a newline; } 7 hoặc void function fizzbuzz For (i = 1; i <= 100; i++) { set print_number to true; If i is divisible by 3 print "Fizz"; set print_number to false; If i is divisible by 5 print "Buzz"; set print_number to false; If print_number, print i; print a newline; } 8 theo điều kiện.

>

Lớn hơn - Đúng nếu toán hạng bên trái lớn hơn bên phải

x> y Let x = 10 (

>>> 2+3
5
4 in binary) and y = 4 (
>>> 2+3
5
5 in binary)

Nhà điều hànhNghĩaThí dụ
+Thêm hai toán hạng hoặc Unary Plusx + y + 2
-Trừ toán hạng bên phải từ bên trái hoặc không trừx - y- 2
*Nhân hai toán hạngx * y
/Chia toán hạng bên trái cho một bên phải (luôn luôn dẫn thành float)x / y
Phần trămMô đun - Phần còn lại của bộ phận của Vật lý bên trái bên phảix % y (phần còn lại của x/y)
-Trừ toán hạng bên phải từ bên trái hoặc không trừx - y- 2

*

Nhân hai toán hạng

x * y

/

Nhà điều hànhThí dụChia toán hạng bên trái cho một bên phải (luôn luôn dẫn thành float)
x / yPhần trămPhần trăm
Mô đun - Phần còn lại của bộ phận của Vật lý bên trái bên phảix % y (phần còn lại của x/y)//
Phân chia sàn - Bộ phận kết quả thành toàn bộ số được điều chỉnh ở bên trái trong dòng sốx // y**
Số mũ - toán hạng bên trái được nâng lên sức mạnh của bên phảix ** y (x cho sức mạnh y)Ví dụ 1: Người vận hành số học trong Python
Đầu raToán tử so sánhCác toán tử so sánh được sử dụng để so sánh các giá trị. Nó trả về
void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
7 hoặc
void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
8 theo điều kiện.
>Lớn hơn - Đúng nếu toán hạng bên trái lớn hơn bên phảix> y
<Ít hơn - Đúng nếu toán hạng bên trái ít hơn bên phảix
==Bằng với - true nếu cả hai toán hạng đều bằng nhaux == y
! =Không bằng - true nếu toán hạng không bằngX! = Y
> =Lớn hơn hoặc bằng - true nếu toán hạng bên trái lớn hơn hoặc bằng bên phảix> = y
Bitwise hoặcNhỏ hơn hoặc bằng - true nếu toán hạng bên trái nhỏ hơn hoặc bằng bên phảix
Ví dụ 2: Các toán tử so sánh trong PythonToán tử logicCác toán tử logic là các toán tử
void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
9,
>>> 2+3
5
0,
>>> 2+3
5
1.
//x // y**

Số mũ - toán hạng bên trái được nâng lên sức mạnh của bên phải

x ** y (x cho sức mạnh y)

Ví dụ 1: Người vận hành số học trong Python

Đầu ra

Nhà điều hànhNghĩaThí dụ
Toán tử so sánhCác toán tử so sánh được sử dụng để so sánh các giá trị. Nó trả về
void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
7 hoặc
void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
8 theo điều kiện.
>
Lớn hơn - Đúng nếu toán hạng bên trái lớn hơn bên phảix> y<

Ít hơn - Đúng nếu toán hạng bên trái ít hơn bên phải

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
0

Đầu ra

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
1

Toán tử so sánh

Các toán tử so sánh được sử dụng để so sánh các giá trị. Nó trả về

void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
7 hoặc
void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
8 theo điều kiện.


>

Lớn hơn - Đúng nếu toán hạng bên trái lớn hơn bên phải

x> y

Nhà điều hànhNghĩaThí dụ
<Ít hơn - Đúng nếu toán hạng bên trái ít hơn bên phải5 trong x
không phải vàoĐúng nếu giá trị/biến không được tìm thấy trong chuỗi5 không ở x

Ví dụ #5: Các nhà khai thác thành viên trong Python

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
2

Đầu ra

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
3

Ở đây,

x = 15
y = 4

# Output: x + y = 19
print('x + y =',x+y)

# Output: x - y = 11
print('x - y =',x-y)

# Output: x * y = 60
print('x * y =',x*y)

# Output: x / y = 3.75
print('x / y =',x/y)

# Output: x // y = 3
print('x // y =',x//y)

# Output: x ** y = 50625
print('x ** y =',x**y)
9 nằm trong X nhưng
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625
0 không có trong X (hãy nhớ, Python nhạy cảm trường hợp). Tương tự,
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625
1 là chìa khóa và
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625
2 là giá trị trong từ điển y. Do đó,
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625
3 trả về
void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}
8.

: = Toán tử làm gì?

Trong tất cả các ngôn ngữ hỗ trợ toán tử: = nó có nghĩa là gán.Trong các ngôn ngữ hỗ trợ toán tử: =, toán tử = toán tử thường có nghĩa là so sánh bình đẳng.Trong các ngôn ngữ trong đó = có nghĩa là gán, == thường được sử dụng để so sánh bình đẳng.

Có gì & = nghĩa là trong Python?

Nó có nghĩa là bitwise và hoạt động.Ví dụ: x = 5 x & = 3 #which giống với x = x & 3 in (x)bitwise AND operation. Example : x = 5 x &= 3 #which is similar to x = x & 3 print(x)

Đại tá có nghĩa là gì?

Bảng tính.Add After: = ActiveSheet, đếm: = 5.Vì vậy, để tổng hợp nó, sự kết hợp bằng nhau đại trào: = được sử dụng để đặt giá trị của tham số (đối số) cho một thuộc tính hoặc phương thức.Dấu hiệu bằng nhau được sử dụng để đặt giá trị của thuộc tính, đối tượng, biến hoặc làm toán tử so sánh.Tôi hy vọng điều đó sẽ giúp.used to set the value of a parameter (argument) for a property or method. The equal sign is use to set the value of a property, object, variable, or as a comparison operator. I hope that helps.