Skip to content

Commit c6eaed6

Browse files
committed
several improvements
1 parent 6fe8692 commit c6eaed6

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Die Slides zum Kurs in deutscher Sprache können unter <https://thomasweise.gith
3535
10. [Der Datentyp `bool`](https://thomasweise.github.io/programmingWithPythonSlidesDE/10_bool.pdf)
3636
11. [Der Datentyp `str`](https://thomasweise.github.io/programmingWithPythonSlidesDE/11_str.pdf)
3737
12. [`None`](https://thomasweise.github.io/programmingWithPythonSlidesDE/12_none.pdf)
38+
13. [Variablen: Wertzuweisung](https://thomasweise.github.io/programmingWithPythonSlidesDE/13_variablen_wertzuweisung.pdf)
3839

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

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,13 @@
280280
We can simply write \pythonil{f"\{5\ + 4\ = \}"}, which, too is evaluated to \pythonil{"5 + 4 = 9"}
281281
The more complex \pythonil{f"\{23\ *\ sin(2\ -\ 5)\ =\ \}"} becomes \pythonil{"23\ *\ sin(2\ -\ 5)\ =\ -3.245760185376946"}.
282282
One cool feature of this kind of expression-to-string conversation is that you can add the other format specifiers we discussed earlier after the \pythonil{=}.
283-
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.
284-
283+
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.%
284+
%
285+
\begin{sloppypar}%
285286
We can also convert our last example of the \pythonil{str}\nobreakdashes-function, \pythonil{str(1 < 5) + " is True and " + str(1 + 5) + " = 6."}, to an \pgls{fstring} and get \pythonil{f"\{1 < 5\} is True and \{1 + 5\} = 6."}.
286-
This not just looks nicer, but it is also faster.
287-
287+
This not just looks nicer, but it is also faster.%
288+
\end{sloppypar}%
289+
%
288290
In the original example, \python\ needs to perform three string concatenation operations via the \pythonil{+}~operator.
289291
It first creates the result of \pythonil{str(1 < 5)}, which is a string.
290292
\pythonil{" is True and "} is also a string by definition.

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

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,39 @@
33
%
44
Like in mathematics, a variable in \python\ is basically a name with a value assigned to it.
55
You can define a variable and assign its value by writing \pythonil{name = value}.
6-
Here, \pythonil{name} is the name of the variable and \pythonil{value} be the value that we want to assign to that name.%
6+
Here, \pythonil{name} is the name of the variable and \pythonil{value} be the value that we want to assign to that name.
7+
If we want to access the value that was stored, we can just use the \pythonil{name} instead.
8+
You can write \pythonil{name} in an arbitrary expression and \python\ then just uses \pythonil{value} instead.
9+
Indeed, you have seen this before, when we used the variables~\pythonilIdx{pi}, \pythonilIdx{e}, \pythonilIdx{inf}, and \pythonilIdx{nan} that we imported from the module~\pythonilIdx{math}.
10+
You can also change which value is stored under a given name by assigning another value to it, e.g., by doing~\pythonil{name = new_value}.
11+
12+
With this, we can now store intermediate results and use them in later computation steps.
13+
This allows us, for the first time, two write programs that perform computations in multiple steps and that consist of multiple lines of code.
14+
\python\ programs are stored as text files with the suffix~\textil{.py}, e.g., \textil{hello.py}.
15+
16+
In the very moment we begin to create such files, two things happen:
17+
First, our code will immediately become much more complex.
18+
Second, our code can be reused, i.e., executed multiple times.
19+
It can be reused by us, now or in ten years, or shared with others.
20+
This changes the quality of programming entirely.
21+
Until now, the \python\ interpreter was basically a fancy calculator.
22+
Now our programs become tools to be used hundereds of times or building blocks, or bricks of giant architectures.
23+
24+
Therefore, from the very start when we actually begin to write program files, it becomes important that we clearly document what we do and why.
25+
For this purpose, we will never just write code -- we will always write comments giving explanations of what our code does.
26+
From the very beginning we must train ourselves to proper discipline.%
27+
%
28+
\bestPractice{comments}{%
29+
Comments help to explain what the code in programs does and are a very important part of the \emph{documentation} of code. %
30+
Comments begin with a \pythonilIdx{\#} character, after which all text is ignored by the \python\ interpreter until the end of the current line. %
31+
Comments can either occupy a complete line or we insert two spaces after the last code character in the line and then start the comment~\cite{PEP8}.%
32+
}%
33+
%
34+
So we now learn two things together:
35+
Using variable assignment and commenting our code.
36+
Because variable assignment is the most fundamental step in writing programs and because programs without comments are \emph{wrong}.
37+
By the way, this is how we do it in this book:
38+
We learn new programming concepts, but try to always intersperse important best practices.%
739
%
840
\hsection{A Simple Example of Variable Assignment and Comments in the Code}%
941
\gitLoadAndExecPython{variables:assignment}{}{variables}{assignment.py}{}%
@@ -96,18 +128,9 @@
96128
\label{fig:variables:assignment}%
97129
\end{figure}%
98130
%
99-
With this, we can now store intermediate results.
100-
This allows us, for the first time, two write programs that perform computations in multiple steps and that consist of multiple lines of code.%
101-
%
102-
\bestPractice{comments}{%
103-
Comments help to explain what the code in programs does and are a very important part of the \emph{documentation} of code. %
104-
Comments begin with a \pythonilIdx{\#} character, after which all text is ignored by the \python\ interpreter until the end of the current line. %
105-
Comments can either occupy a complete line or we insert two spaces after the last code character in the line and then start the comment~\cite{PEP8}.%
106-
}%
107-
%
108-
\cref{lst:variables:assignment} shows the source code of such a commented program.
131+
So let's get to the subject of variable assignments with some examples.
132+
\Cref{lst:variables:assignment} shows the source code of our very first commented program.
109133
This program does not do anything useful, but it illustrates how variables can be used.
110-
111134
It begins by assigning the \pythonilIdx{int} value~\pythonil{1} to a variable named~\pythonil{int_var}.
112135
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}.
113136
But we chose \pythonil{int_var}.

text/main/basics/variables/variables.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
\hsection{Variables}%
22
\label{sec:variables}%
33
%
4-
We did already learn different simple types in \python\ as well as basic expressions, such as mathematical formulas or how to work with strings.
4+
We already learned different simple types in \python\ as well as basic expressions, such as mathematical formulas or how to work with strings.
55
We are still relatively far from writing programs, though.
66
Basically, all we can do is expressions that fit on a single line.
77

0 commit comments

Comments
 (0)