|
23 | 23 | %
|
24 | 24 | \FloatBarrier%
|
25 | 25 | \gitPythonAndOutput{\programmingWithPythonCodeRepo}{03_conditionals}{if_example.py}{--args format}{conditionals:if}{%
|
26 |
| -An example for using the \pythonilIdx{if} statement.}% |
| 26 | +An example of using the \pythonilIdx{if} statement.}% |
27 | 27 | %
|
28 | 28 | The first line begins with \pythonilIdx{if}~statement, followed by a Boolean expression, followed by a colon~(\pythonilIdx{:}).
|
29 | 29 | If -- and only if -- the Boolean expression evaluates to~\pythonilIdx{True}, then the \emph{indented} block of statements below the \pythonil{if} are executed.
|
|
66 | 66 | \hsection{The \texttt{if}\idxdots\texttt{else} Statement}%
|
67 | 67 | %
|
68 | 68 | \gitPythonAndOutput{\programmingWithPythonCodeRepo}{03_conditionals}{if_else_example.py}{--args format}{conditionals:if_else}{%
|
69 |
| -An example for using the \pythonil{if ... else}\pythonIdx{if{\idxdots}else} statement.}% |
| 69 | +An example of using the \pythonil{if ... else}\pythonIdx{if{\idxdots}else} statement.}% |
70 | 70 | %
|
71 | 71 | \gitPythonAndOutput{\programmingWithPythonCodeRepo}{03_conditionals}{if_else_nested.py}{--args format}{conditionals:if_else_nested}{%
|
72 |
| -An example for using nested \pythonil{if ... else}\pythonIdx{if{\idxdots}else!nested} statements.}% |
| 72 | +An example of using nested \pythonil{if ... else}\pythonIdx{if{\idxdots}else!nested} statements.}% |
73 | 73 | %
|
74 | 74 | The ability to perform some action if a given expression evaluates to \pythonil{True} is already nice.
|
75 | 75 | However, often we want to perform some action if the expression evaluates to \pythonil{True} and another action otherwise.
|
|
136 | 136 | \hsection{The \texttt{if}\idxdots\texttt{elif}\idxdots\texttt{else} Statement}%
|
137 | 137 | %
|
138 | 138 | \gitPythonAndOutput{\programmingWithPythonCodeRepo}{03_conditionals}{if_elif_example.py}{--args format}{conditionals:if_elif}{%
|
139 |
| -An example for using the \pythonil{if ... elif}\pythonIdx{if{\idxdots}elif{\idxdots}else} statement.}% |
| 139 | +An example of using the \pythonil{if ... elif}\pythonIdx{if{\idxdots}elif{\idxdots}else} statement.}% |
140 | 140 | %
|
141 | 141 | \gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/ruff.sh 03_conditionals if_else_nested.py}{conditionals:if_else_nested:ruff}{%
|
142 | 142 | The results of linting with \ruff\ of the program given in \cref{lst:conditionals:if_else_nested}.}%
|
143 | 143 | %
|
144 | 144 | \gitPythonAndOutput{\programmingWithPythonCodeRepo}{03_conditionals}{if_elif_nested.py}{--args format}{conditionals:if_elif_nested}{%
|
145 |
| -An example for using the nested \pythonil{if ... elif}\pythonIdx{if{\idxdots}elif{\idxdots}else!nested} statement based on the recommendations of the \ruff\ \pgls{linter} applied to \cref{lst:conditionals:if_else_nested}.}% |
| 145 | +An example of using the nested \pythonil{if ... elif}\pythonIdx{if{\idxdots}elif{\idxdots}else!nested} statement based on the recommendations of the \ruff\ \pgls{linter} applied to \cref{lst:conditionals:if_else_nested}.}% |
146 | 146 | %
|
147 | 147 | %
|
148 | 148 | In some cases, we need to query a sequence of alternatives in such a way that \pythonilIdx{else}~blocks would be nested over \pythonilIdx{else}~blocks over \pythonilIdx{else}~blocks, and so on.
|
|
218 | 218 | \label{sec:inlineIfThenElse}%
|
219 | 219 | %
|
220 | 220 | \gitPythonAndOutput{\programmingWithPythonCodeRepo}{03_conditionals}{if_else_could_be_inline.py}{--args format}{conditionals:if_else_could_be_inline}{%
|
221 |
| -An example for using the nested \pythonil{if ... else}\pythonIdx{if{\idxdots}elif{\idxdots}else} statements that could be inlined. % |
| 221 | +An example of using the nested \pythonil{if ... else}\pythonIdx{if{\idxdots}elif{\idxdots}else} statements that could be inlined. % |
222 | 222 | See \cref{lst:conditionals:inline_if_else} for the more compact inlined variant.}%
|
223 | 223 | %
|
224 | 224 | \gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/ruff.sh 03_conditionals if_else_could_be_inline.py}{conditionals:if_else_could_be_inline:ruff}{%
|
225 | 225 | The results of linting with \ruff\ of the program given in \cref{lst:conditionals:if_else_could_be_inline}.}%
|
226 | 226 | %
|
227 | 227 | \gitPythonAndOutput{\programmingWithPythonCodeRepo}{03_conditionals}{inline_if_else.py}{--args format}{conditionals:inline_if_else}{%
|
228 |
| -An example for using the inline \pythonil{if ... else}\pythonIdx{if{\idxdots}else!inline} expression to shorten \cref{lst:conditionals:if_else_could_be_inline}, which incorporates the suggestion by \ruff\ in \cref{exec:conditionals:if_else_could_be_inline:ruff}.}% |
| 228 | +An example of using the inline \pythonil{if ... else}\pythonIdx{if{\idxdots}else!inline} expression to shorten \cref{lst:conditionals:if_else_could_be_inline}, which incorporates the suggestion by \ruff\ in \cref{exec:conditionals:if_else_could_be_inline:ruff}.}% |
229 | 229 | %
|
230 | 230 | A very common use case of \pythonil{if...else}~statements is to assign values to variables.
|
231 | 231 | In \cref{lst:conditionals:if_else_could_be_inline} we display such a situation.
|
|
269 | 269 | Before this, we could only perform straightforward computations and calculate the results of simple functions.
|
270 | 270 | Now our variables can receive the result of a function~$A$ if the input meets a condition~$B$ and otherwise the result of a function~$C$.
|
271 | 271 | This is already quite nice.
|
272 |
| -For example, we can now implement and hard-code decision trees~\cite{RN2022AIAMA,SSBD2014UMLFTTA} and \cref{lst:conditionals:if_elif} is basically an example for that. |
| 272 | +For example, we can now implement and hard-code decision trees~\cite{RN2022AIAMA,SSBD2014UMLFTTA} and \cref{lst:conditionals:if_elif} is basically an example of that. |
273 | 273 | Still, the instructions in our programs are still executed in the sequence in which we wrote them down.
|
274 | 274 | While our control flow can now branch, it cannot perform anything more fancy and advanced {\dots} like looping back upon itself\dots%
|
275 | 275 | \endhsection%
|
|
0 commit comments