Skip to content

Commit d8c9d9c

Browse files
committed
first steps into loops
1 parent 1dd5d9d commit d8c9d9c

File tree

7 files changed

+81
-8
lines changed

7 files changed

+81
-8
lines changed

styles/listing.sty

+7
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,20 @@ $\downarrow$~~\expandafter\bashil{python3 #3}~~$\downarrow$%
272272
%
273273
%%
274274
%% The python syntax environment.
275+
%% Warning: If we do not use the minipage, this may or may not
276+
%% cause strange errors like:
277+
%% "! Argument of ? has an extra }."
275278
\lstnewenvironment{pythonSyntax}[1][true]{%
279+
\center%
280+
\minipage{\linewidth}%
276281
\let\@old@lst@visiblespace\lst@visiblespace%
277282
\def\lst@visiblespace{{\color{Gray}\@old@lst@visiblespace}}%
278283
\lstset{%
279284
style=python_style,%
280285
showspaces=#1%
281286
}}{%
282287
\let\lst@visiblespace\@old@lst@visiblespace%
288+
\endminipage%
289+
\endcenter%
283290
}%
284291
%

styles/myindex.sty

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
%
1010
\protected\gdef\pythonIdx#1{\index[python]{#1}}%
1111
\protected\gdef\pythonilIdx#1{\index[python]{#1}\pythonil{#1}}%
12+
\protected\gdef\pythonilsIdx#1{\index[python]{#1}\pythonils{#1}}%
1213
\makeindex%
1314
%
1415
%% dots without end-of-sentence spacing at the end

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
They work very similar to the strings we already discussed in \cref{sec:str}, but instead of characters, they can contain any kind of objects and they can be modified.%
66
%
77
\hsection{Basic Functionality and Examples}%
8+
\label{sec:lists:basicFunctions}%
89
%
910
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{02_collections}{lists_1.py}{--args format}{lists:lists_1}{%
1011
A first example for using lists in \python: creating, indexing, printing of and appending elements and other lists to lists.}%
@@ -103,7 +104,7 @@
103104
\pythonil{[5, 6, 7] * 3}\pythonIdx{*}\pythonIdx{list!*} therefore yields \pythonil{[5, 6, 7, 5, 6, 7, 5, 6, 7]}.
104105

105106
In \cref{sec:strBasicOperations}, we discussed string slicing.
106-
Lists can be sliced in pretty much the same way\pythonIdx{list!slicing}\pythonIdx{slicing}~\cite{PSF2024S}.
107+
Lists can be sliced in pretty much the same way\pythonIdx{list!slicing}\pythonIdx{slicing}\pythonIdx{slice}~\cite{PSF2024S}.
107108
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]}.
108109
If \pythonil{j < 0}, then it is replaced with~\pythonil{len(l) - j}.
109110
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}.
@@ -122,7 +123,7 @@
122123
It stops adding elements before reaching index~\pythonil{3}.
123124
Therefore, the result will be the new list~\pythonil{l7 = [7, 5, 6]}.
124125

125-
Notice that the slices we create are independent copies of ranges of the original lists.
126+
Notice that the slices\pythonIdx{slice} we create are independent copies of ranges of the original lists.
126127
The list~\pythonil{l7} is a slice from the list~\pythonil{l4}.
127128
If we modify it, e.g., set \pythonil{l7[1] = 12}, then we set the second element of~\pythonil{l7} to~\pythonil{12}.
128129
\pythonil{l7}~becomes~\pythonil{[7, 12, 6]}.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
Of course, using a negative index that would bring us out of the string's valid range, such as \pythonil{-6}, again yields an \pythonilIdx{IndexError}.
6161

6262
We can also obtain whole substrings by using index ranges, where the inclusive starting index and the \emph{exclusive} end index are separated by a~\pythonilIdx{:}.
63-
In other words, applying the index \pythonil{[a:b]}\pythonIdx{[i:j]}\pythonIdx{slicing} to a string results in all characters in the index range from \pythonil{a} to \pythonil{b - 1}.
64-
Doing this is called \emph{string slicing}\pythonIdx{str!slicing}\pythonIdx{slicing}.
63+
In other words, applying the index \pythonil{[a:b]}\pythonIdx{[i:j]}\pythonIdx{slicing}\pythonIdx{slice} to a string results in all characters in the index range from \pythonil{a} to \pythonil{b - 1}.
64+
Doing this is called \emph{string slicing}\pythonIdx{str!slicing}\pythonIdx{slicing}\pythonIdx{slice}.
6565
\pythonil{"Hello"[0:3]} yields a string composed of the characters at positions~0, 1, and~2 inside \pythonil{"Hello"}, i.e., \pythonil{"Hel"}.
6666
The end index is always excluded, so the character at index~3 is not part of the result.
6767
If we do \pythonil{"Hello"[1:3]}, we get \pythonil{"He"}, because only the characters at indices~1 and~2 are included.

text/main/controlFlow/conditionals/conditionals.tex

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
\hsection{Conditional Statements}%
22
%
3+
Conditional statements allow us to execute a piece of code if a certain Boolean expression evaluates to~\pythonilIdx{True}.
4+
This allows us to let the program make choices, to do one thing in one situation and another thing in a different situation.
5+
Conditionals are therefore the most fundamental control flow statement.%
6+
%
37
\hsection{The \texttt{if} Statement}%
4-
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{03_conditionals}{if_example.py}{--args format}{conditionals:if}{%
5-
An example for using the \pythonilIdx{if} statement.}%
68
%
79
A conditional statement is basically a piece of program code which will execute a set of instructions if a condition is met.
810
The condition is provided as the kind of Boolean expressions that we already discussed back in \cref{sec:bool}.
@@ -19,6 +21,10 @@
1921
# ...
2022
\end{pythonSyntax}
2123
%
24+
\FloatBarrier%
25+
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{03_conditionals}{if_example.py}{--args format}{conditionals:if}{%
26+
An example for using the \pythonilIdx{if} statement.}%
27+
%
2228
The first line begins with \pythonilIdx{if}~statement, followed by a Boolean expression, followed by a colon~(\pythonilIdx{:}).
2329
If -- and only if -- the Boolean expression evaluates to~\pythonilIdx{True}, then the \emph{indented} block of statements below the \pythonil{if} are executed.
2430
Notice that each of these conditionally executed statements is indented by four spaces compared to the~\pythonil{if}.
@@ -142,12 +148,12 @@
142148
In some cases, we need to query a sequence of alternatives in such a way that \pythonilIdx{else}~blocks would be nested over \pythonilIdx{else}~blocks over \pythonilIdx{else}~blocks, and so on.
143149
Since everytime we nest a conditional statement into another one we have to add four spaces of indentation, this would quickly fill the horizontal of our screens and look rather ugly.
144150
Therefore, the \pythonilIdx{elif} statement has been developed, which can replace an \pythonil{else} containing just another \pythonil{if}.
145-
The syntax of a combined \pythonil{if ... elilf} looks like this:%
151+
The syntax of a combined \pythonil{if ... elif} looks like this:%
146152
%
147153
\begin{pythonSyntax}
148154
if booleanExpression 1:
149155
conditional 1 statement 1
150-
conditional 2 statement 2
156+
conditional 1 statement 2
151157
# ...
152158
elif booleanExpression 2: # such block can be placed arbitrarily often
153159
conditional 2 statement 1

text/main/controlFlow/controlFlow.tex

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Now you will learn how to write programs that \emph{do} branch or loop.%
1010
%
1111
\hinput{conditionals}{conditionals.tex}%
12+
\hinput{loops}{loops.tex}%
1213
%
1314
\endhsection%
1415
%

text/main/controlFlow/loops/loops.tex

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
\hsection{Loops}%
2+
%
3+
When we are working with sequences of data, we do not just want to perform an action on one data element.
4+
We instead often want to apply the actions repetitively to all data elements.
5+
Loops allow us to do just that, to perform the same actions multiple times.%
6+
\pythonIdx{loop}%
7+
%
8+
\hsection{The \texttt{for} Loop Statement}%
9+
%
10+
\pythonIdx{loop!for}%
11+
The most basic such sequence in \python\ may be the \pythonilIdx{for}~loop, which has the following pattern:%
12+
%
13+
\begin{pythonSyntax}
14+
for loopVariable in sequence:
15+
loop body statement 1
16+
loop body statement 2
17+
# ...
18+
19+
normal statement 1
20+
normal statement 2
21+
# ...
22+
\end{pythonSyntax}
23+
%
24+
%
25+
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{04_loops}{for_loop_range.py}{--args format}{loops:for_loop_range}{%
26+
An example for using the \pythonilIdx{for} loop over a \pythonilsIdx{range} of integer numbers.}%
27+
%
28+
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{04_loops}{for_loop_pi_liu_hui.py}{--args format}{loops:for_loop_pi_liu_hui}{%
29+
A variant of \cref{lst:variables:pi_liu_hui} which uses a \pythonilIdx{for} loop instead of five copies of the same instructions.}%
30+
%
31+
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{04_loops}{for_loop_continue_break.py}{--args format}{loops:for_loop_continue_break}{%
32+
An example for the \pythonilIdx{continue} and \pythonilIdx{break} statements in a \pythonilIdx{for} loop.}%
33+
%
34+
The keyword~\pythonilIdx{for} is followed by a loop variable.
35+
Then comes the keyword~\pythonilIdx{in}, the \pythonil{sequence} we want to iterate over, and finally a colon~(\pythonilIdx{:}).
36+
This variable will iteratively take on the values in the \pythonil{sequence}.
37+
The loop body statements in the following, indented block are executed for each of these values.
38+
After the loop, we leave a blank line followed by the code that will be executed after the loop completes.
39+
40+
In its most simple form, the \pythonilIdx{for} loop is applied to a \pythonilIdx{range} of integer numbers.
41+
Ranges are sequences which work basically like slices\pythonIdx{slice} (see \cref{sec:lists:basicFunctions,sec:strBasicOperations}).
42+
\pythonil{range(5)} will give us a sequence of integers starting with~0 and reaching up to right \emph{before}~5, i.e., the integer range~\intRange{0}{4}.
43+
\pythonil{range(6, 9)} gives the sequence of integers starting with~6 and stopping right \emph{before}~9, i.e., the integer range~\intRange{6}{8}.
44+
Finally, \pythonil{range(20, 27, 2)} results in a sequence of integers that begins at~20, increments by~2 in each step, and ends right before~27.
45+
This is the sequence~$(20, 22, 24, 26)$.
46+
\pythonilsIdx{range}, like slices\pythonIdx{slice}, can also have negative increments:
47+
The \pythonil{range(40, 30, -3)} starts with~40 and stops before reaching~30 and decrements by~3 in each step.
48+
This is equivalent to the set~$(40, 37, 34, 31)$.
49+
50+
In \cref{lst:loops:for_loop_range}, we loop over exactly these ranges.
51+
In this listing, we try to create a dictionary (see \cref{sec:dictionaries}) where some integer numbers are mapped to their squares.
52+
We use four \pythonilIdx{for} loops to fill this dictionary with data.%
53+
%
54+
\endhsection%
55+
%
56+
\endhsection%
57+
%

0 commit comments

Comments
 (0)