使用布尔值的If语句的语法

Luc*_*ity 7 python if-statement boolean python-3.x

嘿伙计们,我刚刚加入了python3 HypeTrain!但是我只是想知道如何在布尔值上使用if语句:示例:

RandomBool = True
#and now how can i check this in an if statement? Like the following:
if RandomBool == True:
    #DoYourThing
Run Code Online (Sandbox Code Playgroud)

而且,我可以像这样切换一个布尔值吗?

RandomBool1 == True   #Boolean states True
if #AnyThing:
    RandomBool1 = False   #Boolean states False from now on? 
Run Code Online (Sandbox Code Playgroud)

Mat*_*ime 40

您可以随意更改bool的值.至于if:

if randombool == True:
Run Code Online (Sandbox Code Playgroud)

有效,但您也可以使用:

if randombool:
Run Code Online (Sandbox Code Playgroud)

如果你想测试某些东西是否是假的,你可以使用:

if randombool == False
Run Code Online (Sandbox Code Playgroud)

但你也可以使用:

if not randombool:
Run Code Online (Sandbox Code Playgroud)


小智 15

我想你也可以使用

if randombool is True:

elif randombool is False:
Run Code Online (Sandbox Code Playgroud)

我认为你不需要使用等号,除非它是 int 或 float。

如我错了请纠正我