如何在 Python 中為列印編寫內聯 if 語句?


Python 提供兩種編寫內聯 if 語句的方法。它們是

1. if 條件: 語句

2. s1 if 條件 else s2

請注意,第二種型別的 if 不能在沒有 else 的情況下使用。現在,你也可以將它們內聯用於 print 語句。例如,

a = True
if a: print("Hello")

這將輸出

Hello
a = False
print("True" if a else "False")

這將輸出

False

更新於: 17-Jun-2020

6K+ 瀏覽次數

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.