Hướng dẫn ternary operator in python - toán tử bậc ba trong python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    It simply allows testing a condition in a single line replacing the multiline if-else making the code compact.
     

    Bàn luận

    [on_true] if [expression] else [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

    10
    9
    10
    10
    10
    0
    10
    2
    10
    10
    10
    2

    Output:  

    10
    • a, b = 10
      10
      0
      10
      1
      , and lambda

    Cú pháp: & nbsp;

    Phương pháp đơn giản để sử dụng toán tử ternary:

    10
    9
    10
    10
    10
    9

    10
    9
     b is greater than a
    1
     b is greater than a
    2
     b is greater than a
    3
     b is greater than a
    4
     b is greater than a
    5

    10
    9
     b is greater than a
    7
     b is greater than a
    8
     b is greater than a
    9
     b is greater than a
    8
    7 is Greater
    1

    Output:

    10
    10
    10

    Python

    a, b = 10

    10
    0
    10
    1

    • 10
      2 =
      10
      4
      10
      5
      10
      6
      10
      7
      10
      8

    Cú pháp: & nbsp;

    Phương pháp đơn giản để sử dụng toán tử ternary:

    Python

    a, b = 10

    10
    0
    10
    1

    Python

    a, b = 10

    10
    0
    10
    1

    10
    2 =
    10
    4
    10
    5
    10
    6
    10
    7
    10
    8
     

    Cú pháp: & nbsp;

    Phương pháp đơn giản để sử dụng toán tử ternary:

    Python

    a, b = 10

    10
    0
    10
    1

    '''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
    10
    9
    10
    10
    10
    0
    '''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
    10
    2

    a, b 2

    10
    7=2

    10
    2 =
    10
    4
    10
    5
    10
    6
    10
    7
    10
    8

    10
    7=2

    a, b 2

    10
    9
    10
    10
    10
    0
    7 is Greater
    9
    10
    10
    10
    2

    Output:

     b is greater than a

    Python

    a, b = 10

    10
    0
    10
    1

    • 10
      2 =
      10
      4
      10
      5
      10
      6
      10
      7
      10
      8

    Phương pháp trực tiếp bằng cách sử dụng Tuples, Dictionary và Lambda

    Python3

    105=107

    10
    8=
    10
    00

    Độ phức tạp về thời gian: O (1)

    Output:

    7 is Greater

    Python

    a, b = 10

    10
    0
    10
    1

    102 = 104105 106107 108

    • 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

    10
    9
    10
    10
    10
    0
    10
    2
    10
    10
    10
    2

    Output:

    10

    PythonO(1)

    a, b = 10

    10
    0
    10
    1
    O(1)

    10
    2 =
    10
    4
    10
    5
    10
    6
    10
    7
    10
    8
    The 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.