python 三元運算符

| | | | | | | | | | | | | | | | | | |

Python 中沒有三元運算符。但它確實支持編寫 if-else 語句,使其像三元運算符一樣工作。

為什麼 Python 沒有特殊的三元運算符?

許多編程語言都有三元運算符。但他們的主要目標是&通過刪除簡單的 if-else 塊來減少代碼大小。 Python 改進了 if-else 語句以減少代碼的大小,而不是添加額外的語句。

Python 實現

Python 中模擬三元運算符的語法是

< script async src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle .js?client = ca-pub-6959883099270890" crossorigin = "anonymous"> < ins class = "adsbygoogle" style = "display: inline-block; width: 336px; height: 280px" data-ad-client = "ca-pub-6959883099270890" data-ad-slot = "3943413939" >