簡體   English   中英

Python 如果 function 具有多個條件,包括無

[英]Python if function with multiple conditions including none

我正在嘗試做一個 if 語句來檢查兩件事:1)如果 x 是 None 2)如果 x 是 [](空列表)

但是我似乎無法將兩者合並為一個方程式

這是我嘗試過的:

x = None
if x is not None or x != []:
print("x is not none or empty list")

在這種情況下,期望的結果是它什么都不打印,但它仍然打印出來。 我究竟做錯了什么?

您可以使用 and 語句:

if x is not None and x!=[]:
   print('output')

或者我出於任何原因想要使用 or 聲明:

if not(x is None or x == []):
   print('output')

您的問題是,如果x == None x 不為空並且結果為true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM