python中True,False與0,1之間的關系


demo1

>>> print(True == 1)
>>> print(True == 2)
>>> print(False == 0)
>>> print(False == 2)
True
False
True
False

這說明1與True,0與False在python來說是完全相等的東西。

demo2

>>> x = 5
>>> if x%2:
>>> x += 1
>>> else:
>>> x -= 1
>>> print(x)

在demo2中可以看出二者之間關系的妙用。
x%2=0 <——> x%2 = True

 


---------------------
版權聲明:本文為CSDN博主「Bruce chen」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/cpc784221489/article/details/90721228


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM