Python bool Boolean type

56634bea1c1d4541bc9adb5e724734e8.png

 

Python provides the bool type to represent true (right) or false (wrong), such as the common 5 > 3 comparison formula. This is correct and is called true (right) in the programming world. Python uses True to represent it; For example, the 4 > 20 comparison formula is wrong. It is called false (wrong) in the programming world. Python uses False to represent it.

True and False are keywords in Python. When entering them as Python code, you must pay attention to the case of letters, otherwise the interpreter will report an error.

 

It is worth mentioning that the Boolean type can be treated as an integer, that is, True is equivalent to the integer value 1, and False is equivalent to the integer value 0. Therefore, the following operations are possible:

>>> False+1

1

>>> True+1

2

 

Note that this is just to illustrate the integer values ​​corresponding to True and False. It is inappropriate in actual applications, so do not use it like this.

 

In general, the bool type is used to represent the truth (right) or false (wrong) of something. If the thing is correct, it is represented by True (or 1); if the thing is wrong, it is represented by False ( or 0) represents.

 

【Example 1】

>>> 5>3

True

>>> 4>20

False

 

In Python, all objects can be tested for true and false values, including strings, tuples, lists, dictionaries, objects, etc. Since I haven’t learned it yet, I won’t go into too much detail here. I will go into detail when I encounter it later. introduction.

Guess you like

Origin blog.csdn.net/weixin_74774974/article/details/133420843