Việc sử dụng toán tử % trong Python là gì?

Toán tử Python nói chung được sử dụng để thực hiện các thao tác trên các giá trị và biến. Đây là những ký hiệu tiêu chuẩn được sử dụng cho mục đích của các hoạt động logic và số học. Trong bài viết này, chúng ta sẽ xem xét các loại toán tử Python khác nhau.  

  • NGƯỜI ĐIỀU HÀNH. Là những ký hiệu đặc biệt. Ví dụ: + , * , /, v.v.
  • TOÁN TỬ. Đó là giá trị mà toán tử được áp dụ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 phép toán như cộng, trừ, nhân và chia

  • Trong Trăn 3. x, kết quả của phép chia là dấu phẩy động trong khi ở Python 2. phép chia x của 2 số nguyên là một số nguyên và để thu được kết quả số nguyên trong Python 3. x thả nổi (// số nguyên) được sử dụng
Toán tửMô tảCú pháp+Thêm. cộng hai toán hạngx + y–Phép trừ. trừ hai toán hạngx – y*Phép nhân. nhân hai toán hạngx * y/Division (float). chia toán hạng thứ nhất cho toán hạng thứ hai / y//Division (sàn). chia toán hạng thứ nhất cho toán hạng thứ hai // y%Modulus. trả về phần còn lại khi toán hạng đầu tiên được chia cho thứ haix % y**Lũy thừa. Trả về lần đầu tiên được nâng lên lũy thừa secondx ** y

QUYỀN ƯU TIÊN

  • P – Dấu ngoặc đơn
  • E – Luỹ thừa
  • M – Phép nhân     (Phép nhân và phép chia có cùng mức độ ưu tiên)
  • D – Bộ phận
  • A – Phép cộng     (Phép cộng và phép trừ có cùng mức độ ưu tiên)
  • S – Phép trừ

Toán tử mô đun giúp chúng tôi trích xuất các chữ số cuối cùng của một số. Ví dụ

  • x % 10 -> mang lại chữ số cuối cùng
  • x % 100 -> mang lại hai chữ số cuối

Thí dụ. Toán tử số học trong Python

Python3




False
True
False
3

False
True
False
4_______0_______5
False
True
False
6

False
True
False
7
False
True
False
5
False
True
False
9

0
14
-11
14
2
40
0

0
14
-11
14
2
40
1

0
14
-11
14
2
40
2_______0_______5
False
True
False
4
0
14
-11
14
2
40
5
False
True
False
7

0
14
-11
14
2
40
0

0
14
-11
14
2
40
8

0
14
-11
14
2
40
9
False
True
False
5
False
True
False
4
10
20
10
100
102400
2
False
True
False
7

0
14
-11
14
2
40
0

10
20
10
100
102400
5

10
20
10
100
102400
6
False
True
False
5
False
True
False
4
10
20
10
100
102400
9
False
True
False
7

0
14
-11
14
2
40
0

is          True if the operands are identical 
is not      True if the operands are not identical 
2

is          True if the operands are identical 
is not      True if the operands are not identical 
3_______0_______5
False
True
False
4
is          True if the operands are identical 
is not      True if the operands are not identical 
6
False
True
False
7

0
14
-11
14
2
40
0

is          True if the operands are identical 
is not      True if the operands are not identical 
9

True
True
0_______0_______5
False
True
False
4_______29_______6
is          True if the operands are identical 
is not      True if the operands are not identical 
6
False
True
False
7

0
14
-11
14
2
40
0

True
True
7

True
True
8
False
True
False
5
False
True
False
4
in            True if value is found in the sequence
not in        True if value is not found in the sequence
1
False
True
False
7

0
14
-11
14
2
40
0

in            True if value is found in the sequence
not in        True if value is not found in the sequence
4

in            True if value is found in the sequence
not in        True if value is not found in the sequence
5
False
True
False
5
False
True
False
4
10
20
10
100
102400
9
10
20
10
100
102400
9
False
True
False
7

0
14
-11
14
2
40
0

x is NOT present in given list
y is present in given list
2

x is NOT present in given list
y is present in given list
3____59_______4

x is NOT present in given list
y is present in given list
3____59_______6

x is NOT present in given list
y is present in given list
3____59_______8

x is NOT present in given list
y is present in given list
3____67_______0

x is NOT present in given list
y is present in given list
3
610
Hello! Welcome.
2

x is NOT present in given list
y is present in given list
3
610
Hello! Welcome.
4

x is NOT present in given list
y is present in given list
3
610
Hello! Welcome.
6

Đầu ra

13
5
36
2.25
2
1
6561

Ghi chú. Tham khảo Sự khác biệt giữa / và // để biết một số thông tin thú vị về hai toán tử này

Toán tử so sánh

So sánh các toán tử quan hệ so sánh các giá trị. Nó trả về Đúng hoặc Sai tùy theo điều kiện

OperatorDescriptionSyntax>Greater than: True if the left operand is greater than the rightx > y=Greater than or equal to True if the left operand is greater than or equal to the rightx >= y<=Less than or equal to True if the left operand is less than or equal to the rightx <= yis x is the same as yx is yis notx is not the same as yx is not y

= là toán tử gán và == toán tử so sánh

Thí dụ. Toán tử so sánh trong Python

Python3




610
Hello! Welcome.
7

False
True
False
4_______0_______5
False
True
False
True
False
True
00

False
True
False
7
False
True
False
5
False
True
False
True
False
True
03

0
14
-11
14
2
40
0

False
True
False
True
False
True
05

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
07

0
14
-11
14
2
40
0

False
True
False
True
False
True
09

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
11

0
14
-11
14
2
40
0

False
True
False
True
False
True
13

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
15
False
True
False
5
False
True
False
5
False
True
False
True
False
True
18

0
14
-11
14
2
40
0

False
True
False
True
False
True
20

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
22
False
True
False
5
False
True
False
True
False
True
18

0
14
-11
14
2
40
0

False
True
False
True
False
True
26

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
28
False
True
False
5
False
True
False
True
False
True
18

0
14
-11
14
2
40
0

False
True
False
True
False
True
32

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
34
False
True
False
5
False
True
False
True
False
True
18

Đầu ra

False
True
False
True
False
True

Toán tử logic

Các toán tử logic thực hiện các phép toán logic AND, logic OR và logic NOT. Nó được sử dụng để kết hợp các câu điều kiện

Toán tử Mô tả Cú pháp và Logic AND. Đúng nếu cả hai toán hạng là truex và yorLogical OR. Đúng nếu một trong hai toán hạng là đúng x hoặc ynotLogical NOT. Đúng nếu toán hạng sai không phải x

Thí dụ. Toán tử logic trong Python

Python3




False
True
False
True
False
True
37

False
True
False
4_______0_______5
False
True
False
True
False
True
40

False
True
False
7
False
True
False
5
False
True
False
True
False
True
43

0
14
-11
14
2
40
0

False
True
False
True
False
True
45

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
15
False
True
False
True
False
True
48
False
True
False
True
False
True
18

0
14
-11
14
2
40
0

False
True
False
True
False
True
51

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
15
False
True
False
True
False
True
54
False
True
False
True
False
True
18

0
14
-11
14
2
40
0

False
True
False
True
False
True
57

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
False
True
False
True
False
True
60
False
True
False
True
False
True
61

Đầu ra

False
True
False

toán tử Bitwise

Các toán tử theo bit hoạt động trên các bit và thực hiện các hoạt động theo từng bit. Chúng được sử dụng để hoạt động trên các số nhị phân

Toán tử Mô tả Cú pháp Bitwise AND x & y. Bitwise ORx. y~Bitwise NOT~x^Bitwise XORx ^ y>>Bitwise right shiftx>><Thí dụ. Toán tử Bitwise trong Python

Python3




False
True
False
True
False
True
62

False
True
False
4_______0_______5
False
True
False
True
False
True
65

False
True
False
7
False
True
False
5
False
True
False
9

0
14
-11
14
2
40
0

False
True
False
True
False
True
70

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
72

0
14
-11
14
2
40
0

False
True
False
True
False
True
74

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
76

0
14
-11
14
2
40
0

False
True
False
True
False
True
78

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
80

0
14
-11
14
2
40
0

False
True
False
True
False
True
82

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
84

0
14
-11
14
2
40
0

False
True
False
True
False
True
86

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
88
False
True
False
True
False
True
89
False
True
False
True
False
True
90

0
14
-11
14
2
40
0

False
True
False
True
False
True
92

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
94
False
True
False
True
False
True
89
False
True
False
True
False
True
90

Đầu ra

0
14
-11
14
2
40

Toán tử gán

Toán tử gán dùng để gán giá trị cho biến

OperatorDescriptionSyntax=Gán giá trị của vế phải của biểu thức cho toán hạng bên trái x = y + z+=Thêm AND. Cộng toán hạng bên phải với toán hạng bên trái rồi gán cho toán hạng bên trái+=b     a=a+b-=Trừ AND. Trừ toán hạng bên phải từ toán hạng bên trái rồi gán cho toán hạng bên trái-=b     a=a-b*=Nhân AND. Nhân toán hạng bên phải với toán hạng bên trái rồi gán cho toán hạng bên trái*=b     a=a*b/=Divide AND. Chia toán hạng bên trái cho toán hạng bên phải rồi gán cho toán hạng bên trái/=b     a=a/b%=Modulus AND. Lấy mô-đun bằng toán hạng trái và phải và gán kết quả cho toán hạng trái%=b     a=a%b//=Divide(sàn) AND. Chia toán hạng bên trái cho toán hạng bên phải rồi gán giá trị (sàn) cho toán hạng bên trái //=b     a=a//b**=Số mũ AND. Tính toán giá trị số mũ (tăng lũy ​​thừa) bằng cách sử dụng toán hạng và gán giá trị cho toán hạng bên trái**=b     a=a**b&=Thực hiện theo bit AND trên toán hạng và gán giá trị cho toán hạng bên trái&=b       a=a&b. =Thực hiện Bitwise OR trên toán hạng và gán giá trị cho toán hạng bên trái. =b     a=a. b^=Thực hiện xOR theo Bit trên toán hạng và gán giá trị cho toán hạng bên trái^=b     a=a^b>>=Thực hiện Dịch chuyển sang phải theo bit trên toán hạng và gán giá trị cho toán hạng bên trái>>=b     a=a>>b<<=

Thí dụ. Toán tử gán trong Python

Python3




False
True
False
True
False
True
97

False
True
False
4_______0_______5
False
True
False
True
False
True
65

0
14
-11
14
2
40
0

False
True
False
02

False
True
False
7
False
True
False
5
False
True
False
4

x is NOT present in given list
y is present in given list
3
False
True
False
07

0
14
-11
14
2
40
0

False
True
False
09

False
True
False
7
0
14
-11
14
2
40
5
False
True
False
5
False
True
False
4

x is NOT present in given list
y is present in given list
3
False
True
False
07

0
14
-11
14
2
40
0

False
True
False
17

False
True
False
7
10
20
10
100
102400
2
False
True
False
5
False
True
False
4

x is NOT present in given list
y is present in given list
3
False
True
False
07

0
14
-11
14
2
40
0

False
True
False
25

False
True
False
7
10
20
10
100
102400
9
False
True
False
5
False
True
False
4

x is NOT present in given list
y is present in given list
3
False
True
False
07

0
14
-11
14
2
40
0

False
True
False
33

False
True
False
34
False
True
False
5
False
True
False
4

x is NOT present in given list
y is present in given list
3
False
True
False
07

Đầu ra

10
20
10
100
102400

toán tử nhận dạng

is và is not are Cả hai toán tử nhận dạng đều được sử dụng để kiểm tra xem hai giá trị có nằm trên cùng một phần của bộ nhớ hay không. Hai biến bằng nhau không có nghĩa là chúng giống hệt nhau.  

is          True if the operands are identical 
is not      True if the operands are not identical 

Thí dụ. Toán tử nhận dạng

Python3




False
True
False
4_______0_______5
False
True
False
True
False
True
65

False
True
False
7
False
True
False
5
False
True
False
44

False
True
False
45
False
True
False
5
False
True
False
4

0
14
-11
14
2
40
0

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
15
False
True
False
51
False
True
False
True
False
True
60
False
True
False
True
False
True
18

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
15
False
True
False
51
False
True
False
57

Đầu ra

True
True

Thành viên điều hành

in và not in là các toán tử thành viên;

in            True if value is found in the sequence
not in        True if value is not found in the sequence

Thí dụ. Nhà điều hành thành viên

Python3




False
True
False
58

False
True
False
59

False
True
False
60
False
True
False
5
False
True
False
62

False
True
False
63
False
True
False
5
False
True
False
44

False
True
False
66
False
True
False
5
False
True
False
68
False
True
False
True
False
True
65
False
True
False
70
False
True
False
44
False
True
False
70
False
True
False
73
False
True
False
70
False
True
False
75
False
True
False
70
False
True
False
77
False
True
False
78

0
14
-11
14
2
40
0

False
True
False
80
False
True
False
81
False
True
False
True
False
True
60
False
True
False
83
False
True
False
66
False
True
False
85

False
True
False
86
x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
False
True
False
89
False
True
False
True
False
True
90

False
True
False
91______0_______92

False
True
False
86
x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
False
True
False
96
False
True
False
True
False
True
90

0
14
-11
14
2
40
0

False
True
False
80
0
14
-11
14
2
40
00_______0_______83
False
True
False
66_______0_______85

False
True
False
86
x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
0
14
-11
14
2
40
07
False
True
False
True
False
True
90

False
True
False
91______0_______92

False
True
False
86
x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
0
14
-11
14
2
40
14
False
True
False
True
False
True
90

Đầu ra

x is NOT present in given list
y is present in given list

Quyền ưu tiên và tính liên kết của các toán tử

Quyền ưu tiên và tính liên kết của các toán tử. Độ ưu tiên và tính kết hợp của toán tử xác định mức độ ưu tiên của toán tử

ưu tiên điều hành

Điều này được sử dụng trong một biểu thức có nhiều toán tử với mức độ ưu tiên khác nhau để xác định thao tác nào sẽ thực hiện trước

Thí dụ. ưu tiên điều hành

Python3




0
14
-11
14
2
40
16

0
14
-11
14
2
40
0

0
14
-11
14
2
40
18

0
14
-11
14
2
40
19_______0_______5
False
True
False
True
False
True
65
0
14
-11
14
2
40
5
False
True
False
44
10
20
10
100
102400
9
False
True
False
73

x is NOT present in given list
y is present in given list
3
0
14
-11
14
2
40
27

0
14
-11
14
2
40
0

0
14
-11
14
2
40
29

0
14
-11
14
2
40
30_______0_______5
0
14
-11
14
2
40
32

0
14
-11
14
2
40
33
False
True
False
5
0
14
-11
14
2
40
35

0
14
-11
14
2
40
0

False
True
False
80
0
14
-11
14
2
40
30_______0_______5
False
True
False
5
0
14
-11
14
2
40
32
False
True
False
True
False
True
54
0
14
-11
14
2
40
30
False
True
False
5
False
True
False
5
0
14
-11
14
2
40
46
False
True
False
True
False
True
48
0
14
-11
14
2
40
48
False
True
False
5
False
True
False
True
False
True
89
False
True
False
92

False
True
False
86
x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
0
14
-11
14
2
40
55
False
True
False
True
False
True
90

False
True
False
91______0_______92

False
True
False
86
x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
0
14
-11
14
2
40
62
False
True
False
True
False
True
90

Đầu ra

610
Hello! Welcome.

Hiệp hội nhà điều hành

Nếu một biểu thức chứa hai hoặc nhiều toán tử có cùng mức độ ưu tiên thì Toán tử kết hợp được sử dụng để xác định. Nó có thể là từ trái sang phải hoặc từ phải sang trái

Thí dụ. Hiệp hội nhà điều hành

Python3




0
14
-11
14
2
40
64

0
14
-11
14
2
40
0

0
14
-11
14
2
40
66

0
14
-11
14
2
40
67

0
14
-11
14
2
40
68

0
14
-11
14
2
40
69

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
0
14
-11
14
2
40
72
is          True if the operands are identical 
is not      True if the operands are not identical 
6
False
True
False
True
False
True
65
10
20
10
100
102400
9
False
True
False
True
False
True
65
False
True
False
True
False
True
90

0
14
-11
14
2
40
0

0
14
-11
14
2
40
66

0
14
-11
14
2
40
80

0
14
-11
14
2
40
81

0
14
-11
14
2
40
82

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
0
14
-11
14
2
40
85
10
20
10
100
102400
2
False
True
False
True
False
True
89
0
14
-11
14
2
40
5
0
14
-11
14
2
40
89
False
True
False
True
False
True
90

0
14
-11
14
2
40
0

0
14
-11
14
2
40
92

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
0
14
-11
14
2
40
85
10
20
10
100
102400
2
False
True
False
True
False
True
59
False
True
False
True
False
True
89
0
14
-11
14
2
40
5
0
14
-11
14
2
40
89
10
20
10
100
102400
01

0
14
-11
14
2
40
0

10
20
10
100
102400
03

10
20
10
100
102400
04

10
20
10
100
102400
05

10
20
10
100
102400
06

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
False
True
False
True
False
True
89
10
20
10
100
102400
9
10
20
10
100
102400
9
0
14
-11
14
2
40
89
10
20
10
100
102400
9
10
20
10
100
102400
9
False
True
False
True
False
True
89
False
True
False
True
False
True
90

Đầu ra

False
True
False
True
False
True
0

Câu đố về Toán tử Python

Phép chia cho phép bạn chia hai số và trả về một thương số, i. e. , số đầu tiên hoặc số ở bên trái được chia cho số hoặc số thứ hai ở bên phải và trả về thương.  

Có hai loại toán tử chia.  

(i) Phân chia nổi.  

Thương số trả về bởi toán tử này luôn là một số float, bất kể hai số có phải là số nguyên hay không. Ví dụ

False
True
False
True
False
True
1

(ii) Phép chia số nguyên (Phép chia tầng).  

Thương số được trả về bởi toán tử này phụ thuộc vào đối số được truyền. Nếu bất kỳ số nào ở dạng float, nó sẽ trả về đầu ra ở dạng float. Nó còn được gọi là Phép chia sàn bởi vì, nếu bất kỳ số nào âm, thì đầu ra sẽ được chia sàn. Ví dụ

False
True
False
True
False
True
2

Hãy xem xét các câu lệnh dưới đây trong Python

Python3




10
20
10
100
102400
17

10
20
10
100
102400
18

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
0
14
-11
14
2
40
85
is          True if the operands are identical 
is not      True if the operands are not identical 
6
is          True if the operands are identical 
is not      True if the operands are not identical 
6
False
True
False
True
False
True
89
False
True
False
True
False
True
90

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
10
20
10
100
102400
2
0
14
-11
14
2
40
85
is          True if the operands are identical 
is not      True if the operands are not identical 
6
is          True if the operands are identical 
is not      True if the operands are not identical 
6
False
True
False
True
False
True
89
False
True
False
True
False
True
90

đầu ra

False
True
False
True
False
True
3

Đầu ra đầu tiên là tốt, nhưng đầu ra thứ hai có thể ngạc nhiên nếu chúng ta đến với thế giới Java/C++. Trong Python, toán tử “//” hoạt động như phép chia sàn cho các đối số số nguyên và số float. Tuy nhiên, toán tử chia '/' luôn trả về giá trị float

Ghi chú. Toán tử “//” được sử dụng để trả về giá trị số nguyên gần nhất nhỏ hơn hoặc bằng một biểu thức hoặc giá trị đã chỉ định. Vì vậy, từ đoạn mã trên, 5//2 trả về 2. Bạn biết rằng 5/2 là 2. 5 và số nguyên gần nhất nhỏ hơn hoặc bằng là 2[5//2]. (nó nghịch đảo với toán bình thường, trong toán bình thường giá trị là 3)

Thí dụ

Python3




10
20
10
100
102400
34

10
20
10
100
102400
35

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
10
20
10
100
102400
38
is          True if the operands are identical 
is not      True if the operands are not identical 
6
False
True
False
True
False
True
89
False
True
False
True
False
True
90

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
10
20
10
100
102400
2
10
20
10
100
102400
38
is          True if the operands are identical 
is not      True if the operands are not identical 
6
False
True
False
True
False
True
89
False
True
False
True
False
True
90

Đầu ra

False
True
False
True
False
True
4

Toán tử chia tầng thực sự là “//”. Nó trả về giá trị sàn cho cả đối số số nguyên và dấu phẩy động

Python3




10
20
10
100
102400
34

10
20
10
100
102400
50

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
0
14
-11
14
2
40
85
is          True if the operands are identical 
is not      True if the operands are not identical 
6
is          True if the operands are identical 
is not      True if the operands are not identical 
6
False
True
False
True
False
True
89
False
True
False
True
False
True
90

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
10
20
10
100
102400
2
0
14
-11
14
2
40
85
is          True if the operands are identical 
is not      True if the operands are not identical 
6
is          True if the operands are identical 
is not      True if the operands are not identical 
6
False
True
False
True
False
True
89
False
True
False
True
False
True
90

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
10
20
10
100
102400
38
is          True if the operands are identical 
is not      True if the operands are not identical 
6
is          True if the operands are identical 
is not      True if the operands are not identical 
6
False
True
False
True
False
True
89
False
True
False
True
False
True
90

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
10
20
10
100
102400
2
10
20
10
100
102400
38
is          True if the operands are identical 
is not      True if the operands are not identical 
6
is          True if the operands are identical 
is not      True if the operands are not identical 
6
False
True
False
True
False
True
89
False
True
False
True
False
True
90

Đầu ra

False
True
False
True
False
True
5

Xem cái này chẳng hạn.  

nhà khai thác ternary

Toán tử bậc ba còn được gọi là biểu thức điều kiện là toán tử đánh giá điều gì đó dựa trên điều kiện là đúng hay sai. Nó đã được thêm vào Python trong phiên bản 2. 5.  
Nó chỉ đơn giản cho phép kiểm tra một điều kiện trong một dòng thay thế if-else nhiều dòng làm cho mã nhỏ gọn
 

cú pháp.  

False
True
False
True
False
True
6
  • Phương pháp đơn giản để sử dụng toán tử ternary

con trăn




10
20
10
100
102400
81

10
20
10
100
102400
82
False
True
False
5
False
True
False
True
False
True
65
False
True
False
70
False
True
False
44

0
14
-11
14
2
40
0

10
20
10
100
102400
88

10
20
10
100
102400
89
False
True
False
5
False
True
False
4_______0_______80
10
20
10
100
102400
93
False
True
False
91
False
True
False
7

0
14
-11
14
2
40
0

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
10
20
10
100
102400
89
False
True
False
True
False
True
90

đầu ra.  

False
True
False
True
False
True
7
  • Phương pháp trực tiếp bằng cách sử dụng bộ dữ liệu, Từ điển và lambda

con trăn




is          True if the operands are identical 
is not      True if the operands are not identical 
01

10
20
10
100
102400
82
False
True
False
5
False
True
False
True
False
True
65
False
True
False
70
False
True
False
44

0
14
-11
14
2
40
0

is          True if the operands are identical 
is not      True if the operands are not identical 
08

is          True if the operands are identical 
is not      True if the operands are not identical 
09

is          True if the operands are identical 
is not      True if the operands are not identical 
10

is          True if the operands are identical 
is not      True if the operands are not identical 
11

x is NOT present in given list
y is present in given list
3
is          True if the operands are identical 
is not      True if the operands are not identical 
13

0
14
-11
14
2
40
0

is          True if the operands are identical 
is not      True if the operands are not identical 
15

is          True if the operands are identical 
is not      True if the operands are not identical 
16

is          True if the operands are identical 
is not      True if the operands are not identical 
17

x is NOT present in given list
y is present in given list
3
is          True if the operands are identical 
is not      True if the operands are not identical 
19
False
True
False
True
False
True
40
is          True if the operands are identical 
is not      True if the operands are not identical 
21
False
True
False
True
False
True
43
is          True if the operands are identical 
is not      True if the operands are not identical 
23

0
14
-11
14
2
40
0

is          True if the operands are identical 
is not      True if the operands are not identical 
25

is          True if the operands are identical 
is not      True if the operands are not identical 
26

is          True if the operands are identical 
is not      True if the operands are not identical 
27

is          True if the operands are identical 
is not      True if the operands are not identical 
28

x is NOT present in given list
y is present in given list
3
is          True if the operands are identical 
is not      True if the operands are not identical 
30
is          True if the operands are identical 
is not      True if the operands are not identical 
31_______29_______32
is          True if the operands are identical 
is not      True if the operands are not identical 
31
is          True if the operands are identical 
is not      True if the operands are not identical 
34

đầu ra

False
True
False
True
False
True
8
  • Toán tử bậc ba có thể được viết dưới dạng if-else lồng nhau

con trăn




is          True if the operands are identical 
is not      True if the operands are not identical 
35

10
20
10
100
102400
82
False
True
False
5
False
True
False
True
False
True
65
False
True
False
70
False
True
False
44

0
14
-11
14
2
40
0

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
is          True if the operands are identical 
is not      True if the operands are not identical 
44
False
True
False
80
False
True
False
4
False
True
False
5
False
True
False
5
False
True
False
7
False
True
False
91
is          True if the operands are identical 
is not      True if the operands are not identical 
51

is          True if the operands are identical 
is not      True if the operands are not identical 
52
False
True
False
80
is          True if the operands are identical 
is not      True if the operands are not identical 
54
False
True
False
91
is          True if the operands are identical 
is not      True if the operands are not identical 
56
False
True
False
True
False
True
90

Cách tiếp cận trên có thể được viết là.  
 

con trăn




is          True if the operands are identical 
is not      True if the operands are not identical 
35

10
20
10
100
102400
82
False
True
False
5
False
True
False
True
False
True
65
False
True
False
70
False
True
False
44

0
14
-11
14
2
40
0

False
True
False
80
is          True if the operands are identical 
is not      True if the operands are not identical 
66
False
True
False
5
is          True if the operands are identical 
is not      True if the operands are not identical 
68

False
True
False
86
False
True
False
80
is          True if the operands are identical 
is not      True if the operands are not identical 
71

is          True if the operands are identical 
is not      True if the operands are not identical 
52
x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
is          True if the operands are identical 
is not      True if the operands are not identical 
51
False
True
False
True
False
True
90

False
True
False
86
False
True
False
91
False
True
False
92

is          True if the operands are identical 
is not      True if the operands are not identical 
52
x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
is          True if the operands are identical 
is not      True if the operands are not identical 
56
False
True
False
True
False
True
90

False
True
False
91______0_______92

False
True
False
86
x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
is          True if the operands are identical 
is not      True if the operands are not identical 
44
False
True
False
True
False
True
90

đầu ra

False
True
False
True
False
True
9
  • Để sử dụng chức năng in trong toán tử ternary giống như. -

Thí dụ. Tìm số lớn hơn trong số 2 bằng cách sử dụng toán tử ternary trong python3

Python3




False
True
False
4_______0_______5_______7_______85

False
True
False
7
False
True
False
5
is          True if the operands are identical 
is not      True if the operands are not identical 
97

0
14
-11
14
2
40
0

is          True if the operands are identical 
is not      True if the operands are not identical 
99

0
14
-11
14
2
40
0

x is NOT present in given list
y is present in given list
3
True
True
02
True
True
03
False
True
False
True
False
True
90
False
True
False
80
True
True
06
False
True
False
91
x is NOT present in given list
y is present in given list
3
True
True
09
True
True
10
False
True
False
True
False
True
90

đầu ra

False
True
False
0

Điểm quan trọng.  

  • Đầu tiên, điều kiện đã cho được ước tính (a < b), sau đó a hoặc b được trả về dựa trên giá trị Boolean được trả về bởi điều kiện
  • Thứ tự của các đối số trong toán tử khác với các ngôn ngữ khác như C/C++ (Xem C/C++ toán tử bậc ba)
  • Biểu thức điều kiện có mức ưu tiên thấp nhất trong số tất cả các thao tác Python

Phương pháp được sử dụng trước 2. 5 khi toán tử bậc ba không có mặt
Trong một biểu thức như biểu thức được đưa ra dưới đây, trình thông dịch sẽ kiểm tra biểu thức nếu điều này đúng thì on_true được ước tính, nếu không thì on_false được ước tính
 

cú pháp.  

False
True
False
1

Thí dụ.  

con trăn




True
True
12

10
20
10
100
102400
81

10
20
10
100
102400
82
False
True
False
5
False
True
False
True
False
True
65
False
True
False
70
False
True
False
44

0
14
-11
14
2
40
0

True
True
20

True
True
21

True
True
22

10
20
10
100
102400
89
False
True
False
5
10
20
10
100
102400
93
False
True
False
True
False
True
48
False
True
False
4
False
True
False
True
False
True
54
False
True
False
7

0
14
-11
14
2
40
0

x is NOT present in given list
y is present in given list
3
False
True
False
True
False
True
59
10
20
10
100
102400
89
False
True
False
True
False
True
90

đầu ra

False
True
False
True
False
True
7

Ghi chú. Hạn chế duy nhất của phương pháp này là on_true không được bằng 0 hoặc Sai. Nếu điều này xảy ra, on_false sẽ luôn được đánh giá. Lý do là nếu biểu thức là true, trình thông dịch sẽ kiểm tra on_true, nếu nó bằng 0 hoặc sai, điều đó sẽ buộc trình thông dịch kiểm tra on_false để đưa ra kết quả cuối cùng của toàn bộ biểu thức

Công dụng của toán tử là gì?

Một toán tử được sử dụng để thao tác với các mục dữ liệu riêng lẻ và trả về kết quả . Các mục này được gọi là toán hạng hoặc đối số. Các toán tử được thể hiện bằng ký tự đặc biệt hoặc bằng từ khóa.

%= nghĩa là gì trong Python?

Ký hiệu % trong Python được gọi là Toán tử Modulo . Nó trả về phần còn lại của phép chia toán hạng bên trái cho toán hạng bên phải. Nó được sử dụng để lấy phần còn lại của một vấn đề chia.

Việc sử dụng toán tử trong Python với các ví dụ là gì?

Trong Python, in và not in là các toán tử thành viên. Chúng được sử dụng để kiểm tra xem một giá trị hoặc biến có được tìm thấy trong một chuỗi (chuỗi, danh sách, bộ, bộ và từ điển). . nhà khai thác thành viên

Tại sao in toán tử được sử dụng trong Python?

Trong Python, toán tử in xác định xem một giá trị đã cho có phải là thành phần cấu thành của một chuỗi như chuỗi, mảng, danh sách hay bộ hay không. When used in a condition, the statement returns a Boolean result of True or False. The statement returns True if the specified value is found within the sequence.