Xem thảo luận Cải thiện bài viết Lưu bài viết Xem thảo luận Cải thiện bài viết Lưu bài viết Đọc Bàn luận Phương pháp đơn giản để sử dụng toán tử ternary: Python
It simply allows testing a condition in a
single line replacing the multiline if-else making the code compact.
[on_true] if [expression] else [on_false]
Cú pháp: & nbsp;
10
910
10
10
010
210
10
10
2
Output:
10
a, b
=
10
10
010
1, and lambda
Cú pháp: & nbsp;
Phương pháp đơn giản để sử dụng toán tử ternary:
109
10 10 109
109
b is greater than a1
b is greater than a2
b is greater than a3
b is greater than a4
b is greater than a5
109
b is greater than a7
b is greater than a8
b is greater than a9
b is greater than a8
7 is Greater1
Output:
10 10 10
Python
a, b
=
10
100
101
10
2=
10
410
510
610
710
8
Cú pháp: & nbsp;
Phương pháp đơn giản để sử dụng toán tử ternary:
Python
a, b
=
10
100
101
Python
a, b
=
10
100
101
102
=
104
105
106
107
108
Cú pháp: & nbsp;
Phương pháp đơn giản để sử dụng toán tử ternary:
Python
a, b
=
10
100
101
'''When condition becomes true, expression [on_false] is not executed and value of "True and [on_true]" is returned. Else value of "False or [on_false]" is returned. Note that "True and x" is equal to x. And "False or x" is equal to x. ''' [expression] and [on_true] or [on_false]7
109
10 10 100
'''When condition becomes true, expression [on_false] is not executed and value of "True and [on_true]" is returned. Else value of "False or [on_false]" is returned. Note that "True and x" is equal to x. And "False or x" is equal to x. ''' [expression] and [on_true] or [on_false]6
10 10 102
a, b
2
107
=
2102
=
104
105
106
107
108
107
=
2a, b
2
109
10 10 100
7 is Greater9
10 10 102
Output:
b is greater than a
Python
a, b
=
10
100
101
10
2=
10
410
510
610
710
8
Phương pháp trực tiếp bằng cách sử dụng Tuples, Dictionary và Lambda
Python3
10
5=
10
7
108
=
1000
Độ phức tạp về thời gian: O [1]
Output:
7 is Greater
Python
a, b
=
10
100
101
10
2 =
10
410
5 10
610
7 10
8
- Phương pháp trực tiếp bằng cách sử dụng Tuples, Dictionary và Lambda
- Độ phức tạp về thời gian: O [1]
- Không gian phụ trợ: O [1]
Toán tử ternary có thể được viết dưới dạng if-else:the ternary operator was not present
In an expression like the one given below, the interpreter checks for the expression if this is true then on_true is evaluated, else the on_false is evaluated.
Bàn luận
'''When condition becomes true, expression [on_false] is not executed and value of "True and [on_true]" is returned. Else value of "False or [on_false]" is returned. Note that "True and x" is equal to x. And "False or x" is equal to x. ''' [expression] and [on_true] or [on_false]
Các toán tử ternary còn được gọi là biểu thức có điều kiện là các toán tử đánh giá một cái gì đó dựa trên một điều kiện là đúng hoặc sai. Nó đã được thêm vào Python trong phiên bản 2.5. & NBSP; nó chỉ đơn giản cho phép kiểm tra một điều kiện trong một dòng thay thế cho if-else tạo ra mã nhỏ gọn. & Nbsp;
Cú pháp: & nbsp;
Phương pháp đơn giản để sử dụng toán tử ternary:
Python
109
10 10 100
102
10 10 102
Output:
10
PythonO[1]
a, b
=
10
10
010
1O[1]
10
2 =
10
410
5 10
610
7 10
8The only drawback of this method is that on_true must not be zero or False. If this happens on_false will be evaluated always. The reason for that is if the expression is
true, the interpreter will check for the on_true, if that will be zero or false, that will force the interpreter to check for on_false to give the final result of the whole expression.
Phương pháp trực tiếp bằng cách sử dụng Tuples, Dictionary và LambdaMayank Rawat and improved by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other
Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.