Skip to content

Commit 98aaf67

Browse files
committed
many improvements, especially with regard to loops
1 parent a40776f commit 98aaf67

File tree

19 files changed

+266
-131
lines changed

19 files changed

+266
-131
lines changed

LICENSE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The list of files not under the CC BY‑NC‑SA 4.0 license bu
1313
- text/main/introduction/pythonLogo.svg
1414
+ all screenshots from websites
1515
+ The illustration of LIU Hui (刘徽) in file "text/main/basics/variables/assignment/liu_hui.jpg" is from Wenqi Ying (应雯棋), editor. Commemoration of Ancient Chinese Mathematical Master Liu Hui for his Timeless Influence on Mathematics and Civilizational Exchange. [Issue 48 of CAST Newsletter](https://english.cast.org.cn/cms_files/filemanager/1941250207/attach/202412/8f23655a82364d19ad7874eb37b23035.pdf). China, Beijing (中国北京市): 中国科学技术协会 (China Association for Science and Technology, CAST), 2024. Permission was granted to include it in this material, but the copyright remains with CAST.
16+
+ The illustration of Hero(n) of Alexandria in file "text/main/controlFlow/loops/heronOfAlexandria.jpg" is taken from the article "[The Ancient Greek Who Invented the World's First Steam Turbine](https://greekreporter.com/2023/12/13/ancient-greek-world-first-steam-turbine)", where its caption states *Heron of Alexandria. Codex of Saint Gregory Nazianzenos. Greek manuscript of the ninth century. Public Domain*.
1617

1718

1819
## creative commons

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ Die Slides zum Kurs in deutscher Sprache können unter <https://thomasweise.gith
4141
16. [Gleichheit und Identität](https://thomasweise.github.io/programmingWithPythonSlidesDE/16_gleichheit_und_identität.pdf)
4242
17. [Listen](https://thomasweise.github.io/programmingWithPythonSlidesDE/17_listen.pdf)
4343
18. [Zwischenspiel: Der Linter Ruff](https://thomasweise.github.io/programmingWithPythonSlidesDE/18_ruff.pdf)
44-
19. [Tupels](https://thomasweise.github.io/programmingWithPythonSlidesDE/19_tupels.pdf)
44+
19. [Tupel](https://thomasweise.github.io/programmingWithPythonSlidesDE/19_tupel.pdf)
4545
20. [Mengen](https://thomasweise.github.io/programmingWithPythonSlidesDE/20_mengen.pdf)
4646
21. [Dictionaries bzw. Hash Maps](https://thomasweise.github.io/programmingWithPythonSlidesDE/21_dictionaries.pdf)
4747
22. [Alternativen mit `if`](https://thomasweise.github.io/programmingWithPythonSlidesDE/22_alternativen_mit_if.pdf)
4848
23. [Schleifen mit `for`](https://thomasweise.github.io/programmingWithPythonSlidesDE/23_schleifen_mit_for.pdf)
49+
24. [`enumerate` und Zwischenspiel: Pylint](https://thomasweise.github.io/programmingWithPythonSlidesDE/24_enumerate_und_pylint.pdf)
50+
25. [Schleifen mit `while`](https://thomasweise.github.io/programmingWithPythonSlidesDE/25_schleifen_mit_while.pdf)
4951

5052

5153
### 2.3. The Slides in English
@@ -82,6 +84,7 @@ The list of files not under the CC&nbsp;BY&#8209;NC&#8209;SA&nbsp;4.0 license bu
8284
- text/main/introduction/pythonLogo.svg
8385
+ all screenshots from websites
8486
+ The illustration of LIU Hui (刘徽) in file "text/main/basics/variables/assignment/liu_hui.jpg" is from Wenqi Ying (应雯棋), editor. Commemoration of Ancient Chinese Mathematical Master Liu Hui for his Timeless Influence on Mathematics and Civilizational Exchange. [Issue 48 of CAST Newsletter](https://english.cast.org.cn/cms_files/filemanager/1941250207/attach/202412/8f23655a82364d19ad7874eb37b23035.pdf). China, Beijing (中国北京市): 中国科学技术协会 (China Association for Science and Technology, CAST), 2024. Permission was granted to include it in this material, but the copyright remains with CAST.
87+
+ The illustration of Hero(n) of Alexandria in file "text/main/controlFlow/loops/heronOfAlexandria.jpg" is taken from the article "[The Ancient Greek Who Invented the World's First Steam Turbine](https://greekreporter.com/2023/12/13/ancient-greek-world-first-steam-turbine)", where its caption states *Heron of Alexandria. Codex of Saint Gregory Nazianzenos. Greek manuscript of the ninth century. Public Domain*.
8588

8689
You can download its newest version of the course material from <https://thomasweise.github.io/databases>.
8790
This version may change since this course and book both are work in progress.

text/back/scripts.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
\Cref{lst:bash:pylint} offers exactly the same functionality for \pylint.
7070
It checks if this tool is installed and installs it if not.
7171
It then applies \pylint\ to a the selected set of files, using a reasonable default configuration.
72-
\Cref{exec:loops:for_loop_no_enumerate:pylint} is an example of the output of this \pgls{linter}.
72+
\Cref{exec:loops:for_loop_no_enumerate_1:pylint} is an example of the output of this \pgls{linter}.
7373

7474
\Cref{lst:bash:pytest} is similarly structured, but instead of performing \emph{static} code analysis, it executes \pgls{unitTest} cases.
7575
The directory and list of \python\ files with the test cases are provided as command line arguments.

text/main/basics/collections/ruff/ruff.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
%
1212
Recently, we learned that static code analysis tools can help us to discover subtle problems in our programs.
1313
Obviously, when dealing with more complex datastructures like lists, there are also more potential problems, more mistakes that one could make.
14-
Let us look at the very short example program \textil{lists_error.py} in \cref{lst:lists:lists_error}.
14+
Let us look at the very short example program \programUrl{lists:lists_error} in \cref{lst:lists:lists_error}.
1515
The program consists of only two lines, \pythonil{my_list: list[str] = list([1, 2, 3])} and \pythonil{print(my_list)}.
1616
It does not have any \emph{error} in the strict sense.
1717
We can execute it just fine and it will produce the output \textil{[1, 2, 3]} as shown in \cref{exec:lists:lists_error}.
@@ -48,7 +48,7 @@
4848
\listingToolOutput{lists:lists_error:ruff}{%
4949
The results of linting with \ruff\ of the program given in \cref{lst:lists:lists_error}. (We used the script given in \cref{lst:bash:ruff} on \cpageref{lst:bash:ruff} to apply \ruff.)}%
5050
%
51-
Let us apply \ruff\ to the program \textil{lists_error.py} given in \cref{lst:lists:lists_error}.
51+
Let us apply \ruff\ to the program \programUrl{lists:lists_error} given in \cref{lst:lists:lists_error}.
5252
This can be done by executing the command \bashil{ruff check myfile.py}, where \textil{myfile.py} is the file to be checked~(you can also specify a directory).
5353
\ruff\ has many additional parameters, which are explained at~\cite{PSF:TPPIP:R,M2022RAEFPLACFWIR}.
5454
For example, if we want to analyze our code with respect to version~3.12 of \python, we would specify the command line argument~\textil{--target-version py312}.

text/main/basics/simpleDataTypesAndOperations/float/float.tex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
How can we deal with the fact that we cannot represent arbitrary fractional numbers exactly even in typical everyday cases like~\numberPi\ and~\numberE?
3131
How can we deal with the situation that real numbers exist as big as~$10^{300}$ and as small as~$10^{-300}$?
3232
With \pythonilIdx{float}, \python\ offers us one type for fractional numbers.
33-
This datatype represents numbers usually in the same internal structure as \pythonils{double} in the \pgls{C}~programming language~\cite{PSF:P3D:TPSL:NTIFC} -- it is based on the 64~bit IEEE~Standard 754 floating point number layout~\cite{IEEE2019ISFFPA,H1997IS7FPN}.
33+
This datatype represents numbers usually in the same internal structure as \pythonils{double} in the \pgls{C}~programming language~\cite{PSF:P3D:TPSL:NTIFC} -- it is based on the 64~bit IEEE~Standard 754 floating point number layout~\cite{IEEE2019ISFFPA,H1997IS7FPN,G1991WECSSKAFPA}.
3434
The idea behind this standard is exactly to be able to represent both very large numbers, like~$10^{300}$ and very small numbers, like~$10^{-300}$, while accepting that we cannot exactly represent~$10^{300}+10^{-300}$.
3535
In order to achieve this, the 64~bits are divided into three pieces, as illustrated in \cref{fig:floatIEEEStructure}.
3636
%
@@ -61,6 +61,7 @@
6161
\end{sloppypar}%
6262
%
6363
Luckily, you will never really need to know these exact information in your day-to-day programming work.
64+
There also exist many different formats of floating point numbers, using different numbers of bits for significand and exponent~\cite{IEEE2019ISFFPA,G1991WECSSKAFPA}.
6465
The important thing to remember is:
6566
Floating point numbers~(\pythonils{float}) can represent a wide range of different values.
6667
Their range is large but still limited.
@@ -425,7 +426,7 @@
425426
Or \pythonilIdx{nan}, which stands for, well, Not a Number\pythonIdx{Not a Number}.
426427

427428
\pythonilIdx{nan} means that the result of a computation is neither a finite number or infinite.
428-
It is the result of shenanigans such as \pythonil{inf - inf} or \pythonil{inf / inf} or \pythonil{0 * inf}.
429+
It is the result of shenanigans such as \pythonil{inf - inf} or \pythonil{inf / inf} or \pythonil{0 * inf}~\cite{G1991WECSSKAFPA}.
429430
A \pythonilIdx{nan} value anywhere in a computation infects the result of the computation to also become \pythonilIdx{nan}.
430431
\pythonil{nan + 1} remains \pythonilIdx{nan} and so does \pythonil{nan + inf}.
431432

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,37 @@
9696
\centering%
9797
%
9898
\subfloat[][%
99-
The file \textil{assignment.py} opened in \pycharm.%
99+
The file \programUrl{variables:assignment} opened in \pycharm.%
100100
\label{fig:assignmentPyCharm1}%
101101
]{%
102102
\tightbox{\includegraphics[width=0.49\linewidth]{\currentDir/assignmentPyCharm1}}%
103103
}%
104104
\hfill%
105105
%
106106
\subfloat[][%
107-
Left-clicking on \menu{Run `assignment'} in the pop-up menu after right-clicking on \textil{assignment.py}, or directly pressing \keys{\ctrl+\shift+F10}, to run the program.%
107+
Left-clicking on \menu{Run `assignment'} in the pop-up menu after right-clicking on \programUrl{variables:assignment}, or directly pressing \keys{\ctrl+\shift+F10}, to run the program.%
108108
\label{fig:assignmentPyCharm2}%
109109
]{%
110110
\tightbox{\includegraphics[width=0.49\linewidth]{\currentDir/assignmentPyCharm2}}%
111111
}%
112112
\\%
113113
%
114114
\subfloat[][%
115-
The output of the program \textil{assignment.py} in \pycharm.%
115+
The output of the program \programUrl{variables:assignment} in \pycharm.%
116116
\label{fig:assignmentPyCharm3}%
117117
]{%
118118
\tightbox{\includegraphics[width=0.7\linewidth]{\currentDir/assignmentPyCharm3}}%
119119
}%
120120
\\%
121121
%
122122
\subfloat[][%
123-
The output of the program \textil{assignment.py} in the \ubuntu\ \pgls{terminal} (which you can open via~\ubuntuTerminal).%
123+
The output of the program \programUrl{variables:assignment} in the \ubuntu\ \pgls{terminal} (which you can open via~\ubuntuTerminal).%
124124
\label{fig:assignmentTerminal}%
125125
]{%
126126
\includegraphics[width=0.7\linewidth]{\currentDir/assignmentTerminal}%
127127
}%
128128
%
129-
\caption{Running the program \textil{assignment.py} from \cref{lst:variables:assignment} in \pycharm~(\cref{fig:assignmentPyCharm1,fig:assignmentPyCharm2,fig:assignmentPyCharm3}) or the \ubuntu\ \pgls{terminal}~(\cref{fig:assignmentTerminal}).}%
129+
\caption{Running the program \programUrl{variables:assignment} from \cref{lst:variables:assignment} in \pycharm~(\cref{fig:assignmentPyCharm1,fig:assignmentPyCharm2,fig:assignmentPyCharm3}) or the \ubuntu\ \pgls{terminal}~(\cref{fig:assignmentTerminal}).}%
130130
\label{fig:variables:assignment}%
131131
\end{figure}%
132132
%
@@ -172,18 +172,18 @@
172172
(Do you remember a method, to get this output even more easily?)%
173173
\end{sloppypar}%
174174
%
175-
This first program is stored in a file named~\textil{assignment.py}.
175+
This first program is stored in a file named~\programUrl{variables:assignment}.
176176
To execute it, you have two choices:
177177
You can do this in the \pgls{terminal} or using \pycharm.%
178178
%
179179
\begin{sloppypar}%
180180
Under \ubuntu\ \linux, you open the \pgls{terminal} by pressing \ubuntuTerminal, under \microsoftWindows\ you instead \windowsTerminal.
181-
Then you enter the folder where the program \textil{assignment.py} is stored using the command~\bashil{cd}.
181+
Then you enter the folder where the program \programUrl{variables:assignment} is stored using the command~\bashil{cd}.
182182
Then you would execute the command~\bashil{python3 assignment.py} to run the \python\ interpreter, as illustrated in \cref{fig:assignmentTerminal}.%
183183
\end{sloppypar}%
184184
%
185185
Alternatively, you can open the program file in \pycharm\ \pgls{ide}, as sketched in \cref{fig:assignmentPyCharm1}.
186-
You would then right-click on the file \textil{assignment.py} in the project tree view.
186+
You would then right-click on the file \programUrl{variables:assignment} in the project tree view.
187187
In the popup-menu that opens, you would left-click on \menu{Run `assignment'} as shown in \cref{fig:assignmentPyCharm2}.
188188
As a shortcut, you can also simply press~\keys{\ctrl+\shift+F10}.
189189
Either way, \pycharm\ will run the program and the output appears in \cref{fig:assignmentPyCharm3}.
@@ -361,7 +361,7 @@
361361
Now that we have learned some programming, we do no longer need to type the numbers and computation steps into a calculator.
362362
We also do not need to use \python\ as calculator.
363363
Instead, we can enter all the commands needed for the computation into a program file.
364-
We will call it~\textil{pi_liu_hui.py}, as illustrated in \cref{lst:variables:pi_liu_hui}.
364+
We will call it~\programUrl{variables:pi_liu_hui}, as illustrated in \cref{lst:variables:pi_liu_hui}.
365365

366366
We begin by setting the initial number of edges \pythonil{e = 6} and the initial side length of the $e$\nobreakdashes-gon to \pythonil{s = 1}, because we still keep with the choice of~$\liuhuir=1$.
367367
In each iteration of the approximation, we simply set \pythonil{e *= 2}\pythonIdx{*=}, which is equivalent to \pythonil{e = e * 2}, to double the number of edges.
@@ -377,7 +377,7 @@
377377

378378
For your convenience, we also showed the results when executing the program in \pycharm\ or the \ubuntu\ \pgls{terminal} in \cref{fig:variables:liuHuiPi}.
379379
To open a \pgls{terminal} under \ubuntu\ \linux, you would press~\ubuntuTerminal, whereas under \microsoftWindows, you~\windowsTerminal.
380-
With the command \bashil{cd}, you would enter the directory where our program \textil{pi_liu_hui.py} is located.
380+
With the command \bashil{cd}, you would enter the directory where our program \programUrl{variables:pi_liu_hui} is located.
381381
You would then type in \bashil{python3 pi_liu_hui.py} and hit~\keys{\enter}.
382382
As you can see in \cref{fig:liuHuiPiTerminal}, you will get the same output as given in \cref{exec:variables:pi_liu_hui}.
383383

text/main/basics/variables/errorsInIde/errorsInIde.tex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
\centering%
1010
%
1111
\subfloat[][%
12-
We run the program \textil{assignment_wrong.py} given in \cref{lst:variables:assignment_wrong} in the \pycharm\ \pgls{ide} by clicking on the \pycharmRun\ button or by pressing~\keys{\shift+F10}.%
12+
We run the program \programUrl{variables:assignment_wrong} given in \cref{lst:variables:assignment_wrong} in the \pycharm\ \pgls{ide} by clicking on the \pycharmRun\ button or by pressing~\keys{\shift+F10}.%
1313
\label{fig:errorsInIde01runProgram}%
1414
]{\tightbox{\includegraphics[width=0.9\linewidth]{\currentDir/errorsInIde01runProgram}}}%
1515
%
@@ -19,7 +19,7 @@
1919
The output in the run window is the same as given in \cref{exec:variables:assignment_wrong}. %
2020
It is the \emph{first} way to find out what went wrong. %
2121
It tells us what went wrong and even suggests how to fix it:~\emph{\pythonilIdx{NameError}: name \inSQuotes{\pythonil{intvar}} is not defined. Did you mean: \inSQuotes{\pythonil{int_var}}?} %
22-
It also tells us the exact file and line where the error occurred, namely in line~12 of file \textil{assignment_wrong.py}.%
22+
It also tells us the exact file and line where the error occurred, namely in line~12 of file \programUrl{variables:assignment_wrong}.%
2323
\label{fig:errorsInIde02exception}%
2424
]{\tightbox{\includegraphics[width=0.9\linewidth]{\currentDir/errorsInIde02exception}}}%
2525
%
@@ -115,12 +115,12 @@
115115
%
116116
%
117117
\hsection{Finding the Error by using the Program Output and Exceptions}%
118-
In \cref{lst:variables:assignment_wrong}, we prepared program \textil{assignment_wrong.py}, a variant of \textil{assignment.py}~(\cref{lst:variables:assignment}) with an error.
118+
In \cref{lst:variables:assignment_wrong}, we prepared program \programUrl{variables:assignment_wrong}, a variant of \programUrl{variables:assignment}~(\cref{lst:variables:assignment}) with an error.
119119
For the sake of the example, let us assume that the programmer made a typo in line~12 of the program:
120120
They misspelled \pythonil{int_var} as \pythonil{intvar}.
121121
Executing the program with the error leads to the output given in \cref{exec:variables:assignment_wrong}.
122122

123-
We can also open the program \textil{assignment_wrong.py} given in the \pycharm\ \pgls{ide}.
123+
We can also open the program \programUrl{variables:assignment_wrong} given in the \pycharm\ \pgls{ide}.
124124
We execute this program manually by clicking on the \pycharmRun\ button or by pressing~\keys{\shift+F10} in \cref{fig:errorsInIde01runProgram}.
125125
As you can see, the output in the run window is the same as given in \cref{exec:variables:assignment_wrong}~(\cref{fig:errorsInIde02exception}).
126126

@@ -133,7 +133,7 @@
133133

134134
The \python\ interpreter then checks whether some similar name exists.
135135
It found that there is a variable named \pythonil{int_var}.
136-
Even more, it also tells us the exact file and line where the error occurred, namely in line~12 of file \textil{assignment_wrong.py}!
136+
Even more, it also tells us the exact file and line where the error occurred, namely in line~12 of file \programUrl{variables:assignment_wrong}!
137137
With this information, we have a good chance of finding the mistake.
138138

139139
The so-called \pythonilIdx{Exception} \pgls{stackTrace} that it prints to \pgls{stderr} thus not just tells us that there was an error.

0 commit comments

Comments
 (0)