Skip to content

Commit 6367aed

Browse files
committed
added best practice for style guides
1 parent 39ce8db commit 6367aed

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

text/main/basics/collections/lists/lists.tex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@
9595

9696
We can change~\pythonil{cpy} by deleting its first element via~\pythonil{del cpy[0]}.
9797
\pythonil{numbers} will be unaffected by this and stays unchanged.
98-
Now, both \pythonil{cpy == numbers} and \pythonil{cpy is numbers}\pythonIdx{is} will be~\pythonil{False}.
99-
By the way, in the same manner as \pythonil{not in} is the opposite of the \pythonil{in}~operator, \pythonilIdx{not is} is the opposite of~\pythonilIdx{is}:
98+
Now, both \pythonil{cpy == numbers}\pythonIdx{==} and \pythonil{cpy is numbers}\pythonIdx{is} will be~\pythonil{False}.
99+
By the way, in the same manner as \pythonilIdx{not in} is the opposite of the \pythonil{in}~operator, \pythonilIdx{not is} is the opposite of~\pythonilIdx{is}:
100100
\pythonil{cpy is not numbers} yields \pythonil{True} because \pythonil{cpy} is not the same object as \pythonil{numbers}.
101101

102102
In \cref{lst:lists:lists_3}, we continue our journey through the magical land of \python\ \pythonils{list}.
@@ -109,7 +109,7 @@
109109
In \cref{sec:strBasicOperations}, we discussed string slicing.
110110
Lists can be sliced in pretty much the same way\pythonIdx{list!slicing}\pythonIdx{slicing}\pythonIdx{slice}~\cite{PSF:P3D:TPLR:S}.
111111
When slicing a list~\pythonil{l} or a string, you can provide either two or three values in the square brackets\pythonIdx{[\idxdots]}\pythonIdx{[i:j:k]}, i.e., either do~\pythonil{l[i:j]}\pythonIdx{[i:j]}\pythonIdx{slicing} or \pythonil{l[i:j:k]}.
112-
If \pythonil{j < 0}, then it is replaced with~\pythonil{len(l) - j}.
112+
If \pythonil{j < 0}, then it is replaced with~\pythonil{len(l) - j}\pythonIdx{len}.
113113
In both the two and three indices case, \pythonil{i} is the inclusive start index and \pythonil{j} is the exclusive end index, i.e., all elements with index~\pythonil{m} such that \pythonil{i <= m < j}.
114114
In other words, the slice will contain elements from~\pythonil{l} whose index is between~\pythonil{i} and~\pythonil{j}, including the element at index~\pythonil{i} but \emph{not} including the element at index~\pythonil{j}.
115115
If a third index~\pythonil{k} is provided, the it is the step length.

text/main/basics/simpleDataTypesAndOperations/introduction/introduction.tex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
We have also already learned our first two \python\ commands:%
55
%
66
\begin{itemize}%
7-
\item \pythonil{print("Hello World!")} prints the text \inQuotes{Hello World!} to the output.
8-
\item \pythonil{exit()} exits and terminates the \python\ interpreter.%
7+
\item \pythonil{print("Hello World!")}\pythonIdx{print} prints the text \inQuotes{Hello World!} to the output.
8+
\item \pythonil{exit()}\pythonIdx{exit} exits and terminates the \python\ interpreter.%
99
\end{itemize}%
1010
%
1111
Now, it would be very strange if the \pythonil{print} function could print \inQuotes{Hello World!}.
1212
That would not make much sense.
13-
\pythonil{print} expects one parameter.
13+
\pythonilIdx{print} expects one parameter.
1414
This parameter cannot just be anything.
1515
It must be a text.
1616

17-
The command \pythonil{exit}, on the other hand, can either have no parameter or one parameter.
17+
The command \pythonilIdx{exit}, on the other hand, can either have no parameter or one parameter.
1818
If it receives one parameter, this parameter will be the \pgls{exitCode} of the program.
1919
Here, \pythonil{0} indicates success.
2020
If no parameter is provided, this will be used as default value.
21-
We need to invoke \pythonil{exit} if we use the \python\ console in the \pgls{terminal} explicitly.
21+
We need to invoke \pythonilIdx{exit} if we use the \python\ console in the \pgls{terminal} explicitly.
2222
If we just run a program, then after the last instruction of the program was executed, then the interpreter will also terminate with \pgls{exitCode}~0.
2323
Indeed, when we executed our first program in \cref{sec:ourFirstProgram}, we saw exactly that happen in \cref{fig:firstProgram09programResult}.
2424
Different from the parameter of \pythonil{print}, which must be some text, the parameter of \pythonil{exit} needs to be a number.

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,25 @@
157157
Variable names should be lowercase, with words separated by underscores~\cite{PEP8}.%
158158
}
159159
%
160+
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.
161+
Before continuing further, let us revisit the deeper meaning behind them.
162+
Why is it important to style our code in a consistent way?
163+
Why can't we just write things down in any way that pleases us?%
164+
%
165+
\bestPractice{codeStyle}{%
166+
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.%
167+
}%
168+
For many programming languages, there exist comprehensive and clear style guides.
169+
Since we usually work collaboratively on larger projects, writing code in a consistent style is very important.
170+
Ideally, all collaborators can open a source code file and easily read and understand our code.
171+
If everybody writes code in different styles, maybe using different indentations or different naming conventions, reading code can become harder and even confusing.
172+
Therefore, style guides often tell us how to name things and how to structure code consistently.%
173+
%
174+
\bestPractice{PEP8}{%
175+
The most important style guide for the \python\ programming language is PEP8:~\citetitle{PEP8}~\cite{PEP8} at \citeurl{PEP8}. %
176+
\python\ code violating PEP8 is invalid \python code.%
177+
}%
178+
%
160179
\FloatBarrier%
161180
\endhsection%
162181
%
@@ -241,37 +260,37 @@
241260
\centering%
242261
%
243262
\subfloat[][%
244-
The file \textil{pi_liu_hui.py} opened in \pycharm.%
263+
The file \programUrl{variables:pi_liu_hui} opened in \pycharm.%
245264
\label{fig:liuHuiPiPyCharm1}%
246265
]{%
247266
\tightbox{\includegraphics[width=0.49\linewidth]{\currentDir/liuHuiPiPyCharm1}}%
248267
}%
249268
\hfill%
250269
%
251270
\subfloat[][%
252-
Left-clicking on \menu{Run `pi\_liu\_hui'} in the pop-up menu after right-clicking on \textil{pi_liu_hui.py}, or directly pressing \keys{\ctrl+\shift+F10}, to run the program.%
271+
Left-clicking on \menu{Run `pi\_liu\_hui'} in the pop-up menu after right-clicking on \programUrl{variables:pi_liu_hui}, or directly pressing \keys{\ctrl+\shift+F10}, to run the program.%
253272
\label{fig:liuHuiPiPyCharm2}%
254273
]{%
255274
\tightbox{\includegraphics[width=0.49\linewidth]{\currentDir/liuHuiPiPyCharm2}}%
256275
}%
257276
\\%
258277
%
259278
\subfloat[][%
260-
The output of the program \textil{pi_liu_hui.py} in \pycharm.%
279+
The output of the program \programUrl{variables:pi_liu_hui} in \pycharm.%
261280
\label{fig:liuHuiPiPyCharm3}%
262281
]{%
263282
\tightbox{\includegraphics[width=0.7\linewidth]{\currentDir/liuHuiPiPyCharm3}}%
264283
}%
265284
\\%
266285
%
267286
\subfloat[][%
268-
The output of the program \textil{pi_liu_hui.py} in the \ubuntu\ \pgls{terminal} (which you can open via~\ubuntuTerminal).%
287+
The output of the program \programUrl{variables:pi_liu_hui} in the \ubuntu\ \pgls{terminal} (which you can open via~\ubuntuTerminal).%
269288
\label{fig:liuHuiPiTerminal}%
270289
]{%
271290
\includegraphics[width=0.7\linewidth]{\currentDir/liuHuiPiTerminal}%
272291
}%
273292
%
274-
\caption{Running the program \textil{pi_liu_hui.py} from \cref{lst:variables:assignment} in \pycharm~(\cref{fig:assignmentPyCharm1,fig:assignmentPyCharm2,fig:assignmentPyCharm3}) or the \ubuntu\ \pgls{terminal}~(\cref{fig:assignmentTerminal}).}%
293+
\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}).}%
275294
\label{fig:variables:liuHuiPi}%
276295
\end{figure}%
277296
%

0 commit comments

Comments
 (0)