Hướng dẫn nested ternary operator python - trăn toán tử chim ba lồng lồng nhau

Trong Zen of Python, Tim Peters tuyên bố rằng

if :
    
else:
    
1. Nếu tôi đã hiểu điều đó một cách chính xác, thì trong Python, điều này:

Show
 if  else 

thường được ưa thích hơn điều này:

if :
    
else:
    

Tuy nhiên, trong các ngôn ngữ khác, tôi đã được bảo không làm tổ của người vận hành ternary và thay vào đó sử dụng

if :
    
else:
    
2 truyền thống. Sau đó, câu hỏi của tôi là tôi nên sử dụng điều này:

( if  else ) if  else 

hoặc

if :
    if :
        
    else:
        
else:
    

? Đặc biệt nếu các tuyên bố và điều kiện dài, và dòng đầu tiên sẽ cần chia tách?

Để sử dụng chức năng in trong toán tử ternary giống như:-& nbsp;

Ví dụ: Tìm số lượng lớn hơn trong số 2 sử dụng toán tử ternary trong Python3

Các

  • Điểm quan trọng: & NBSP;
  • Đầu tiên, điều kiện đã cho được đánh giá (a

    Để sử dụng chức năng in trong toán tử ternary giống như:-& nbsp;

    Ví dụ: Tìm số lượng lớn hơn trong số 2 sử dụng toán tử ternary trong Python3

    Các

    Điểm quan trọng: & NBSP;
    It simply allows testing a condition in a single line replacing the multiline if-else making the code compact.
     

    10
    10
    10
    3
    ( if  else ) if  else 
    
    5
    ( if  else ) if  else 
    
    6
    10
    10
    10
    7
    ( if  else ) if  else 
    
    8

    [on_true] if [expression] else [on_false] 
    • Để sử dụng chức năng in trong toán tử ternary giống như:-& nbsp;

    Python

    if :
        
    else:
        
    
    3
    if :
        
    else:
        
    
    4
    if :
        
    else:
        
    
    5
    if :
        
    else:
        
    
    6
    if :
        
    else:
        
    
    7

    ( if  else ) if  else 
    
    1
     b is greater than a
    5
    if :
        
    else:
        
    
    4
     b is greater than a
    7

    ( if  else ) if  else 
    
    5
    ( if  else ) if  else 
    
    6
    if :
        
    else:
        
    
    8
    ( if  else ) if  else 
    
    8

    Output:  

    10
    •  b is greater than a
      8
      ( if  else ) if  else 
      
      1
      7 is Greater
      0
      , and lambda

    Python

    if :
        
    else:
        
    
    3
    if :
        
    else:
        
    
    4
    if :
        
    else:
        
    
    5
    if :
        
    else:
        
    
    6
    if :
        
    else:
        
    
    7

    ( if  else ) if  else 
    
    5
    if :
        if :
            
        else:
            
    else:
        
    
    5

    ( if  else ) if  else 
    
    5
    if :
        if :
            
        else:
            
    else:
        
    
    7
    if :
        if :
            
        else:
            
    else:
        
    
    8
    if :
        if :
            
        else:
            
    else:
        
    
    9
    [on_true] if [expression] else [on_false] 
    0
    [on_true] if [expression] else [on_false] 
    1

    ( if  else ) if  else 
    
    5
    [on_true] if [expression] else [on_false] 
    3
    [on_true] if [expression] else [on_false] 
    4
    [on_true] if [expression] else [on_false] 
    5
    [on_true] if [expression] else [on_false] 
    4
    [on_true] if [expression] else [on_false] 
    7

    Output:

    10
    10
    10

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

    Không gian phụ trợ: O (1)

    • ( if  else ) if  else 
      
      1
       b is greater than a
      5
      if :
          
      else:
          
      
      4
       b is greater than a
      7

    Python

    if :
        
    else:
        
    
    3
    if :
        
    else:
        
    
    4
    if :
        
    else:
        
    
    5
    if :
        
    else:
        
    
    6
    if :
        
    else:
        
    
    7

    ( if  else ) if  else 
    
    1
     b is greater than a
    5
    if :
        
    else:
        
    
    4
     b is greater than a
    7

    10
    10
    10
    3
    ( if  else ) if  else 
    
    1
    10
    10
    10
    5
    ( if  else ) if  else 
    
    3
    10
    10
    10
    7
    ( if  else ) if  else 
    
    8

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

    Không gian phụ trợ: O (1)

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

    Python

    if :
        
    else:
        
    
    3
    if :
        
    else:
        
    
    4
    if :
        
    else:
        
    
    5
    if :
        
    else:
        
    
    6
    if :
        
    else:
        
    
    7

    ( if  else ) if  else 
    
    1
     b is greater than a
    5
    if :
        
    else:
        
    
    4
     b is greater than a
    7

     b is greater than a
    8
    ( if  else ) if  else 
    
    1
    7 is Greater
    0

    10
    10
    10
    3
    ( if  else ) if  else 
    
    5
    ( if  else ) if  else 
    
    6
    10
    10
    10
    2
    ( if  else ) if  else 
    
    8

     b is greater than a
    8
    ( if  else ) if  else 
    
    3
    7 is Greater
    8

    10
    10
    10
    3
    ( if  else ) if  else 
    
    5
    ( if  else ) if  else 
    
    6
    10
    10
    10
    7
    ( if  else ) if  else 
    
    8

    ( if  else ) if  else 
    
    3
    7 is Greater
    8

     b is greater than a
    8
    ( if  else ) if  else 
    
    5
    ( if  else ) if  else 
    
    6
    10
    5
    ( if  else ) if  else 
    
    8

    Output:

     b is greater than a

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

    Không gian phụ trợ: O (1)

    • Cách tiếp cận trên có thể được viết là: & nbsp; & nbsp;

    Python

    Python3

    if :
        
    else:
        
    
    01
    if :
        
    else:
        
    
    4
    if :
        
    else:
        
    
    03

    ( if  else ) if  else 
    
    4
    if :
        
    else:
        
    
    4
    if :
        
    else:
        
    
    06

    if :
        
    else:
        
    
    3
    if :
        
    else:
        
    
    4
    if :
        
    else:
        
    
    5
    if :
        
    else:
        
    
    6
    if :
        
    else:
        
    
    7

    Output:

    7 is Greater

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

    Không gian phụ trợ: O (1)

    Điểm quan trọng: & NBSP;

    • Đầu tiên, điều kiện đã cho được đánh giá (a
    • 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 toán tử ternary C/C ++).
    • Biểu thức có điều kiện có mức độ ưu tiên thấp nhất trong số tất cả các hoạt động Python.

    Phương thức được sử dụng trước 2.5 khi toán tử ternary không có mặt & nbsp; trong một biểu thức như loại được đưa ra dưới đây, trình thông dịch kiểm tra biểu thức nếu điều này đúng thì on_true được đánh giá, nếu không thì on_false được đánh giá. & NBSP;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.
     

    Cú pháp: & nbsp; 

    '''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] 

    Ví dụ: & nbsp; 

    Python

    if :
        
    else:
        
    
    3
    if :
        
    else:
        
    
    4
    if :
        
    else:
        
    
    5
    if :
        
    else:
        
    
    6
    if :
        
    else:
        
    
    7

    if :
        
    else:
        
    
    8
    if :
        
    else:
        
    
    4
    ( if  else ) if  else 
    
    2
    if :
        
    else:
        
    
    26
    ( if  else ) if  else 
    
    0
    if :
        
    else:
        
    
    28
    ( if  else ) if  else 
    
    4

    ( if  else ) if  else 
    
    5
    ( if  else ) if  else 
    
    6
    if :
        
    else:
        
    
    8
    ( if  else ) if  else 
    
    8

    Output:

    10

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

    Không gian phụ trợ: O (1)O(1)

    Điểm quan trọng: & NBSP;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.

    Đầu tiên, điều kiện đã cho được đánh giá (a Mayank 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.