21 comparisons boolean variables
1]: print( 5 > 2)
In [True
2]: print( 5 < 2)
In [False
3]: print( 2 == 2)
In [True
4]: print( "2" == 2)
In [False
5]: print( 1 != 2)
In [True
6]: print( 1 < "1")
In [---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-eef05bdfaefd> in <module>
----> 1 print( 1 < "1")
TypeError: '<' not supported between instances of 'int' and 'str'
21.1 Question what is the result of following code execution? Why?
print("Bremen" > "bremen")
See Ascii table for answer to this question.
21.1.1 Logical operators and, or, not
- and operator returns true if both sides are true
AND | ||
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
- or operator return trues if one of the sides are true
OR | ||
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
not reverse the boolean,
False -> True, True -> False
1]: birth_year = 1999
In [
2]: birth_year < 2000 and birth_year < 2019
In [2]: True
Out[
3]: birth_year < 1900 or birth_year > 1950
In [3]: True
Out[
4]: not (birth_year < 1900 or birth_year > 1950 )
In [4]: False Out[
21.2 Links in our references
- boolean-operations
- Non-Programmer’s Tutorial for Python 3/Boolean Expressions
- Python Booleans in w3schools.com
21.2.1 Chaining comparison operators
TODO