Python None 關鍵字



Python 的None關鍵字用於定義值或根本沒有值。它與空字串False不同。它屬於NoneType類物件。它是區分大小寫的。void 函式在執行時返回None

示例

以下是 Python None關鍵字的基本示例 -

var1 = None
if var1 is None:
    print("True")
else:
    print("False")

輸出

以下是上述程式碼的輸出 -

True

Void 函式中的 None 關鍵字

不執行任何操作且函式體為空的函式稱為void 函式。為了避免IndentationError,我們使用pass關鍵字。void 函式返回None

示例

這裡,我們定義了一個 void 函式Tp(),並在執行時返回None -

def Tp():
    pass
result1 = Tp()
print("The return type of Void Function :",result1)

None 的資料型別

None關鍵字是NoneType類物件的資料型別。

示例

讓我們嘗試透過以下示例找到None關鍵字的資料型別 -

x = None
print(type(x))

輸出

以下是上述程式碼的輸出 -

<class 'NoneType'>

類中的 None 關鍵字

內部定義的函式稱為方法。如果方法為空,則在呼叫它時將返回None

示例

這裡,我們建立了一個名為Python的類,並在其中包含一個名為Tp()的方法。當我們呼叫Tp()時,由於它是空的,因此返回None -

class Python:
    def Tp():
        pass
        
Obj1 = Python
print("The return type of empty method :",Obj1.Tp())

輸出

以下是上述程式碼的輸出 -

The return type of empty method : None
python_keywords.htm
廣告