Skip to content

Commit ccab3e7

Browse files
committed
some improvements
1 parent 7ff854c commit ccab3e7

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Die Slides zum Kurs in deutscher Sprache können unter <https://thomasweise.gith
3333
8. [Der Datentyp `float`](https://thomasweise.github.io/programmingWithPythonSlidesDE/08_float.pdf)
3434
9. [Zwischenspiel: Python Dokumentation und Informationsquellen](https://thomasweise.github.io/programmingWithPythonSlidesDE/09_dokumentation.pdf)
3535
10. [Der Datentyp `bool`](https://thomasweise.github.io/programmingWithPythonSlidesDE/10_bool.pdf)
36+
11. [Der Datentyp `str`](https://thomasweise.github.io/programmingWithPythonSlidesDE/11_str.pdf)
3637

3738
### 2.3. The Slides in English
3839
The slides for the course are available at <https://thomasweise.github.io/programmingWithPythonSlides> and also listed below.

bookbase

text/main/basics/simpleDataTypesAndOperations/str/str.tex

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
\hsection{Text Strings}%
22
\label{sec:str}%
33
%
4-
The fourth important datatype in \python\ are text strings.
4+
The fourth and last important basic datatype in \python\ are text strings.
55
Text strings are sequences of characters of an arbitrary length.
66
In \python, they are represented by the datatype \pythonilIdx{str}.
77
Indeed, we have already used it before, even in our very first example program back that simply printed \pythonil{"Hello World"} in \cref{lst:very_first_program} in \cref{sec:ourFirstProgram}.
@@ -16,10 +16,10 @@
1616
As \cref{exec:str_indexing} shows, there are two basic ways to specify a text string literal\pythonIdx{str!literal}:
1717
Either enclosed by double quotes, e.g., \pythonil{"Hello World!"}\pythonIdx{\textquotedbl\idxdots\textquotedbl} or enclosed by single quotes, e.g., \pythonil{'Hello World!'}\pythonIdx{\textquotesingle\idxdots\textquotesingle}.
1818
The quotation marks are only used to delimit the strings, i.e., to tell \python\ where the string begins or ends.
19-
They are not themselves part of the string.
20-
21-
\bestPractice{strDoubleQuote}{When defining a string literal, the double-quotation mark variant~(\pythonil{"..."}\pythonIdx{\textquotedbl\idxdots\textquotedbl}) may be preferred over the single-quotation mark variant~(\pythonil{'...'}\pythonIdx{\textquotesingle}); see also~\cref{bp:longstrDoubleQuote}.}
22-
19+
They are not themselves part of the string.%
20+
%
21+
\bestPractice{strDoubleQuote}{When defining a string literal, the double-quotation mark variant~(\pythonil{"..."}\pythonIdx{\textquotedbl\idxdots\textquotedbl}) may be preferred over the single-quotation mark variant~(\pythonil{'...'}\pythonIdx{\textquotesingle}).~(The \citetitle{PEP8}~\cite{PEP8} does not give a recommendation, but maybe for consistency with the~\citetitle{PEP257}~\cite{PEP257}, see also~\cref{bp:longstrDoubleQuote}.)}%
22+
%
2323
One basic operation is string concatenation\pythonIdx{str!concatenation}\pythonIdx{str!+}\pythonIdx{+}:
2424
\pythonil{"Hello" + ' ' + "World"}\pythonIdx{\textquotedbl\idxdots\textquotedbl}\pythonIdx{\textquotesingle\idxdots\textquotesingle} concatenates the three strings \pythonil{"Hello"}, \pythonil{" "}, and \pythonil{"World"}.
2525
The result is \pythonil{"Hello World"}\pythonIdx{\textquotedbl\idxdots\textquotedbl}.
@@ -68,6 +68,8 @@
6868
Finally, we can also omit the start index, in which case everything until right before the end index is returned.
6969
Therefore, \pythonil{"Hello"[:-2]} will return everything from the beginning of the string until right before the second-to-last character.
7070
This gives us \pythonil{"Hel"}.
71+
The slice~\pythonil{[1:8:2]} returns the substring starting at index~1 and ending before index~8, containing every second character.
72+
Applied to~\pythonil{"Hello World!"} it therefore yields~\pythonil{"el o"}.
7173
We will discussing slicing again later when discussing lists in~\cref{sec:lists}.
7274

7375
\gitEvalPython{str_basic_ops}{}{simple_datatypes/str_basic_ops.py}%
@@ -87,8 +89,11 @@
8789
It returns \pythonil{6}, because the \inQuotes{W} of \inQuotes{World} is the seventh character in this string and the indices are zero-based.
8890
Trying to find the \pythonil{"world"} in \pythonil{"Hello World!"} yields~\pythonil{-1}, however.
8991
\pythonil{-1} means that the string cannot be found.
92+
9093
We learn that string operations are case-sensitive\pythonIdx{str!case-sensitive}:
91-
\pythonil{"World" != "world"} would be \pythonilIdx{True}.
94+
The uppercase character~\inQuotes{W} is different from the lowercase character~\pythonil{w}.
95+
Therefore, \pythonil{"World" != "world"} is~\pythonilIdx{True}.
96+
Therefore, \pythonil{"world"} cannot be found in \pythonil{"Hello World!"}.
9297
We also learn that we need to be careful not to use the result of \pythonilIdx{find} as index in a string directly before checking that it is \pythonil{>= 0}!
9398
As you have learned, \pythonil{-1} is a perfectly fine index into a string, even though it means that the string we tried to find was not found.
9499

@@ -106,18 +111,22 @@
106111
If we want to search from the end of the string, we use \pythonilIdx{rfind}.
107112
\pythonil{"Hello World!".rfind("l")} gives us~\pythonil{9} directly.
108113
If we want to search for the~\inQuotes{l} before that one, we need to supply an inclusive starting and exclusive ending index of the range to be searched.
109-
\pythonil{"Hello World!".rfind("l", 0, 9)} searches for any~\inQuotes{l} from index~8 down to~0 and thus returns~\pythonil{3}.
114+
\pythonil{"Hello World!".rfind("l", 2, 9)} searches for any~\inQuotes{l} from index~8 down to~2 and thus returns~\pythonil{3}.
110115
\pythonil{"Hello World!".rfind("l", 0, 3)} gives us~\pythonil{2} and since there is no~\inQuotes{l} before that, \pythonil{"Hello World!".rfind("l", 0, 2)} yields~\pythonil{-1}.
111116
\end{sloppypar}%
112117
%
113118
\begin{sloppypar}%
114119
Another common operation is to replace substrings with something else.
115120
\pythonil{"Hello World!".replace("Hello", "Hi")}\pythonIdx{replace} replaces all occurrences of \inQuotes{"Hello"} in \inQuotes{Hello World} with \inQuotes{Hi}.
116-
The result is \pythonil{"Hi World!"} and \pythonil{"Hello Hello World!".replace("Hello", "Hi")} becomes \pythonil{"Hi Hi World!"}.
121+
The result is \pythonil{"Hi World!"} and \pythonil{"Hello World! Hello!".replace("Hello", "Hi")} becomes \pythonil{"Hi World! Hi!"}.
122+
It does not replace strings recursively, though.
123+
If you try to do \pythonil{"Hello World!".replace("Hello", "Hello! Hello!")}, then the \pythonil{"Hello"} is indeed replaced with \pythonil{"Hello! Hello!"}.
124+
This means that the new string now contains \pythonil{"Hello"} twice.
125+
These new occurrences are \emph{not} replaced, so the result remains as \pythonil{"Hello! Hello! World!"}.%
117126
\end{sloppypar}%
118127
%
119128
\begin{sloppypar}%
120-
Often, we want to remove all leading or trailing whitespace characters from a string.
129+
Often, we want to remove all leading or trailing whitespace characters~(spaces, newlines, tabs, \dots) from a string.
121130
The \pythonilIdx{strip} function does this for us:
122131
\pythonil{" Hello World! ".strip()} returns \pythonil{"Hello World!".strip()}, i.e., the same string, but with the leading and trailing space removed.
123132
If we only want to remove the spaces on the left-hand side, we use \pythonilIdx{lstrip} and if we only want to remove those on the right-hand side, we use \pythonilIdx{rstrip} instead.
@@ -134,6 +143,7 @@
134143

135144
Of course, these were just a small selection of the many string operations available in \python.
136145
You can find more in the \href{https://docs.python.org/3/library/stdtypes.html\#textseq}{official documentation}~\cite{PSF:P3D:TPSL:TSTS}.%
146+
\FloatBarrier%
137147
\endhsection%
138148
%
139149
\hsection{The str Function and f-strings}%
@@ -273,6 +283,7 @@
273283
For example, you could write \pythonil{f"\{23\ *\ sin(2\ -\ 5)\ =\ :.2f\}"} and then the \pythonil{.2f} format would be applied to the result of the expression, i.e., you would get \pythonil{"23 * sin(2 - 5) = -3.25"} as the result of the extrapolation.
274284

275285
You are now able to convert the results of your computations to nice text.%
286+
\FloatBarrier%
276287
\endhsection%
277288
%
278289
\hsection{Converting Strings to other Datatypes}%
@@ -298,6 +309,7 @@
298309
Finally, the function \pythonilIdx{bool}\pythonIdx{bool!function} converts the strings \pythonil{"True"} and \pythonil{"False"} to \pythonilIdx{True} and \pythonilIdx{False}, respectively.
299310
With this, you are also able to convert strings to data that you can use as input for your computations.%
300311
%
312+
\FloatBarrier%
301313
\endhsection%
302314
%
303315
%
@@ -359,6 +371,7 @@
359371
We already learned the sequences \inQuotes{\textbraceleft\textbraceleft}\pythonIdx{\textbraceleft\textbraceleft} and \inQuotes{\textbraceright\textbraceright}\pythonIdx{\textbraceright\textbraceright} that were designed for \pglspl{fstring} only.
360372
The backslash-based escape sequence we discussed in this section work for both \pglspl{fstring} and normal strings.%
361373
\pythonIdx{str!escaping}\pythonIdx{escaping}%
374+
\FloatBarrier%
362375
\endhsection%
363376
%
364377
\hsection{Multi-Line Strings}%
@@ -372,10 +385,10 @@
372385
Such string delimiters are used for multi-line strings.
373386
In such strings, you can insert linebreaks by hitting \keys{\enter} completely normally.
374387
You can use the escape sequences from the previous section as well.
375-
The main use case are \pglspl{docstring}, which we will discuss later, see, e.g., \cref{bp:module:docstrings}.
376-
377-
\bestPractice{longstrDoubleQuote}{When defining a multi-line string literal, the double-quotation mark variant~(\pythonil{"""..."""})\pythonIdx{\textquotedbl\textquotedbl\textquotedbl\idxdots\textquotedbl\textquotedbl\textquotedbl} is usually preferred over the single-quotation mark variant~(\pythonil{'''...'''}\pythonIdx{\textquotesingle\textquotesingle\textquotesingle})~\cite{PEP257,PEP8}.}
378-
388+
The main use case are \pglspl{docstring}, which we will discuss later, see, e.g., \cref{bp:module:docstrings}.%
389+
%
390+
\bestPractice{longstrDoubleQuote}{When defining a multi-line string literal, the double-quotation mark variant~(\pythonil{"""..."""})\pythonIdx{\textquotedbl\textquotedbl\textquotedbl\idxdots\textquotedbl\textquotedbl\textquotedbl} is preferred over the single-quotation mark variant~(\pythonil{'''...'''}\pythonIdx{\textquotesingle\textquotesingle\textquotesingle})~\cite{PEP257,PEP8}.}%
391+
%
379392
\cref{exec:str_multiline} shows what happens if we print such a multi-line string.
380393
We first create the string by writing the three lines \textil{This is a multi-line string.}, \textil{I can hit enter to begin a new line.}, and \textil{This linebreak is then part of the string.}.
381394
The first line begins with \pythonil{"""}\pythonIdx{\textquotedbl\textquotedbl\textquotedbl\idxdots\textquotedbl\textquotedbl\textquotedbl} and the last one ends with \pythonil{"""}\pythonIdx{\textquotedbl\textquotedbl\textquotedbl\idxdots\textquotedbl\textquotedbl\textquotedbl} as well.
@@ -384,6 +397,7 @@
384397
We can also have multi-line \pglspl{fstring}\pythonIdx{str!f}\pythonIdx{f-string!multi-line}.
385398
These then simply start with \pythonil{f"""}\pythonIdx{f\textquotedbl\textquotedbl\textquotedbl\idxdots\textquotedbl\textquotedbl\textquotedbl}.
386399
The example in \cref{exec:str_multiline} presents such a multi-line \pgls{fstring} with two expressions for \pgls{strinterpolation} which spans over three lines.%
400+
\FloatBarrier%
387401
\endhsection%
388402
%
389403
\hsection{Unicode and Character Representation}%
@@ -439,6 +453,7 @@
439453
Anyway, in \cref{exec:str_unicode}, we use the information obtained in \cref{fig:unicodeCharacterTableSubset} to print the Chinese text \inQuotes{你好。} standing for \inQuotes{Hello.} and pronounced as \inQuotes{N{\v{\i}} h{\v{a}}o.} as a unicode-escaped string.
440454
We found that the character for \inQuotes{你} has unicode number~4f60, \inQuotes{好} has~597d, and the big period~\inQuotes{。} has~3002.
441455
The string \pythonil{"\\u4f60\\u597d\\u3002"} then corresponds to the correct Chinese text~\inQuotes{你好。}.%
456+
\FloatBarrier%
442457
\endhsection%
443458
%
444459
\hsection{Summary}%

text/main/controlFlow/exceptions/exceptions.tex

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,35 @@
4646
The latter is what \python\ does in the examples mentioned initially:
4747
While it could simply ignore if we try to overwrite an element of a \pythonil{tuple}, it instead raises an~\pythonilIdx{TypeError}, for example.
4848
Personally, I am also a fan of this approach.
49-
49+
And the \python\ documentation is, too:%
50+
%
51+
\cquotation{PEP20}{%
52+
Errors should never pass silently. %
53+
Unless explicitly silenced.%
54+
}%
55+
%
5056
If we would follow the \pgls{GIGO} paradigm, then faulty data will propagate.
5157
Maybe the output of our function is fed as input into another function, whose output is then piped into another function, and so on.
5258
An error could then will lead to some crash down the line.
53-
If functions that are written under the assumption that \pgls{GIGO} is OK are paired with such that perform input sanitization, errors could remain unnoticed.
59+
Then again, if functions that are written under the assumption that \pgls{GIGO} is OK are paired with such that perform input sanitization, errors could remain unnoticed.
5460
The erroneous results could then become part of some actual, real-life decisions and designs.
5561
And even if found out, it will be extremely hard to discover where things went wrong in the long chain of computations and function calls.
62+
It could even be that faulty results are stored in files and then cause other tools to crash later.
63+
And \emph{later} could mean something like a week later.
64+
And \emph{other tools} could mean \emph{programs run by someone else in another department}.
65+
Good luck finding the piece of code that caused the error then.
5666

5767
Input sanitization could cover some error that happened earlier.
5868
This could even cause more errors, because it will allow other programmers to call our code with wrong values.
59-
Input sanitization encourages sloppy programming.
69+
Input sanitization also encourages sloppy programming.
6070

6171
This leaves raising an \pythonilIdx{Exception}.
6272
But what does that actually mean?
6373
Raising an \pythonilIdx{Exception} means two things:%
6474
%
6575
\begin{enumerate}%
6676
%
67-
\item We store information about the error and information about the current execution state (current line of code and the function call hierarchy) in an object (the \pythonilIdx{Exception}).%
77+
\item We store information about the error and information about the current execution state~(the current line of code and the function call hierarchy) in an object~(the \pythonilIdx{Exception}).%
6878
%
6979
\item The control flow immediately leaves the currently executed block of instructions as well as all calling blocks or functions.
7080
It jumps up in the call hierarchy until reaching code that handles the raised exception.

0 commit comments

Comments
 (0)