Python False 關鍵字



在 Python 中,False 關鍵字表示布林值。它是比較操作的結果。False 的整數值為

它是區分大小寫的。它通常用於控制流語句,如ifwhilefor 迴圈,以控制流程並根據邏輯執行。

False 關鍵字的整數值

False 關鍵字的整數值為0。在算術運算中,我們可以使用False 代替零。

示例

讓我們透過以下示例找到False 關鍵字的整數值:

x=False
y=int(x)
print("The integer value of ",x,":",y)

輸出

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

The integer value of  False : 0

False 關鍵字區分大小寫

False 關鍵字是區分大小寫的。它必須寫成 False,其中F 大寫。使用false 代替False 將導致NameError

示例

x=false
print("We will get NameError",x)

輸出

Traceback (most recent call last):
  File "E:\pgms\Keywords\false.py", line 11, in <module>
    x=false
      ^^^^^
NameError: name 'false' is not defined. Did you mean: 'False'?

False 關鍵字在條件語句中

False 關鍵字用於if 塊、while 迴圈。僅當條件為 True 時才執行if 塊。如果給定條件為False,則不會執行該特定塊。類似地,在while 迴圈中,迴圈內的語句將不會執行。

示例:if 語句中的 False 關鍵字

讓我們透過一個示例瞭解False 關鍵字。這裡,x 的值為9。x 是一個大於 0 的正數。給定條件是 x 小於 0,這是False,因此不會執行塊內的程式碼。

x=9
if x<0:
    print(x,"is a negative number")
else:
    print(x,"is a positive number")

輸出

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

9 is a positive number

迴圈中的 False 關鍵字

當給定條件不滿足時,條件為False。在這種情況下,while 迴圈內的語句將不會執行。

示例

var=False
while var:
    print("This statements will not be executed as the condition is False.")
   
print("This statements gets executed as it is outside the while loop.")

輸出

This statements gets excecuted as it is outside the while loop.

函式中的 False 關鍵字

讓我們瞭解函式中的False 關鍵字。函式將根據條件返回TrueFalse。如果條件滿足則返回 True,否則返回False

def natural(num):
    return num>0
    
x=-4
print("The given number is natural number True/False :",natural(x))
y=9
print("The given number is natural number True/False :",natural(y))

輸出

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

The given number is natural number True/False : False
The given number is natural number True/False : True
python_keywords.htm
廣告

© . All rights reserved.