|
| 1 | +\hsection{Boolean Values}% |
| 2 | +\label{sec:bool}% |
| 3 | +% |
| 4 | +Before, we already mentioned comparisons and their results, which can either be \pythonilIdx{True} or \pythonilIdx{False}. |
| 5 | +These two values constitute another basic datatype in \python: \pythonilIdx{bool}. |
| 6 | +They are fundamental for making decisions in a program, i.e., for deciding what to do based on data. |
| 7 | +% |
| 8 | +\hsection{Comparisons}% |
| 9 | +% |
| 10 | +\begin{figure}% |
| 11 | +\centering% |
| 12 | +\includegraphics[width=0.8\linewidth]{\currentDir/boolComparisons}% |
| 13 | +\caption{The results of basic comparisons are instances of \pythonilIdx{bool}.}% |
| 14 | +\label{fig:boolComparisons}% |
| 15 | +\end{figure}% |
| 16 | +% |
| 17 | +In the sections on \pythonilIdx{float}s and \pythonilIdx{int}s, we learned how to do arithmetics with real and integer numbers. |
| 18 | +You have learned these operations already in preschool. |
| 19 | +However, before you learn to calculate with numbers, you learned how to \emph{compare} them. |
| 20 | +If we compare two numbers, the result is either \pythonilIdx{True}, if the comparison works our positively, or \pythonilIdx{False}, if it does not. |
| 21 | +\python\ supports six types of comparison:% |
| 22 | +% |
| 23 | +\begin{itemize}% |
| 24 | +% |
| 25 | +\item equal: $a = b$ corresponds to \pythonil{a == b}\pythonIdx{==},% |
| 26 | +\item unequal: $a \neq b$ corresponds to \pythonil{a != b}\pythonIdx{!=},% |
| 27 | +\item less-than: $a < b$ corresponds to \pythonil{a < b}\pythonIdx{<},% |
| 28 | +\item less-than or equal: $a \leq b$ corresponds to \pythonil{a <= b}\pythonIdx{<=},% |
| 29 | +\item greater-than: $a > b$ corresponds to \pythonil{a > b}\pythonIdx{>}, and% |
| 30 | +\item greater-than or equal: $a \geq b$ corresponds to \pythonil{a >= b}\pythonIdx{>=}.% |
| 31 | +% |
| 32 | +\end{itemize}% |
| 33 | +% |
| 34 | +How to use these operators is illustrated in \cref{fig:boolComparisons}. |
| 35 | +It shows that \pythonil{6 == 6}\pythonIdx{==} yields \pythonilIdx{True}, while \pythonil{6 != 6}\pythonIdx{!=} yields \pythonilIdx{False}. |
| 36 | +The expression \pythonil{6 > 6}\pythonIdx{>} gives us \pythonilIdx{False}, but \pythonil{6 >= 6}\pythonIdx{>=} is \pythonilIdx{True}. |
| 37 | +\pythonil{6 < 6}\pythonIdx{<} is also \pythonilIdx{False} while \pythonil{6 <= 6} is, of course, \pythonilIdx{True}. |
| 38 | +While \pythonil{5 > 6}\pythonIdx{>} is not \pythonilIdx{True}, \pythonil{6 > 5}\pythonIdx{>} is. |
| 39 | +It is also possible to compare floating point numbers with integers and vice versa. |
| 40 | +\pythonil{5.5 == 5}\pythonIdx{==} is \pythonilIdx{False}, while \pythonilIdx{5.0 == 5} is \pythonilIdx{True}. |
| 41 | + |
| 42 | +Comparisons can also be chained: |
| 43 | +\pythonil{3 < 4 < 5 < 6} is \pythonilIdx{True}, because \pythonil{3 < 4} and \pythonil{4 < 5} and \pythonil{5 < 6}. |
| 44 | +\pythonil{5 >= 4 > 4 >= 3}, however, is \pythonilIdx{False}, because while \pythonil{5 >= 4} and \pythonil{4 >= 3}, it is not true that \pythonil{4 > 4}. |
| 45 | + |
| 46 | +If we check the type\pythonIdx{type} of \pythonilIdx{True}, it yields \pythonil{<class 'bool'>}, i.e., \pythonilIdx{bool}. |
| 47 | +The result of the expression \pythonil{5 == 5} is a \pythonilIdx{bool} as well. |
| 48 | + |
| 49 | +When talking about comparisons, there is one important, counter-intuitive exception to recall: |
| 50 | +The \pythonilIdx{nan}\pythonIdx{Not a Number} floating point value\pythonIdx{float} from \cref{sec:float:special}. |
| 51 | +In \cref{fig:floatMathInConsoleNaN}, we learned that \pythonil{nan == nan}\pythonIdx{==} is \pythonilIdx{False} and \pythonil{nan != nan}\pythonIdx{!=} is \pythonilIdx{True}. |
| 52 | +This is the only primitive value (to my knowledge) which is not equal to itself.% |
| 53 | +\endhsection% |
| 54 | +% |
| 55 | +% |
| 56 | +\hsection{Boolean Operators}% |
| 57 | +% |
| 58 | +\begin{figure}% |
| 59 | +\centering% |
| 60 | +% |
| 61 | +\subfloat[][% |
| 62 | +The truth table for the logical conjunction (\emph{logical and}\pythonIdx{and}): \pythonil{a and b}.% |
| 63 | +\label{fig:booleanAnd}% |
| 64 | +]{% |
| 65 | +~~~~% |
| 66 | +\begin{tabular}{|c|c|c|}% |
| 67 | +\hline% |
| 68 | +\pythonil{a}&\pythonil{b}&\pythonil{a and b}\\% |
| 69 | +\hline% |
| 70 | +\pythonilIdx{False}&\pythonilIdx{False}&\pythonilIdx{False}\\% |
| 71 | +\hline% |
| 72 | +\pythonilIdx{False}&\pythonilIdx{True}&\pythonilIdx{False}\\% |
| 73 | +\hline% |
| 74 | +\pythonilIdx{True}&\pythonilIdx{False}&\pythonilIdx{False}\\% |
| 75 | +\hline% |
| 76 | +\pythonilIdx{True}&\pythonilIdx{True}&\pythonilIdx{True}\\% |
| 77 | +\hline% |
| 78 | +\end{tabular}% |
| 79 | +~~~~% |
| 80 | +}% |
| 81 | +% |
| 82 | +\strut\hfill\strut% |
| 83 | +% |
| 84 | +\subfloat[][% |
| 85 | +The truth table for the logical disjunction (\emph{logical or})\pythonIdx{or}: \pythonil{a or b}.% |
| 86 | +\label{fig:booleanOr}% |
| 87 | +]{% |
| 88 | +~~~~% |
| 89 | +\begin{tabular}{|c|c|c|}% |
| 90 | +\hline% |
| 91 | +\pythonil{a}&\pythonil{b}&\pythonil{a or b}\\ |
| 92 | +\hline% |
| 93 | +\pythonilIdx{False}&\pythonilIdx{False}&\pythonilIdx{False}\\% |
| 94 | +\hline% |
| 95 | +\pythonilIdx{False}&\pythonilIdx{True}&\pythonilIdx{True}\\% |
| 96 | +\hline% |
| 97 | +\pythonilIdx{True}&\pythonilIdx{False}&\pythonilIdx{True}\\% |
| 98 | +\hline% |
| 99 | +\pythonilIdx{True}&\pythonilIdx{True}&\pythonilIdx{True}\\% |
| 100 | +\hline% |
| 101 | +\end{tabular}% |
| 102 | +~~~~% |
| 103 | +}% |
| 104 | +% |
| 105 | +\strut\hfill\strut% |
| 106 | +% |
| 107 | +\subfloat[][% |
| 108 | +The truth table for the logical negation (\emph{logical not}\pythonIdx{not}): \pythonil{not a}.% |
| 109 | +\label{fig:booleanNot}% |
| 110 | +]{% |
| 111 | +~~~~% |
| 112 | +\begin{tabular}{|c|c|}% |
| 113 | +\hline% |
| 114 | +\pythonil{a}&\pythonil{not a}\\% |
| 115 | +\hline% |
| 116 | +\pythonilIdx{False}&\pythonilIdx{True}\\% |
| 117 | +\hline% |
| 118 | +\pythonilIdx{True}&\pythonilIdx{False}\\% |
| 119 | +\hline% |
| 120 | +\end{tabular}% |
| 121 | +~~~~% |
| 122 | +}% |
| 123 | +% |
| 124 | +\caption{The truth tables for the Boolean operators \pythonilIdx{and}, \pythonilIdx{or}, and \pythonilIdx{not}.}% |
| 125 | +\label{fig:boolLogicTables}% |
| 126 | +\end{figure}% |
| 127 | +% |
| 128 | +\begin{figure}% |
| 129 | +\centering% |
| 130 | +\includegraphics[width=0.8\linewidth]{\currentDir/boolLogic}% |
| 131 | +\caption{The \pythonilIdx{bool} values can be combined with the Boolean logical operators \pythonilIdx{and}, \pythonilIdx{or}, and \pythonilIdx{not}.}% |
| 132 | +\label{fig:boolLogic}% |
| 133 | +\end{figure}% |
| 134 | +% |
| 135 | +The most common operations with Boolean values are the well-known Boolean logical operators \pythonilIdx{and}, \pythonilIdx{or}, and \pythonilIdx{not}. |
| 136 | +Their truth tables are illustrated in \cref{fig:boolLogicTables}.% |
| 137 | +% |
| 138 | +\begin{itemize}% |
| 139 | +% |
| 140 | +\item A Boolean conjunction, i.e., \pythonilIdx{and}, is \pythonilIdx{True} if and only both of its operands are also \pythonilIdx{True} and \pythonilIdx{False} otherwise, as shown in \cref{fig:booleanAnd}.% |
| 141 | +% |
| 142 | +\item A Boolean disjunction, i.e., \pythonilIdx{and}, is \pythonilIdx{True} if at least one of its two operands is \pythonilIdx{True} and \pythonilIdx{False} otherwise, as shown in \cref{fig:booleanOr}.% |
| 143 | +% |
| 144 | +\item The Boolean negation, i.e., \pythonilIdx{not}, is \pythonilIdx{True} if its operand is \pythonilIdx{False}. % |
| 145 | +Otherwise, it is \pythonilIdx{False}, as shown in \cref{fig:booleanNot}.% |
| 146 | +% |
| 147 | +\end{itemize}% |
| 148 | +% |
| 149 | +\begin{sloppypar}% |
| 150 | +In \cref{fig:boolLogic} we explore these three operators in the \python\ console. |
| 151 | +You can see that the operations can be used exactly as in the truth tables and yield the expected results. |
| 152 | +Additionally, you can of course nest and combine Boolean operators using parentheses\pythonIdx{(}\pythonIdx{)}. |
| 153 | +For example, \pythonil{(True or False) and ((False or True) or (False and False))} resolves to \pythonil{True and (True or False)}, which becomes \pythonil{True and True}, which ultimately becomes \pythonilIdx{True}. |
| 154 | +You can also combine Boolean expressions like comparisons using the logical operators: |
| 155 | +\pythonil{(5 < 4) or (6 < 9 < 8)} will be resolved to \pythonil{(False) or (False)}, which becomes \pythonilIdx{False}.% |
| 156 | +\end{sloppypar}% |
| 157 | +% |
| 158 | +\endhsection% |
| 159 | +% |
| 160 | +\hsection{Summary}% |
| 161 | +% |
| 162 | +Boolean values are very easy to understand and deal with. |
| 163 | +They can either be \pythonilIdx{True} or \pythonilIdx{False}. |
| 164 | +They can be combined using \pythonilIdx{and}, \pythonilIdx{or}, and \pythonilIdx{not}. |
| 165 | +And, finally, they are the results of comparison operators. |
| 166 | +Later, we will learn that Boolean decisions form the foundation for steering the control flow of programs.% |
| 167 | +% |
| 168 | +\endhsection% |
| 169 | +\endhsection% |
| 170 | +% |
0 commit comments