|
37 | 37 | By the way, this is how we do it in this book: |
38 | 38 | We learn new programming concepts, but try to always intersperse important best practices.% |
39 | 39 | % |
| 40 | +% |
40 | 41 | \hsection{A Simple Example of Variable Assignment and Comments in the Code}% |
| 42 | +% |
41 | 43 | \gitLoadAndExecPython{variables:assignment}{}{variables}{assignment.py}{}% |
42 | 44 | \listingPythonAndOutput{variables:assignment}{% |
43 | 45 | A \python\ program showing some examples for variable assignments.}{}% |
|
132 | 134 | \Cref{lst:variables:assignment} shows the source code of our very first commented program. |
133 | 135 | This program does not do anything useful, but it illustrates how variables can be used. |
134 | 136 | It begins by assigning the \pythonilIdx{int} value~\pythonil{1} to a variable named~\pythonil{int_var}. |
135 | | -We could have chosen any other name as well, as long as it does not contain spaces, e.g., \pythonil{my_value}, \pythonil{cow}, \pythonil{race_car}. |
| 137 | +We could have chosen any other name for the variable as well, e.g., \pythonil{my_value}, \pythonil{cow}, \pythonil{race_car}, as long as it does not contain special characters like spaces or line breaks. |
136 | 138 | But we chose \pythonil{int_var}. |
137 | 139 | The \pythonilIdx{=} assigns the value~\pythonil{1} to \pythonil{int_value}. |
138 | 140 | As \cref{fig:variable:assignment1} illustrates, the value~\pythonil{1} will now be stored somewhere in memory and \pythonil{int_var} is a name that points to this memory location.% |
139 | 141 | % |
140 | 142 | \begin{sloppypar}% |
141 | 143 | We can use \pythonil{int_var} just like any other value. |
142 | 144 | For example, we can compute \pythonil{2 + int_var} and pass the result to the \pythonilIdx{print} function. |
143 | | -This will then print \pythonil{3} to the \pgls{stdout} of our program. |
| 145 | +This will then print the text~\textil{3} to the \pgls{stdout} of our program. |
144 | 146 | We can also use \pythonil{int_var} in \pglspl{fstring}\pythonIdx{f-string}\pythonIdx{str!f} about which we learned back in \cref{sec:fstrings}. |
145 | | -\pythonil{f"int_var has value \{int_var\}."} will render to \pythonil{"int_var has value 1."}.% |
| 147 | +\pythonil{f"int_var has value \{int_var\}."} will be \glslink{strinterpolation}{interpolated} to \pythonil{"int_var has value 1."}.% |
146 | 148 | \end{sloppypar}% |
147 | 149 | % |
148 | 150 | Variables are called \emph{variables} and not \emph{constants} because we can change their value. |
149 | 151 | Hence, we can update \pythonil{int_var} and give it a new value. |
150 | 152 | For example, we can do \pythonil{int_var = (3 * int_var ) + 1}. |
151 | | -As sketched in \cref{fig:variable:assignment2}, this will update \pythonil{int_var} to now hold the result of the computation \pythonil{(3 * int_var) + 1}. |
| 153 | +This will update \pythonil{int_var} to now hold the result of the computation \pythonil{(3 * int_var) + 1}. |
152 | 154 | In this computation, the current (old) value of \pythonil{int_var} is used. |
153 | 155 | It therefore corresponds to computing \pythonil{(3 * 1) + 1}, which equals~\pythonil{4}. |
154 | | -This value is stored somewhere in memory and \pythonil{int_var} points to it. |
155 | | -Doing \pythonil{print(f"int_var is now \{int_var\}.")} will print \textil{int_var is now 4.} to the \pgls{stdout}. |
| 156 | +This value is stored somewhere in memory and \pythonil{int_var} points to it, as sketched in \cref{fig:variable:assignment2}. |
156 | 157 | The value \pythonil{1} is now no longer referenced. |
157 | 158 | Eventually, the \python\ interpreter could free the corresponding memory to use it for something else. |
| 159 | +Doing \pythonil{print(f"int_var is now \{int_var\}.")} will print \textil{int_var is now 4.} to the \pgls{stdout}. |
158 | 160 |
|
159 | 161 | Ofcourse, we can have multiple variables. |
160 | 162 | The command \pythonil{float_var = 3.5} creates a variable named \pythonil{float_var}. |
161 | 163 | It also allocates a piece of memory, writes the floating point value \pythonil{3.5} into it, and lets \pythonil{float_var} point to that piece of memory, as illustrated in \cref{fig:variable:assignment3}. |
162 | 164 | We can use this variable in an \pgls{fstring}\pythonIdx{f-string}\pythonIdx{str!f} as well: |
163 | | -\pythonil{print(f"float_var has value \{float_var\}.")} is interpolated to \pythonil{"float_var has value 3.5."}.% |
| 165 | +\pythonil{print(f"float_var has value \{float_var\}.")} is \glslink{strinterpolation}{interpolated} to \pythonil{"float_var has value 3.5."}.% |
164 | 166 | % |
165 | 167 | \begin{sloppypar}% |
166 | 168 | In a final step, we create a third variables with the name \pythonil{new_var} by computing \pythonil{new_var = float_var * int_var}. |
167 | | -The result is \pythonil{3.5 * 4}, i.e., \pythonil{14.0}, \pythonilIdx{float} value. |
| 169 | +The result is \pythonil{3.5 * 4}, i.e., the \pythonilIdx{float} value~\pythonil{14.0}. |
168 | 170 | \cref{fig:variable:assignment3} illustrates this variable assignment step. |
169 | | -Finally, \pythonil{print(f"new_var = \{new_var\}." )} then prints \textil{new_var = 14.0.}.% |
| 171 | +Finally, \pythonil{print(f"new_var = \{new_var\}." )} then prints \textil{new_var = 14.0.}. |
| 172 | +(Do you remember a method, to get this output even more easily?)% |
170 | 173 | \end{sloppypar}% |
171 | 174 | % |
172 | 175 | This first program is stored in a file named~\textil{assignment.py}. |
|
178 | 181 | You would then right-click on the file \textil{assignment.py} in the project tree view. |
179 | 182 | In the popup-menu that opens, you would left-click on \menu{Run `assignment'} as shown in \cref{fig:assignmentPyCharm2}. |
180 | 183 | As a shortcut, you can also simply press~\keys{\ctrl+\shift+F10}. |
181 | | -Either way, \pycharm\ will run the program and the output appears in \cref{fig:assignmentPyCharm}. |
| 184 | +Either way, \pycharm\ will run the program and the output appears in \cref{fig:assignmentPyCharm3}. |
182 | 185 | As you see, the full \acrfull{stdout} of the complete program given in \cref{exec:variables:assignment} is identical to what we get from the manual execution in either the \pgls{terminal} or \pycharm. |
183 | 186 |
|
184 | 187 | We are not completely done yet, though. |
|
193 | 196 | Matter of fact, we now have seen some best practices on styling our code, e.g., \cref{bp:variableNames,bp:longstrDoubleQuote,bp:comments}, and many more such best practices will follow. |
194 | 197 | Before continuing further, let us therefore revisit the deeper meaning behind them. |
195 | 198 | Why is it important to style our code in a consistent way? |
196 | | -Why can't we just write things down in any way that pleases us?% |
| 199 | +Why can't we just write things down in any way that pleases us? |
| 200 | + |
| 201 | +Well, because following best practices is nothing that can be done \inQuotes{later.} |
| 202 | +You will never have the time to revisit and improve the style of your old code. |
| 203 | +It is also nothing that you can just switch over to. |
| 204 | +If you have learned doing a certain thing in a certain way, it will always be hard to switch over to a different way. |
| 205 | +If an apprentice in a kitchen is not taught to wash their hands before beginning to prepare meals, then they will not simply begin doing that consistently after being told to do it once after they join a new restaurant. |
| 206 | +Following style guides and best practices is a habit. |
| 207 | +And we need to nurture this habit right from the start.% |
197 | 208 | % |
198 | 209 | \bestPractice{codeStyle}{% |
199 | 210 | Regardless which programming language you are using, it is important to write code and scripts in a consistent style, to use a consistent naming scheme for all things that can be named, and to follow the generally established best practices and norms for that language.% |
|
245 | 256 |
|
246 | 257 | Well, we I say \inQuotes{we can compute it}, then the question \inQuotes{How?} immediately arises. |
247 | 258 | One particularly ingenious answer was given by the Chinese mathematician LIU Hui~(刘徽) somewhere in the third century~AD~\cite{OR2003LH} in his commentary to the famous Chinese mathematics book \emph{Jiu Zhang Suanshu}~(九章算术)~\cite{OR2003LH,SCL1999TNCOTMACAC,S1998LHATFGAOCM,D2010AALHOCAS,C2002LFLHADWTDM}. |
248 | | -In \cref{fig:liuHuiCircle}, we show how~\numberPi, i.e., the ratio of the circumference and the diameter of a circle can be approximated by inscribing regular~$e$\nobreakdashes-gons into a circle. |
249 | | -The corners of the $e$\nobreakdashes-gons lie on the circle. |
| 259 | +In \cref{fig:liuHuiCircle}, we show how~\numberPi, i.e., the ratio of the circumference and the diameter of a circle can be approximated. |
| 260 | +The idea of LIU Hui~(刘徽) was to inscribe regular~$e$\nobreakdashes-gons with an increasing number~$e$ of edges into a circlem such that the corners of the $e$\nobreakdashes-gons lie on the circle. |
250 | 261 |
|
251 | 262 | We start with a hexagon~($e=6$) where the radius~\liuhuir\ is equal to the radius of the circle. |
252 | 263 | All the $e$~edges~\liuhuiss\ of this hexagon then have length~\liuhuir\ as well. |
|
324 | 335 | \includegraphics[width=0.7\linewidth]{\currentDir/liuHuiPiTerminal}% |
325 | 336 | }% |
326 | 337 | % |
327 | | -\caption{Running the program \programUrl{variables:pi_liu_hui} from \cref{lst:variables:assignment} in \pycharm~(\cref{fig:assignmentPyCharm1,fig:assignmentPyCharm2,fig:assignmentPyCharm3}) or the \ubuntu\ \pgls{terminal}~(\cref{fig:assignmentTerminal}).}% |
| 338 | +\caption{Running the program \programUrl{variables:pi_liu_hui} from \cref{lst:variables:assignment} in \pycharm~(\cref{fig:liuHuiPiPyCharm1,fig:liuHuiPiPyCharm2,fig:liuHuiPiPyCharm3}) or the \ubuntu\ \pgls{terminal}~(\cref{fig:assignmentTerminal}).}% |
328 | 339 | \label{fig:variables:liuHuiPi}% |
329 | 340 | \end{figure}% |
330 | 341 | % |
|
0 commit comments