Skip to content

Commit 4070e97

Browse files
committed
minor improvements and fixes
1 parent 122a32b commit 4070e97

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

text/main/basics/variables/assignment/assignment.tex

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
By the way, this is how we do it in this book:
3838
We learn new programming concepts, but try to always intersperse important best practices.%
3939
%
40+
%
4041
\hsection{A Simple Example of Variable Assignment and Comments in the Code}%
42+
%
4143
\gitLoadAndExecPython{variables:assignment}{}{variables}{assignment.py}{}%
4244
\listingPythonAndOutput{variables:assignment}{%
4345
A \python\ program showing some examples for variable assignments.}{}%
@@ -132,41 +134,42 @@
132134
\Cref{lst:variables:assignment} shows the source code of our very first commented program.
133135
This program does not do anything useful, but it illustrates how variables can be used.
134136
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.
136138
But we chose \pythonil{int_var}.
137139
The \pythonilIdx{=} assigns the value~\pythonil{1} to \pythonil{int_value}.
138140
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.%
139141
%
140142
\begin{sloppypar}%
141143
We can use \pythonil{int_var} just like any other value.
142144
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.
144146
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."}.%
146148
\end{sloppypar}%
147149
%
148150
Variables are called \emph{variables} and not \emph{constants} because we can change their value.
149151
Hence, we can update \pythonil{int_var} and give it a new value.
150152
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}.
152154
In this computation, the current (old) value of \pythonil{int_var} is used.
153155
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}.
156157
The value \pythonil{1} is now no longer referenced.
157158
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}.
158160

159161
Ofcourse, we can have multiple variables.
160162
The command \pythonil{float_var = 3.5} creates a variable named \pythonil{float_var}.
161163
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}.
162164
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."}.%
164166
%
165167
\begin{sloppypar}%
166168
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}.
168170
\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?)%
170173
\end{sloppypar}%
171174
%
172175
This first program is stored in a file named~\textil{assignment.py}.
@@ -178,7 +181,7 @@
178181
You would then right-click on the file \textil{assignment.py} in the project tree view.
179182
In the popup-menu that opens, you would left-click on \menu{Run `assignment'} as shown in \cref{fig:assignmentPyCharm2}.
180183
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}.
182185
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.
183186

184187
We are not completely done yet, though.
@@ -193,7 +196,15 @@
193196
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.
194197
Before continuing further, let us therefore revisit the deeper meaning behind them.
195198
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.%
197208
%
198209
\bestPractice{codeStyle}{%
199210
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,8 +256,8 @@
245256

246257
Well, we I say \inQuotes{we can compute it}, then the question \inQuotes{How?} immediately arises.
247258
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.
250261

251262
We start with a hexagon~($e=6$) where the radius~\liuhuir\ is equal to the radius of the circle.
252263
All the $e$~edges~\liuhuiss\ of this hexagon then have length~\liuhuir\ as well.
@@ -324,7 +335,7 @@
324335
\includegraphics[width=0.7\linewidth]{\currentDir/liuHuiPiTerminal}%
325336
}%
326337
%
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}).}%
328339
\label{fig:variables:liuHuiPi}%
329340
\end{figure}%
330341
%

0 commit comments

Comments
 (0)