There are operators ʻor, ʻand
, and not
in Python, but these are called Boolean operators.
Python can represent any object as a truth value (true / false value), and the following built-in functions are judged to be false.
--Constants defined as false: None and False --Zero in numeric type: 0, 0.0, 0j, Decimal (0), Fraction (0, 1) --Empty sequence or collection:'', (), [], {}, set (), range (0)
Also, if the object's class defines a __bool__ ()
method and it returns a False
, or if it defines a__len__ ()
method and returns 0, it will be judged false. ..
The built-in function bool ()
is used for judgment.
>>> bool(None)
False
>>> bool(False)
False
>>> bool(0)
False
>>> bool("")
False
>>> bool([])
False
#Everything that is not false is true
>>> bool("0")
True
>>> bool([False])
True
ʻUsed when you want to determine whether either A or B is true`. Of course, it can be used outside of control syntax such as if statements.
>>> x = True
>>> y = False
>>> x or y
True
>>> bool(x or y)
True
By the way, ʻor and ʻand
described later do not return a Boolean value, evaluate the value of the compared object from the left, and return it as it is when the return value is fixed.
In the above example (x or y)
--If x is true, y is not evaluated and x is returned (even if y is true, x is returned) --If not, return y
It works
>>> a = 0 #Define a value whose truth value is True
>>> b = 1 #Define a value whose truth value is False
>>> a or b #Since b is True, b is returned
1
>>> b or a #Even if it is reversed, b is True, so b is returned.
1
>>> b = "" #Reassign a value different from a whose truth value is False
a or b #b is returned because a is False
""
By the way, ʻor` behaves in the same way even if two or more are connected, and is evaluated from the left.
>>> a = 0
>>> b = 1
>>> c = 2
>>> a or b or c
1
It's easy to think of it as an operator that returns a truth value, but don't forget.
ʻAnd can be used when you want to determine whether both ʻA and B are true
.
As for ʻand`, the return value is the object to be evaluated, so a Boolean value is not returned.
>>> x = 0
>>> y = 1
>>> x and y
0
As with ʻor`, the object is returned when the result is confirmed.
--Return x if x is false --Otherwise it returns y
It works.
Note that we haven't strictly checked that everything we compare is true, as false objects will return the results as soon as they are found.
Two or more can be connected for ʻand`.
>>> a = 0
>>> b = 1
>>> c = 2
>>> a and b and c #Returns a because a is false
0
>>> d = 10
>>> d and b and c #When d is true, b is true and c is confirmed, the result is confirmed, so c is returned.
2
not
is an operator that represents negation, and takes only one value after it, such as not False
, and negates that value.
Unlike the two operators described above, it returns a Boolean value.
>>> x = 0
>>> y = 1
>>> not x #x is a value that becomes False, but if not is added, it becomes True.
True
>>> not y #y is a value that is True, but if not is added, it becomes False.
False
not
has a higher priority than ʻor and ʻand
and is evaluated first.
>>> a = 0
>>> b = 1
>>> c = 2
>>> not a and b and c #False a is negated and becomes true, and since everything is true, the last evaluated object c is returned
c
>>> d = 10
>>> d and not b and c #Everything was true, but b is denied and false, so b is returned
False
Although it is basic, it is easy to forget about the return value, so I would like to use it consciously.
Recommended Posts