Giả sử biến A giữ giá trị 10 và biến B giữ giá trị 20, sau đó -a holds the value 10 and variable b holds the value 20, then −
= | Gán các giá trị từ các toán hạng bên phải sang bên trái bên trái | c = a + b gán giá trị của a + b vào c |
+= Thêm và | Nó thêm toán hạng bên phải vào toán hạng bên trái và gán kết quả cho toán hạng bên trái | C + = A tương đương với C = C + A |
-= trừ và | Nó trừ đi toán hạng bên phải từ toán hạng bên trái và gán kết quả cho toán hạng bên trái | c - = a tương đương với c = c - a |
*= Nhân và | Nó nhân lên bên phải với toán hạng bên trái và gán kết quả cho toán hạng bên trái | c * = a tương đương với c = c * a |
/= Chia và | Nó chia toán hạng bên trái với toán hạng bên phải và gán kết quả cho toán hạng bên trái | C / = A tương đương với c = c / ac / = a tương đương với c = c / a |
%= Mô đun và | Nó lấy mô đun bằng hai toán hạng và gán kết quả cho toán hạng bên trái | C % = A tương đương với c = c % a |
** = số mũ và | Thực hiện tính toán theo cấp số nhân [nguồn] trên các toán tử và gán giá trị cho toán hạng bên trái | c ** = a tương đương với c = c ** a |
// = phân chia sàn | Nó thực hiện phân chia sàn trên các nhà khai thác và gán giá trị cho toán hạng bên trái | c // = a tương đương với c = c // a |
Thí dụ
Giả sử biến A giữ giá trị 10 và biến B giữ giá trị 20, sau đó -a holds the value 10 and variable b holds the value 20, then −
#!/usr/bin/python3 a = 21 b = 10 c = 0 c = a + b print ["Line 1 - Value of c is ", c] c += a print ["Line 2 - Value of c is ", c ] c *= a print ["Line 3 - Value of c is ", c ] c /= a print ["Line 4 - Value of c is ", c ] c = 2 c %= a print ["Line 5 - Value of c is ", c] c **= a print ["Line 6 - Value of c is ", c] c //= a print ["Line 7 - Value of c is ", c]
=
Gán các giá trị từ các toán hạng bên phải sang bên trái bên trái
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 99864
python_basic_operators.htm
Toán tử là các biểu tượng đặc biệt thực hiện các hoạt động trên các biến và giá trị. Ví dụ,
print[5 + 6] # 11
Ở đây,
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998646 là một toán tử thêm hai số: 5 và 6.5 and 6.
Các loại người vận hành python
Dưới đây là danh sách các loại nhà khai thác python khác nhau mà chúng ta sẽ học trong hướng dẫn này.
- Toán tử số học
- Toán tử chuyển nhượng
- Toán tử so sánh
- Toán tử logic
- Các nhà khai thác bitwise
- Các nhà khai thác đặc biệt
1. Các toán tử số học Python
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.
sub = 10 - 5 # 5
Ở đây,
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998647 là một toán tử số học trừ hai giá trị hoặc biến.
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998646 | Phép cộng | Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998649 |
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998647 | Phép trừ | 1 |
2 | Phép nhân | 3 |
4 | Phân công | 5 |
6 | Modulo | 7 |
8 | Quyền lực | 9 |
Ví dụ 1: Người vận hành số học trong Python
a = 7
b = 2
# addition
print ['Sum: ', a + b]
# subtraction
print ['Subtraction: ', a - b]
# multiplication
print ['Multiplication: ', a * b]
# division
print ['Division: ', a / b]
# modulo
print ['Modulo: ', a % b]
# a to the power b
print ['Power: ', a ** b]
Đầu ra
Sum: 9 Subtraction: 5 Multiplication: 14 Division: 3.5 Modulo: 1 Power: 49
Trong ví dụ trên, chúng tôi đã sử dụng nhiều toán tử số học,
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 99864
6 để thêm
1 vàsub = 10 - 5 # 5
2sub = 10 - 5 # 5
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 99864
7 để trừ
2 từsub = 10 - 5 # 5
1sub = 10 - 5 # 5
2 với nhânprint[5 + 6] # 11
1 vàsub = 10 - 5 # 5
2sub = 10 - 5 # 5
4 để chiaprint[5 + 6] # 11
1 chosub = 10 - 5 # 5
2sub = 10 - 5 # 5
6 để có được phần còn lạiprint[5 + 6] # 11
8 để có đượcprint[5 + 6] # 11
1 cho sức mạnhsub = 10 - 5 # 5
2sub = 10 - 5 # 5
2. Các nhà khai thác phân công Python
Các toán tử gán được sử dụng để gán các giá trị cho các biến. Ví dụ,
# assign 5 to x
var x = 5
Ở đây,
a = 7
b = 2
# addition
print ['Sum: ', a + b]
# subtraction
print ['Subtraction: ', a - b]
# multiplication
print ['Multiplication: ', a * b]
# division
print ['Division: ', a / b]
# modulo
print ['Modulo: ', a % b]
# a to the power b
print ['Power: ', a ** b]
6 là một toán tử gán gán a = 7
b = 2
# addition
print ['Sum: ', a + b]
# subtraction
print ['Subtraction: ', a - b]
# multiplication
print ['Multiplication: ', a * b]
# division
print ['Division: ', a / b]
# modulo
print ['Modulo: ', a % b]
# a to the power b
print ['Power: ', a ** b]
7 cho a = 7
b = 2
# addition
print ['Sum: ', a + b]
# subtraction
print ['Subtraction: ', a - b]
# multiplication
print ['Multiplication: ', a * b]
# division
print ['Division: ', a / b]
# modulo
print ['Modulo: ', a % b]
# a to the power b
print ['Power: ', a ** b]
8.Dưới đây là danh sách các toán tử chuyển nhượng khác nhau có sẵn trong Python.
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998646 | Phép cộng | Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998649 |
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998647 | Phép trừ | 1 |
2 | Phép nhân | 3 |
4 | Phân công | 5 |
6 | Modulo | 7 |
8 | Quyền lực | 9 |
Ví dụ 1: Người vận hành số học trong Python | Đầu ra | Trong ví dụ trên, chúng tôi đã sử dụng nhiều toán tử số học, |
Line 1 - Value of c is 31
Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c is 52.0
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152
Line 7 - Value of c is 99864
6 để thêm sub = 10 - 5 # 5
1 và sub = 10 - 5 # 5
2
sub = 10 - 5 # 5
sub = 10 - 5 # 5
# assign 10 to a
a = 10
# assign 5 to b
b = 5
# assign the sum of a and b to a
a += b # a = a + b
print[a]
# Output: 15
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998647 để trừ
sub = 10 - 5 # 5
2 từ sub = 10 - 5 # 5
1print[5 + 6] # 11
2 với nhân sub = 10 - 5 # 5
1 và sub = 10 - 5 # 5
2print[5 + 6] # 11
4 để chia sub = 10 - 5 # 5
1 cho sub = 10 - 5 # 5
2
print[5 + 6] # 11
sub = 10 - 5 # 5
sub = 10 - 5 # 5
print[5 + 6] # 11
6 để có được phần còn lạia = 5
b =2
print [a > b] # True
print[5 + 6] # 11
8 để có được sub = 10 - 5 # 5
1 cho sức mạnh sub = 10 - 5 # 5
2
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998646 | Phép cộng | Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998649False |
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998647 | Phép trừ | 1True
|
2 | Phép nhân | 3False
|
4 | Phân công | 5True
|
6 | Modulo | 7False
|
8 | Quyền lực | 9True
|
Ví dụ 1: Người vận hành số học trong Python
a = 5
b = 2
# equal to operator
print['a == b =', a == b]
# not equal to operator
print['a != b =', a != b]
# greater than operator
print['a > b =', a > b]
# less than operator
print['a < b =', a < b]
# greater than or equal to operator
print['a >= b =', a >= b]
# less than or equal to operator
print['a b] # True
7 and a = 5
b =2
print [a > b] # True
8 are # assign 5 to x
var x = 5
7, the result is # assign 5 to x
var x = 5
7.
Thí dụ | Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998646and b | Phép cộng: 7 only if both the operands are 7
|
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998649 | Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998647or b | Phép trừ: 7 if at least one of the operands is 7
|
1 | 2 a
| Phép nhân: 7 if the operand is 8 and vice-versa.
|
print[5 + 6] # 11
3
print[5 + 6] # 11
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998642
4: Here is the truth table for these logical operators.print[5 + 6] # 11
Phân công
print[5 + 6] # 11
5print[5 + 6] # 11
62 is Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986400 in binary and 7 is
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986401.
Modulo Let x = 10 [
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986402 in binary] and y = 4 [
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986403 in binary]
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998646 | Phép cộng | Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998649 |
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998647 | Phép trừ | 1 |
2 | Phép nhân | 3 |
4 | Phân công | 5 |
6 | Modulo | 7 |
8 | Quyền lực |
print[5 + 6] # 11
9
print[5 + 6] # 11
Ví dụ 1: Người vận hành số học trong Pythonidentity operator and the membership operator. They are described below with examples.
Đầu ra
Trong ví dụ trên, chúng tôi đã sử dụng nhiều toán tử số học,
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998646 | Phép cộng | Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986413 |
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986410 | 7 Nếu các toán hạng không giống nhau [không tham khảo cùng một đối tượng] | Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986416 |
Ví dụ 4: Người vận hành danh tính trong Python
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998643
Ở đây, chúng ta thấy rằng X1 và Y1 là số nguyên của cùng một giá trị, vì vậy chúng bằng nhau cũng như giống hệt nhau. Tương tự là trường hợp với x2 và y2 [chuỗi].
Nhưng x3 và y3 là danh sách. Họ bằng nhau nhưng không giống nhau. Đó là bởi vì trình thông dịch định vị chúng riêng biệt trong bộ nhớ mặc dù chúng bằng nhau.
Các nhà khai thác thành viên
Trong Python,
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986417 và
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986418 là các nhà khai thác thành viên. Chúng được sử dụng để kiểm tra xem một giá trị hoặc biến được tìm thấy trong một chuỗi [chuỗi, danh sách, tuple, bộ và từ điển].
Trong một từ điển, chúng ta chỉ có thể kiểm tra sự hiện diện của khóa, không phải giá trị.
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986417 | 7 Nếu giá trị/biến được tìm thấy trong chuỗifound in the sequence
| Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986421 |
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986418 | 7 Nếu giá trị/biến không được tìm thấy trong chuỗinot found in the sequence
| Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986424 |
Ví dụ 5: Các nhà khai thác thành viên trong Python
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998644
Đầu ra
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 998645
Ở đây,
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986425 nằm trong x nhưng
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986426 không có trong X [hãy nhớ, Python là trường hợp nhạy cảm].
Tương tự,
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986427 là chìa khóa và
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986428 là giá trị trong từ điển y. Do đó,
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52.0 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 9986429 trả về
# assign 5 to x
var x = 5
8.