Python1: boolean type, python1 Boolean Type
Boolean users determine conditions in conditions or loops. Here we will introduce the Boolean Type in python.
True and False
The python boolean type corresponds to two boolean values: True and False, corresponding to 1 and 0 respectively. See the following example.
>>> True = 1
True
>>> False = 0
True
>>> True + False + 21
22
Boolean expression value
When the following value is used as a Boolean expression, it is regarded as False by the interpreter:
1. None;
2. False;
3. Any numeric type of 0, such as 0, 0.0, and 0j;
4. Any empty sequence, such as '', (), [];
5. Any empty dictionary, such :{};
6. for user-defined class instances, if the class defines the _ bool _ () or _ len _ () method, and this method returns 0 or Boolean value False.
All other values are treated as True by the interpreter.
You can use bool to convert other values to boolean values:
>>> Bool ('')
False
>>> Bool ('this is a test ')
True
>>> Bool (42)
True
>>> Bool (0)
False
Boolean operation
The following is a Boolean operation in python:
1. x or y: if x is false, then y, else x
2. x and y: if x is false, then x, else y
3. not x: if x is false, then True, else False