Updated notebooks

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
Christopher Arndt 2024-05-08 17:10:40 +02:00
parent e8bb889d9b
commit d38d917e71
17 changed files with 18200 additions and 183 deletions

View File

@ -7531,7 +7531,7 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7543,15 +7543,36 @@ a.anchor-link {
<span class="n">a</span> <span class="o">=</span> <span class="mi">42</span>
<span class="n">b</span> <span class="o">=</span> <span class="mi">23</span>
<span class="n">c</span> <span class="o">=</span> <span class="mh">0x20</span>
<span class="n">d</span> <span class="o">=</span> <span class="mb">0b1110101</span>
<span class="c1">#e = calculate()</span>
<span class="n">f</span> <span class="o">=</span> <span class="mi">1_000_000</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>42
23
65
19
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7566,13 +7587,32 @@ a.anchor-link {
<span class="nb">print</span><span class="p">(</span><span class="n">pi</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">pi</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">pi</span> <span class="o">*</span> <span class="n">e</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">pi</span> <span class="o">*</span> <span class="n">e</span>
<span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>3.14159
&lt;class 'float'&gt;
2.71828
8.539721265199999
&lt;class 'float'&gt;
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7584,8 +7624,9 @@ a.anchor-link {
<span class="n">i</span> <span class="o">=</span> <span class="mi">3</span>
<span class="n">f</span> <span class="o">=</span> <span class="mf">1.5</span>
<span class="n">summe</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">f</span>
<span class="n">summe</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="n">f</span>
<span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">summe</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">summe</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
<span class="n">z</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">n</span> <span class="o">=</span> <span class="mi">6</span>
@ -7595,7 +7636,23 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>3 1.5 4.5
&lt;class 'float'&gt;
&lt;class 'float'&gt;
0.16666666666666666
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7621,7 +7678,27 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>2
999
144
8.0
3.8
1.0
23.799999999999997
0.3333333333333333
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7644,7 +7721,24 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>64
512
512
285.0043245227213
285.0043245227213
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7659,11 +7753,38 @@ a.anchor-link {
<span class="n">rest</span> <span class="o">=</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">3</span>
<span class="nb">print</span><span class="p">(</span><span class="n">rest</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">rest</span><span class="p">))</span>
<span class="n">f</span> <span class="o">=</span> <span class="mf">1.2345</span>
<span class="n">i</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">f</span> <span class="o">%</span> <span class="mi">1</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="o">-</span><span class="mf">2.3456</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="mf">2.3455</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="nb">print</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="mf">2.345</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>1
&lt;class 'int'&gt;
1.2345 1
0.23449999999999993
-2
2.35
2
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-Cell jp-MarkdownCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
@ -7675,7 +7796,7 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7691,7 +7812,7 @@ a.anchor-link {
<span class="nb">print</span><span class="p">(</span><span class="n">s3a</span><span class="p">)</span>
<span class="n">s3b</span> <span class="o">=</span> <span class="s2">"Oder 'andersherum'."</span>
<span class="nb">print</span><span class="p">(</span><span class="n">s3b</span><span class="p">)</span>
<span class="n">s4</span> <span class="o">=</span> <span class="s2">"Alternativ kann man eine Backslash als </span><span class="se">\"</span><span class="s2">Escape</span><span class="se">\"</span><span class="s2">-Zeichen verwenden"</span>
<span class="n">s4</span> <span class="o">=</span> <span class="s2">"Alternativ kann man eine Backslash </span><span class="se">\'</span><span class="s2">als</span><span class="se">\'</span><span class="s2"> </span><span class="se">\"</span><span class="s2">Escape</span><span class="se">\"</span><span class="s2">-Zeichen verwenden"</span>
<span class="nb">print</span><span class="p">(</span><span class="n">s4</span><span class="p">)</span>
<span class="n">s5</span> <span class="o">=</span> <span class="s2">"String-Literale"</span> <span class="s2">", die direkt hintereinander"</span> <span class="s2">", durch Leerzeichen getrennt, stehen"</span> <span class="s2">"werden zusammengefügt."</span>
<span class="nb">print</span><span class="p">(</span><span class="n">s5</span><span class="p">)</span>
@ -7699,12 +7820,45 @@ a.anchor-link {
<span class="s2">"über mehrere Zeilen "</span>
<span class="s2">"hinweg"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">s6</span><span class="p">)</span>
<span class="n">l</span> <span class="o">=</span> <span class="p">[</span>
<span class="s2">"joe"</span><span class="p">,</span>
<span class="s2">"bob"</span><span class="p">,</span>
<span class="s2">"alice"</span><span class="p">,</span>
<span class="s2">"jenny"</span>
<span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>Dies ist ein String!
Man kann einfache oder doppelte Anführungszeichen verwenden.
Innerhalb der Anführungszeichen kann man den jeweils "anderen Typ" verwenden
Oder 'andersherum'.
Alternativ kann man eine Backslash 'als' "Escape"-Zeichen verwenden
String-Literale, die direkt hintereinander, durch Leerzeichen getrennt, stehenwerden zusammengefügt.
Mit Klammerung klappt das auch über mehrere Zeilen hinweg
['joe', 'bob', 'alice', 'jenny']
['joe', 'bob', 'alice', 'jenny']
['joe', 'bob', 'alice', 'jenny']
['joe', 'bob', 'alice', 'jenny']
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7714,23 +7868,46 @@ a.anchor-link {
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="c1"># Längere Strings</span>
<span class="n">long_string</span> <span class="o">=</span> <span class="s2">"""Dies ist ein langer String ("triple quoted string").</span>
<span class="s2">Er kann Zeilenumbrüche enthalten.</span>
<span class="n">long_string</span> <span class="o">=</span> <span class="s1">'''Dies ist ein langer String ("triple quoted string").</span>
<span class="s1">Er kann Zeilenumbrüche enthalten.</span>
<span class="s2">Und Leerzeilen.</span>
<span class="s1">Und Leerzeilen.</span>
<span class="s2">Und auch 'einfache' oder "doppelte" Anführungszeichen.</span>
<span class="s1">Und auch 'einfache' oder "doppelte" Anführungszeichen.</span>
<span class="s2">Er endet mit drei weiteren Anführungszeichen des gleichen Typs.</span>
<span class="s2">"""</span>
<span class="s1">Er endet mit drei weiteren Anführungszeichen des gleichen Typs.</span>
<span class="s1">'''</span>
<span class="nb">print</span><span class="p">(</span><span class="n">long_string</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">long_string</span><span class="p">))</span>
<span class="n">s</span> <span class="o">=</span> <span class="s1">'Dies ist ein langer String ("triple quoted string").</span><span class="se">\n</span><span class="s1">Er kann Zeilenumbrüche enthalten.</span><span class="se">\n\n</span><span class="s1">Und Leerzeilen.</span><span class="se">\n\n</span><span class="s1">Und auch </span><span class="se">\'</span><span class="s1">einfache</span><span class="se">\'</span><span class="s1"> oder "doppelte" Anführungszeichen.</span><span class="se">\n\n</span><span class="s1">Er endet mit drei weiteren Anführungszeichen des gleichen Typs.</span><span class="se">\n</span><span class="s1">'</span>
</pre></div>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>Dies ist ein langer String ("triple quoted string").
Er kann Zeilenumbrüche enthalten.
Und Leerzeilen.
Und auch 'einfache' oder "doppelte" Anführungszeichen.
Er endet mit drei weiteren Anführungszeichen des gleichen Typs.
'Dies ist ein langer String ("triple quoted string").\nEr kann Zeilenumbrüche enthalten.\n\nUnd Leerzeilen.\n\nUnd auch \'einfache\' oder "doppelte" Anführungszeichen.\n\nEr endet mit drei weiteren Anführungszeichen des gleichen Typs.\n'
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7755,7 +7932,28 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>Erste Zeile
Zweite Zeile
Eine Zeile
Noch eine Zeile
Eins Zwei Drei
1 2 3
'Dieser String enthält ein\x00Null-Zeichen'
Hexadezimal-Codes können wir druckbare (e.g. '@') und nicht druckbare Zeichen (e.g. ) verwendet werden.
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7776,7 +7974,21 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>Dies ist der erste String.Und dies der zweite.
Ich habe es dir schon dreimal gesagt! Ich habe es dir schon dreimal gesagt! Ich habe es dir schon dreimal gesagt!
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7794,7 +8006,23 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>SCHREI NICHT SO LAUT!
True
True
True
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7805,12 +8033,68 @@ a.anchor-link {
<div class="highlight hl-ipython3"><pre><span></span><span class="n">hallo</span> <span class="o">=</span> <span class="s2">"Hallo"</span>
<span class="nb">print</span><span class="p">(</span><span class="n">hallo</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">hallo</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">hallo</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">'e'</span> <span class="o">+</span> <span class="n">hallo</span><span class="p">[</span><span class="mi">2</span><span class="p">:])</span>
<span class="n">hello</span> <span class="o">=</span> <span class="n">hallo</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">'e'</span> <span class="o">+</span> <span class="n">hallo</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">hello</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"hello"</span> <span class="o">*</span> <span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>Hallo
a
Hello
hellohellohello
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="c1"># Aber das geht nicht!:</span>
<span class="n">hallo</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'e'</span>
<span class="c1"># Strings sind immutable!</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="application/vnd.jupyter.stderr" tabindex="0">
<pre>
<span class="ansi-red-fg">---------------------------------------------------------------------------</span>
<span class="ansi-red-fg">TypeError</span> Traceback (most recent call last)
<span class="ansi-green-intense-fg ansi-bold">/home/chris/work/python-kurs-softed/notebooks/basictypes.ipynb Cell 16</span> line <span class="ansi-cyan-fg">3
</span><span class="ansi-green-intense-fg ansi-bold"> &lt;a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/basictypes.ipynb#X21sZmlsZQ%3D%3D?line=0'&gt;1&lt;/a&gt;</span> # Aber das geht nicht!:
<span class="ansi-green-fg">----&gt; &lt;a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/basictypes.ipynb#X21sZmlsZQ%3D%3D?line=2'&gt;3&lt;/a&gt;</span> hallo[1] = 'e'
<span class="ansi-green-intense-fg ansi-bold"> &lt;a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/basictypes.ipynb#X21sZmlsZQ%3D%3D?line=4'&gt;5&lt;/a&gt;</span> # Strings sind immutable!
<span class="ansi-red-fg">TypeError</span>: 'str' object does not support item assignment</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
@ -7819,9 +8103,8 @@ a.anchor-link {
<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="c1"># Aber das geht nicht!:</span>
<span class="n">hallo</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'e'</span>
<div class="highlight hl-ipython3"><pre><span></span><span class="n">i</span> <span class="o">=</span> <span class="mf">3.141</span>
<span class="n">i</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">42</span>
</pre></div>
</div>
</div>

View File

@ -16,24 +16,52 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"42\n",
"23\n",
"65\n",
"19\n"
]
}
],
"source": [
"# Ganzzahlen (Integers)\n",
"\n",
"a = 42\n",
"b = 23\n",
"c = 0x20\n",
"d = 0b1110101\n",
"#e = calculate()\n",
"f = 1_000_000\n",
"print(a)\n",
"print(b)\n",
"print(a + b)"
"print(a + b)\n",
"print(a - b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.14159\n",
"<class 'float'>\n",
"2.71828\n",
"8.539721265199999\n",
"<class 'float'>\n"
]
}
],
"source": [
"# Fließkommazahlen (Floats)\n",
"\n",
@ -42,21 +70,35 @@
"print(pi)\n",
"print(type(pi))\n",
"print(e)\n",
"print(pi * e)"
"x = pi * e\n",
"print(x)\n",
"print(type(x))"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 1.5 4.5\n",
"<class 'float'>\n",
"<class 'float'>\n",
"0.16666666666666666\n"
]
}
],
"source": [
"# Implizite Umwandlung von numerischen Typen\n",
"\n",
"i = 3\n",
"f = 1.5\n",
"summe = a + f\n",
"summe = i + f\n",
"print(i, f, summe)\n",
"print(type(summe))\n",
"print(type(e))\n",
"z = 1\n",
"n = 6\n",
@ -65,9 +107,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"999\n",
"144\n",
"8.0\n",
"3.8\n",
"1.0\n",
"23.799999999999997\n",
"0.3333333333333333\n"
]
}
],
"source": [
"# Grundrechenarten\n",
"\n",
@ -86,9 +143,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"64\n",
"512\n",
"512\n",
"285.0043245227213\n",
"285.0043245227213\n"
]
}
],
"source": [
"# X hoch Y\n",
"\n",
@ -104,9 +173,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"<class 'int'>\n",
"1.2345 1\n",
"0.23449999999999993\n",
"-2\n",
"2.35\n",
"2\n"
]
}
],
"source": [
"# Modulo\n",
"\n",
@ -114,7 +197,15 @@
"y = 3\n",
"rest = x % 3\n",
"print(rest)\n",
"print(type(rest))"
"print(type(rest))\n",
"f = 1.2345\n",
"i = int(f)\n",
"print(f, i)\n",
"print(f % 1)\n",
"print(int(-2.3456))\n",
"print(round(2.3455, 2))\n",
"import math\n",
"print(math.floor(2.345))"
]
},
{
@ -126,9 +217,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dies ist ein String!\n",
"Man kann einfache oder doppelte Anführungszeichen verwenden.\n",
"Innerhalb der Anführungszeichen kann man den jeweils \"anderen Typ\" verwenden\n",
"Oder 'andersherum'.\n",
"Alternativ kann man eine Backslash 'als' \"Escape\"-Zeichen verwenden\n",
"String-Literale, die direkt hintereinander, durch Leerzeichen getrennt, stehenwerden zusammengefügt.\n",
"Mit Klammerung klappt das auch über mehrere Zeilen hinweg\n",
"['joe', 'bob', 'alice', 'jenny']\n",
"['joe', 'bob', 'alice', 'jenny']\n",
"['joe', 'bob', 'alice', 'jenny']\n",
"['joe', 'bob', 'alice', 'jenny']\n"
]
}
],
"source": [
"s = \"Dies ist ein String!\"\n",
"print(s)\n",
@ -138,25 +247,36 @@
"print(s3a)\n",
"s3b = \"Oder 'andersherum'.\"\n",
"print(s3b)\n",
"s4 = \"Alternativ kann man eine Backslash als \\\"Escape\\\"-Zeichen verwenden\"\n",
"s4 = \"Alternativ kann man eine Backslash \\'als\\' \\\"Escape\\\"-Zeichen verwenden\"\n",
"print(s4)\n",
"s5 = \"String-Literale\" \", die direkt hintereinander\" \", durch Leerzeichen getrennt, stehen\" \"werden zusammengefügt.\"\n",
"print(s5)\n",
"s6 = (\"Mit Klammerung klappt das auch \"\n",
" \"über mehrere Zeilen \"\n",
" \"hinweg\")\n",
"print(s6)"
"print(s6)\n",
"l = [\n",
" \"joe\",\n",
" \"bob\",\n",
" \"alice\",\n",
" \"jenny\"\n",
"]\n",
"print(l)\n",
"print(l)\n",
"print(l)\n",
"print(l)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# Längere Strings\n",
"\n",
"long_string = \"\"\"Dies ist ein langer String (\"triple quoted string\").\n",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dies ist ein langer String (\"triple quoted string\").\n",
"Er kann Zeilenumbrüche enthalten.\n",
"\n",
"Und Leerzeilen.\n",
@ -164,16 +284,49 @@
"Und auch 'einfache' oder \"doppelte\" Anführungszeichen.\n",
"\n",
"Er endet mit drei weiteren Anführungszeichen des gleichen Typs.\n",
"\"\"\"\n",
"\n",
"'Dies ist ein langer String (\"triple quoted string\").\\nEr kann Zeilenumbrüche enthalten.\\n\\nUnd Leerzeilen.\\n\\nUnd auch \\'einfache\\' oder \"doppelte\" Anführungszeichen.\\n\\nEr endet mit drei weiteren Anführungszeichen des gleichen Typs.\\n'\n"
]
}
],
"source": [
"# Längere Strings\n",
"\n",
"long_string = '''Dies ist ein langer String (\"triple quoted string\").\n",
"Er kann Zeilenumbrüche enthalten.\n",
"\n",
"Und Leerzeilen.\n",
"\n",
"Und auch 'einfache' oder \"doppelte\" Anführungszeichen.\n",
"\n",
"Er endet mit drei weiteren Anführungszeichen des gleichen Typs.\n",
"'''\n",
"print(long_string)\n",
"print(repr(long_string))"
"print(repr(long_string))\n",
"s = 'Dies ist ein langer String (\"triple quoted string\").\\nEr kann Zeilenumbrüche enthalten.\\n\\nUnd Leerzeilen.\\n\\nUnd auch \\'einfache\\' oder \"doppelte\" Anführungszeichen.\\n\\nEr endet mit drei weiteren Anführungszeichen des gleichen Typs.\\n'"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Erste Zeile\n",
"Zweite Zeile\n",
"Eine Zeile\n",
"Noch eine Zeile\n",
"Eins\tZwei\tDrei\n",
"1\t2\t3\n",
"'Dieser String enthält ein\\x00Null-Zeichen'\n",
"Hexadezimal-Codes können wir druckbare (e.g. '@') und nicht druckbare Zeichen (e.g. ) verwendet werden.\n",
"❤\n"
]
}
],
"source": [
"# Escape Sequenzen\n",
"\n",
@ -191,9 +344,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dies ist der erste String.Und dies der zweite.\n",
"Ich habe es dir schon dreimal gesagt! Ich habe es dir schon dreimal gesagt! Ich habe es dir schon dreimal gesagt! \n"
]
}
],
"source": [
"# String-Operationen\n",
"\n",
@ -207,9 +369,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SCHREI NICHT SO LAUT!\n",
"True\n",
"True\n",
"True\n"
]
}
],
"source": [
"klein = \"schrei nicht so laut!\"\n",
"print(klein.upper())\n",
@ -220,14 +393,52 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hallo\n",
"a\n",
"Hello\n",
"hellohellohello\n"
]
}
],
"source": [
"hallo = \"Hallo\"\n",
"print(hallo)\n",
"print(hallo[1])\n",
"print(hallo[0] + 'e' + hallo[2:])"
"hello = hallo[0] + 'e' + hallo[2:]\n",
"print(hello)\n",
"print(\"hello\" * 3)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'str' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/home/chris/work/python-kurs-softed/notebooks/basictypes.ipynb Cell 16\u001b[0m line \u001b[0;36m3\n\u001b[1;32m <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/basictypes.ipynb#X21sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m \u001b[39m# Aber das geht nicht!:\u001b[39;00m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/basictypes.ipynb#X21sZmlsZQ%3D%3D?line=2'>3</a>\u001b[0m hallo[\u001b[39m1\u001b[39;49m] \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39me\u001b[39m\u001b[39m'\u001b[39m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/basictypes.ipynb#X21sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m \u001b[39m# Strings sind immutable!\u001b[39;00m\n",
"\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
]
}
],
"source": [
"# Aber das geht nicht!:\n",
"\n",
"hallo[1] = 'e'\n",
"\n",
"# Strings sind immutable!"
]
},
{
@ -236,9 +447,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Aber das geht nicht!:\n",
"\n",
"hallo[1] = 'e'"
"i = 3.141\n",
"i: int = 42"
]
}
],

View File

@ -9,9 +9,14 @@
a = 42
b = 23
c = 0x20
d = 0b1110101
#e = calculate()
f = 1_000_000
print(a)
print(b)
print(a + b)
print(a - b)
# %%
# Fließkommazahlen (Floats)
@ -21,15 +26,18 @@ e = 2.71828
print(pi)
print(type(pi))
print(e)
print(pi * e)
x = pi * e
print(x)
print(type(x))
# %%
# Implizite Umwandlung von numerischen Typen
i = 3
f = 1.5
summe = a + f
summe = i + f
print(i, f, summe)
print(type(summe))
print(type(e))
z = 1
n = 6
@ -70,6 +78,14 @@ y = 3
rest = x % 3
print(rest)
print(type(rest))
f = 1.2345
i = int(f)
print(f, i)
print(f % 1)
print(int(-2.3456))
print(round(2.3455, 2))
import math
print(math.floor(2.345))
# %% [markdown]
# # Strings
@ -83,7 +99,7 @@ s3a = 'Innerhalb der Anführungszeichen kann man den jeweils "anderen Typ" verwe
print(s3a)
s3b = "Oder 'andersherum'."
print(s3b)
s4 = "Alternativ kann man eine Backslash als \"Escape\"-Zeichen verwenden"
s4 = "Alternativ kann man eine Backslash \'als\' \"Escape\"-Zeichen verwenden"
print(s4)
s5 = "String-Literale" ", die direkt hintereinander" ", durch Leerzeichen getrennt, stehen" "werden zusammengefügt."
print(s5)
@ -91,11 +107,21 @@ s6 = ("Mit Klammerung klappt das auch "
"über mehrere Zeilen "
"hinweg")
print(s6)
l = [
"joe",
"bob",
"alice",
"jenny"
]
print(l)
print(l)
print(l)
print(l)
# %%
# Längere Strings
long_string = """Dies ist ein langer String ("triple quoted string").
long_string = '''Dies ist ein langer String ("triple quoted string").
Er kann Zeilenumbrüche enthalten.
Und Leerzeilen.
@ -103,9 +129,10 @@ Und Leerzeilen.
Und auch 'einfache' oder "doppelte" Anführungszeichen.
Er endet mit drei weiteren Anführungszeichen des gleichen Typs.
"""
'''
print(long_string)
print(repr(long_string))
s = 'Dies ist ein langer String ("triple quoted string").\nEr kann Zeilenumbrüche enthalten.\n\nUnd Leerzeilen.\n\nUnd auch \'einfache\' oder "doppelte" Anführungszeichen.\n\nEr endet mit drei weiteren Anführungszeichen des gleichen Typs.\n'
# %%
# Escape Sequenzen
@ -143,11 +170,19 @@ print("straße".upper() == "STRASSE")
hallo = "Hallo"
print(hallo)
print(hallo[1])
print(hallo[0] + 'e' + hallo[2:])
hello = hallo[0] + 'e' + hallo[2:]
print(hello)
print("hello" * 3)
# %%
# Aber das geht nicht!:
hallo[1] = 'e'
# Strings sind immutable!
# %%
i = 3.141
i: int = 42

View File

@ -7531,7 +7531,7 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7558,7 +7558,24 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>[5, 10, 20, 30, 50]
[0.5, 0.6, 0.8, 1.0, 1.2]
['Joe', 'Alice', 'Bob', 'Charly']
[1, 'Joe', 1.4, 'Alice', 100]
['one'] [] []
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7585,7 +7602,24 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>(5, 10, 20, 30, 50)
(0.5, 0.6, 0.8, 1.0, 1.2)
('Joe', 'Alice', 'Bob', 'Charly')
(1, 'Joe', 1.4, 'Alice', 100)
('one',) ('one',) () ()
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7607,7 +7641,22 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>[1, 2, 3, 4, 5] (1, 2, 3, 4, 5)
[10, 20, 30, 40, 50] (1, 2, 3, 4, 5)
[1, 2, 3, 4, 5]
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7623,6 +7672,20 @@ a.anchor-link {
</div>
</div>
</div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>['h', 'a', 'l', 'l', 'o']
('h', 'a', 'l', 'l', 'o')
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-Cell jp-MarkdownCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
@ -7634,7 +7697,7 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7652,7 +7715,23 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>Frühling
Herbst
Winter
Sommer
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7667,6 +7746,25 @@ a.anchor-link {
</div>
</div>
</div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="application/vnd.jupyter.stderr" tabindex="0">
<pre>
<span class="ansi-red-fg">---------------------------------------------------------------------------</span>
<span class="ansi-red-fg">IndexError</span> Traceback (most recent call last)
<span class="ansi-green-intense-fg ansi-bold">/home/chris/work/python-kurs-softed/notebooks/containertypes.ipynb Cell 9</span> line <span class="ansi-cyan-fg">2
</span><span class="ansi-green-intense-fg ansi-bold"> &lt;a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/containertypes.ipynb#X11sZmlsZQ%3D%3D?line=0'&gt;1&lt;/a&gt;</span> # Ungültige Indizes führen zu einem Fehler (IndexError Exception)
<span class="ansi-green-fg">----&gt; &lt;a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/containertypes.ipynb#X11sZmlsZQ%3D%3D?line=1'&gt;2&lt;/a&gt;</span> print(seasons[4]) # 4 Elemente =&gt; gültige Indizes 0..3
<span class="ansi-red-fg">IndexError</span>: list index out of range</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
@ -7756,6 +7854,7 @@ a.anchor-link {
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">l</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span> <span class="c1"># l = [0, 1, ..., 9]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">4</span><span class="p">])</span> <span class="c1"># Start ist inklusiv, Ende nicht!</span>
</pre></div>
</div>
@ -7806,11 +7905,13 @@ a.anchor-link {
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="c1"># Sind Start und Ende leer, ist das Ergebnis eine Kopie der Liste / des Tuples</span>
<span class="n">l</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">l2</span> <span class="o">=</span> <span class="n">l</span><span class="p">[:]</span>
<span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">999</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
<span class="n">l1</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">l2</span> <span class="o">=</span> <span class="n">l1</span><span class="p">[:]</span>
<span class="n">l3</span> <span class="o">=</span> <span class="n">l1</span>
<span class="n">l1</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">999</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l1</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l3</span><span class="p">)</span>
</pre></div>
</div>
</div>
@ -7893,6 +7994,26 @@ a.anchor-link {
<span class="nb">print</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">t2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">t</span> <span class="ow">is</span> <span class="n">t2</span><span class="p">)</span>
<span class="n">s</span> <span class="o">=</span> <span class="s2">"ein string"</span>
<span class="n">s</span> <span class="o">+=</span> <span class="s2">" und noch mehr"</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
</pre></div>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
<div class="jp-InputArea jp-Cell-inputArea">
<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">l</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="n">sys</span><span class="o">.</span><span class="n">getsizeof</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
</pre></div>
</div>
</div>
@ -7978,6 +8099,7 @@ a.anchor-link {
<span class="n">scores</span><span class="p">[</span><span class="s2">"white team"</span><span class="p">]</span> <span class="o">=</span> <span class="mi">45</span>
<span class="n">scores</span><span class="p">[</span><span class="s2">"rainbow team"</span><span class="p">]</span> <span class="o">=</span> <span class="mi">50</span>
<span class="nb">print</span><span class="p">(</span><span class="n">scores</span><span class="p">)</span>
<span class="k">del</span> <span class="n">scores</span><span class="p">[</span><span class="s2">"blue team"</span><span class="p">]</span>
</pre></div>
</div>
</div>
@ -8047,7 +8169,7 @@ a.anchor-link {
<div class="jp-InputPrompt jp-InputArea-prompt">In [ ]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="cm-editor cm-s-jupyter">
<div class="highlight hl-ipython3"><pre><span></span><span class="c1"># Keys müsen keine Strings, aber immutable sein</span>
<div class="highlight hl-ipython3"><pre><span></span><span class="c1"># Keys müssen keine Strings, aber immutable sein</span>
<span class="c1"># =&gt; Integers, Floats, Tuples sind ok</span>
<span class="n">numbers</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">:</span> <span class="s2">"one"</span><span class="p">,</span> <span class="mi">2</span><span class="p">:</span> <span class="s2">"two"</span><span class="p">,</span> <span class="mi">3</span><span class="p">:</span> <span class="s2">"three"</span><span class="p">}</span>
@ -8063,10 +8185,8 @@ a.anchor-link {
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">6</span><span class="p">):</span>
<span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
<span class="k">if</span> <span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">)</span> <span class="ow">in</span> <span class="n">pixels</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">pixels</span><span class="p">[(</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">)],</span> <span class="n">end</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"."</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span>
<span class="n">value</span> <span class="o">=</span> <span class="n">pixels</span><span class="o">.</span><span class="n">get</span><span class="p">((</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">),</span> <span class="s2">"."</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span>
<span class="nb">print</span><span class="p">()</span>
</pre></div>
</div>

View File

@ -16,9 +16,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5, 10, 20, 30, 50]\n",
"[0.5, 0.6, 0.8, 1.0, 1.2]\n",
"['Joe', 'Alice', 'Bob', 'Charly']\n",
"[1, 'Joe', 1.4, 'Alice', 100]\n",
"['one'] [] []\n"
]
}
],
"source": [
"# Listenliterale\n",
"\n",
@ -38,9 +50,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(5, 10, 20, 30, 50)\n",
"(0.5, 0.6, 0.8, 1.0, 1.2)\n",
"('Joe', 'Alice', 'Bob', 'Charly')\n",
"(1, 'Joe', 1.4, 'Alice', 100)\n",
"('one',) ('one',) () ()\n"
]
}
],
"source": [
"# Tuplelliterale\n",
"values = (5, 10, 20, 30, 50)\n",
@ -60,9 +84,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5] (1, 2, 3, 4, 5)\n",
"[10, 20, 30, 40, 50] (1, 2, 3, 4, 5)\n",
"[1, 2, 3, 4, 5]\n"
]
}
],
"source": [
"# Umwandlung\n",
"\n",
@ -77,9 +111,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['h', 'a', 'l', 'l', 'o']\n",
"('h', 'a', 'l', 'l', 'o')\n"
]
}
],
"source": [
"s = \"hallo\"\n",
"print(list(s))\n",
@ -95,9 +138,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Frühling\n",
"Herbst\n",
"Winter\n",
"Sommer\n"
]
}
],
"source": [
"seasons = [\"Frühling\", \"Sommer\", \"Herbst\", \"Winter\"]\n",
"print(seasons[0]) # Erstes Element hat Index 0!\n",
@ -108,9 +162,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/home/chris/work/python-kurs-softed/notebooks/containertypes.ipynb Cell 9\u001b[0m line \u001b[0;36m2\n\u001b[1;32m <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/containertypes.ipynb#X11sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m \u001b[39m# Ungültige Indizes führen zu einem Fehler (IndexError Exception)\u001b[39;00m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/containertypes.ipynb#X11sZmlsZQ%3D%3D?line=1'>2</a>\u001b[0m \u001b[39mprint\u001b[39m(seasons[\u001b[39m4\u001b[39;49m]) \u001b[39m# 4 Elemente => gültige Indizes 0..3\u001b[39;00m\n",
"\u001b[0;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"# Ungültige Indizes führen zu einem Fehler (IndexError Exception)\n",
"print(seasons[4]) # 4 Elemente => gültige Indizes 0..3"
@ -179,6 +245,7 @@
"outputs": [],
"source": [
"l = list(range(10)) # l = [0, 1, ..., 9]\n",
"print(l)\n",
"print(l[2:4]) # Start ist inklusiv, Ende nicht!"
]
},
@ -214,11 +281,13 @@
"source": [
"# Sind Start und Ende leer, ist das Ergebnis eine Kopie der Liste / des Tuples\n",
"\n",
"l = [1, 2, 3]\n",
"l2 = l[:]\n",
"l[0] = 999\n",
"print(l)\n",
"print(l2)"
"l1 = [1, 2, 3]\n",
"l2 = l1[:]\n",
"l3 = l1\n",
"l1[0] = 999\n",
"print(l1)\n",
"print(l2)\n",
"print(l3)"
]
},
{
@ -280,7 +349,22 @@
"t += (1, 2, 3) # Tuples sind immutable, t ist eine neue Kopie!\n",
"print(t)\n",
"print(t2)\n",
"print(t is t2)\n"
"print(t is t2)\n",
"s = \"ein string\"\n",
"s += \" und noch mehr\"\n",
"i = 0\n",
"i += 1\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"l = list(range(10))\n",
"import sys\n",
"sys.getsizeof(l)"
]
},
{
@ -341,7 +425,8 @@
"\n",
"scores[\"white team\"] = 45\n",
"scores[\"rainbow team\"] = 50\n",
"print(scores)"
"print(scores)\n",
"del scores[\"blue team\"]"
]
},
{
@ -391,7 +476,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Keys müsen keine Strings, aber immutable sein\n",
"# Keys müssen keine Strings, aber immutable sein\n",
"# => Integers, Floats, Tuples sind ok\n",
"\n",
"numbers = {1: \"one\", 2: \"two\", 3: \"three\"}\n",
@ -407,10 +492,8 @@
"\n",
"for row in range(6):\n",
" for col in range(10):\n",
" if (row, col) in pixels:\n",
" print(pixels[(row, col)], end=\"\")\n",
" else:\n",
" print(\".\", end=\"\")\n",
" value = pixels.get((row, col), \".\")\n",
" print(value, end=\"\")\n",
" print()\n"
]
},

View File

@ -97,6 +97,7 @@ t[2] = "drei"
# %%
l = list(range(10)) # l = [0, 1, ..., 9]
print(l)
print(l[2:4]) # Start ist inklusiv, Ende nicht!
# %%
@ -114,11 +115,13 @@ print(l[:3]) # auch hier ist der Ende-Index nicht-inklusiv!
# %%
# Sind Start und Ende leer, ist das Ergebnis eine Kopie der Liste / des Tuples
l = [1, 2, 3]
l2 = l[:]
l[0] = 999
print(l)
l1 = [1, 2, 3]
l2 = l1[:]
l3 = l1
l1[0] = 999
print(l1)
print(l2)
print(l3)
# %%
# Zuweisung an Listenslices ist möglich!
@ -158,8 +161,17 @@ t += (1, 2, 3) # Tuples sind immutable, t ist eine neue Kopie!
print(t)
print(t2)
print(t is t2)
s = "ein string"
s += " und noch mehr"
i = 0
i += 1
# %%
l = list(range(10))
import sys
sys.getsizeof(l)
# %% [markdown]
# ## Dictionaries
@ -192,6 +204,7 @@ print("The red team scored:", scores["red team"])
scores["white team"] = 45
scores["rainbow team"] = 50
print(scores)
del scores["blue team"]
# %%
# Umwandlung
@ -217,7 +230,7 @@ for key, value in scores.items():
print("Das", key, "hat", value, "Punkte.")
# %%
# Keys müsen keine Strings, aber immutable sein
# Keys müssen keine Strings, aber immutable sein
# => Integers, Floats, Tuples sind ok
numbers = {1: "one", 2: "two", 3: "three"}
@ -233,10 +246,8 @@ pixels = {
for row in range(6):
for col in range(10):
if (row, col) in pixels:
print(pixels[(row, col)], end="")
else:
print(".", end="")
value = pixels.get((row, col), ".")
print(value, end="")
print()

View File

@ -7531,7 +7531,7 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7554,7 +7554,20 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>Hmm, ob das so stimmt?
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7577,7 +7590,20 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>Nach Süden, nach Süden!
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7606,6 +7632,19 @@ a.anchor-link {
</div>
</div>
</div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>Auf in den hohen Norden!
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-Cell jp-MarkdownCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
@ -7617,7 +7656,7 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7634,7 +7673,24 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>Schleifendurchlauf Nr. 0
Schleifendurchlauf Nr. 1
Schleifendurchlauf Nr. 2
Schleifendurchlauf Nr. 3
Schleifendurchlauf Nr. 4
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7651,7 +7707,20 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>Summe der Zahlen 1-10: 55
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7667,7 +7736,20 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>9 passt nicht in die Reihe
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7687,7 +7769,20 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>10 ... 9 ... 8 ... 7 ... 6 ... 5 ... 4 ... 3 ... 2 ... 1 ... Lift off!
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7706,7 +7801,28 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>10
11
12
14
15
16
17
18
19
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7733,7 +7849,20 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>Iteration: 142
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7754,7 +7883,20 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>q ist Buchstabe Nr. 17 des dt. Alphabets.
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7773,6 +7915,19 @@ a.anchor-link {
</div>
</div>
</div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>q ist Buchstabe Nr. 17 des dt. Alphabets.
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-Cell jp-MarkdownCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
@ -7784,7 +7939,7 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7803,7 +7958,20 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>256 256 100
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7820,7 +7988,21 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7839,7 +8021,20 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>5
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7856,7 +8051,21 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>15
24
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7871,6 +8080,23 @@ a.anchor-link {
</div>
</div>
</div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="application/vnd.jupyter.stderr" tabindex="0">
<pre>
<span class="ansi-cyan-fg"> Cell </span><span class="ansi-green-fg">In[32], line 2</span>
<span class="ansi-red-fg"> print(add(a=6, 10))</span>
^
<span class="ansi-red-fg">SyntaxError</span><span class="ansi-red-fg">:</span> positional argument follows keyword argument
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-Cell jp-MarkdownCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
@ -7882,7 +8108,7 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7908,7 +8134,27 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>False
True
False
True
True
False
True
True
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7927,7 +8173,23 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>True
True
True
False
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7945,7 +8207,22 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>True
True
True
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7964,7 +8241,23 @@ a.anchor-link {
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs">
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>True
True
True
False
</pre>
</div>
</div>
</div>
</div>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell">
<div class="jp-Cell-inputWrapper" tabindex="0">
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
</div>
@ -7987,6 +8280,28 @@ a.anchor-link {
</div>
</div>
</div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
</div>
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div class="jp-OutputPrompt jp-OutputArea-prompt"></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain" tabindex="0">
<pre>False
True
True
False
True
True
True
True
False
True
</pre>
</div>
</div>
</div>
</div>
</div>
</main>
</body>

View File

@ -16,9 +16,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hmm, ob das so stimmt?\n"
]
}
],
"source": [
"# if / else / elif\n",
"\n",
@ -34,9 +42,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Nach Süden, nach Süden!\n"
]
}
],
"source": [
"richtung = \"s\"\n",
"\n",
@ -52,9 +68,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Auf in den hohen Norden!\n"
]
}
],
"source": [
"# match\n",
"\n",
@ -83,9 +107,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Schleifendurchlauf Nr. 0\n",
"Schleifendurchlauf Nr. 1\n",
"Schleifendurchlauf Nr. 2\n",
"Schleifendurchlauf Nr. 3\n",
"Schleifendurchlauf Nr. 4\n"
]
}
],
"source": [
"# for\n",
"\n",
@ -95,9 +131,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Summe der Zahlen 1-10: 55\n"
]
}
],
"source": [
"result = 0\n",
"for i in range(1, 11):\n",
@ -107,9 +151,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9 passt nicht in die Reihe\n"
]
}
],
"source": [
"for i in (2, 4, 6, 8, 9, 10):\n",
" if i % 2:\n",
@ -118,9 +170,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 ... 9 ... 8 ... 7 ... 6 ... 5 ... 4 ... 3 ... 2 ... 1 ... Lift off!\n"
]
}
],
"source": [
"# while\n",
"\n",
@ -133,9 +193,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"11\n",
"12\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n"
]
}
],
"source": [
"# continue\n",
"\n",
@ -147,9 +223,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 25,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iteration: 142\n"
]
}
],
"source": [
"# break\n",
"\n",
@ -169,9 +253,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"q ist Buchstabe Nr. 17 des dt. Alphabets.\n"
]
}
],
"source": [
"alphabet = \"abcdefghifklmnopqrstuvwxyz\"\n",
"buchstabe = \"q\"\n",
@ -185,9 +277,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"q ist Buchstabe Nr. 17 des dt. Alphabets.\n"
]
}
],
"source": [
"# einfacher:\n",
"\n",
@ -206,9 +306,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 28,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"256 256 100\n"
]
}
],
"source": [
"# Aufruf eingebauter Funktionen\n",
"\n",
@ -220,9 +328,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 29,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n"
]
}
],
"source": [
"# optionale Parameter\n",
"\n",
@ -232,9 +349,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 30,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"# eigene Funktionen\n",
"\n",
@ -246,9 +371,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 31,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n",
"24\n"
]
}
],
"source": [
"# Keyword arguments\n",
"\n",
@ -258,9 +392,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 32,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "SyntaxError",
"evalue": "positional argument follows keyword argument (1525390589.py, line 2)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[32], line 2\u001b[0;36m\u001b[0m\n\u001b[0;31m print(add(a=6, 10))\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n"
]
}
],
"source": [
"# Das geht nicht:\n",
"print(add(a=6, 10))"
@ -275,9 +418,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 35,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"True\n",
"False\n",
"True\n",
"True\n",
"False\n",
"True\n",
"True\n"
]
}
],
"source": [
"a = 10\n",
"b = 50\n",
@ -296,9 +454,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 36,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"True\n",
"False\n"
]
}
],
"source": [
"# Negation\n",
"\n",
@ -310,9 +479,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 37,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"True\n"
]
}
],
"source": [
"# logische Kombination\n",
"\n",
@ -323,23 +502,51 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 38,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"True\n",
"False\n"
]
}
],
"source": [
"# Bool'scher (True/False) Wert von Integers, Floats und Strings \n",
"print(a > c)\n",
"print(bool(a > c)) ## redundant, da Vergleichsausdrücke immer einen bool'schen Wert haben\n",
"# Aber:\n",
"print(bool(a))\n",
"print(not a) # auch ein bool'scher Ausdruck\n"
"print(not a) # auch ein bool'scher Ausdruck"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"True\n",
"True\n",
"False\n",
"True\n",
"True\n",
"True\n",
"True\n",
"False\n",
"True\n"
]
}
],
"source": [
"print(bool(0))\n",
"print(bool(1))\n",
@ -350,7 +557,7 @@
"print(bool(\"True\"))\n",
"print(bool(\"False\")) # !!!\n",
"print(bool(\"\"))\n",
"print(bool(\"\\0\"))\n"
"print(bool(\"\\0\"))"
]
}
],

View File

@ -197,7 +197,6 @@ print(bool(a > c)) ## redundant, da Vergleichsausdrücke immer einen bool'schen
print(bool(a))
print(not a) # auch ein bool'scher Ausdruck
# %%
print(bool(0))
print(bool(1))
@ -211,4 +210,3 @@ print(bool(""))
print(bool("\0"))

1
notebooks/data.json Normal file
View File

@ -0,0 +1 @@
{"foo": 42, "bar": 23, "baz": [1, 2, 3]}

7922
notebooks/dateien.html Normal file

File diff suppressed because one or more lines are too long

313
notebooks/dateien.ipynb Normal file
View File

@ -0,0 +1,313 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dateien"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'str'>\n"
]
}
],
"source": [
"fp = open(\"../uebungen/salaries.txt\", encoding=\"utf-8\")\n",
"content = fp.read()\n",
"print(type(content))\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'bytes'>\n",
"<class 'str'>\n"
]
}
],
"source": [
"fp = open(\"../uebungen/salaries.txt\", \"rb\")\n",
"content = fp.read()\n",
"print(type(content))\n",
"text = content.decode(\"utf-8\")\n",
"print(type(text))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'Anna: 2000\\n'\n"
]
}
],
"source": [
"fp = open(\"../uebungen/salaries.txt\", mode=\"r\")\n",
"line = fp.readline()\n",
"print(repr(line))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Anna: 2000\n",
"Mark: 3000\n",
"Judith: 3500\n",
"Thomas: 2500\n",
"Barbara: 3000\n",
"Elke: 3300\n",
"Michael: 2800\n",
"Johann: 2000\n"
]
}
],
"source": [
"fp = open(\"../uebungen/salaries.txt\", mode=\"r\", encoding=\"utf-8\")\n",
"while True:\n",
" line = fp.readline()\n",
" if not line:\n",
" break\n",
" print(line, end=\"\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'list'>\n",
"['Anna: 2000\\n', 'Mark: 3000\\n', 'Judith: 3500\\n', 'Thomas: 2500\\n', 'Barbara: 3000\\n', 'Elke: 3300\\n', 'Michael: 2800\\n', 'Johann: 2000\\n']\n"
]
}
],
"source": [
"fp = open(\"../uebungen/salaries.txt\", mode=\"r\", encoding=\"utf-8\")\n",
"lines = fp.readlines()\n",
"print(type(lines))\n",
"print(lines)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Anna: 2000\n",
"Mark: 3000\n",
"Judith: 3500\n",
"Thomas: 2500\n",
"Barbara: 3000\n",
"Elke: 3300\n",
"Michael: 2800\n",
"Johann: 2000\n"
]
}
],
"source": [
"fp = open(\"../uebungen/salaries.txt\", mode=\"r\", encoding=\"utf-8\")\n",
"for line in fp:\n",
" print(line, end=\"\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Anna: 2000\n",
"Mark: 3000\n",
"Judith: 3500\n",
"Thomas: 2500\n",
"Barbara: 3000\n",
"Elke: 3300\n",
"Michael: 2800\n",
"Johann: 2000\n"
]
}
],
"source": [
"fp = open(\"../uebungen/salaries.txt\", mode=\"r\", encoding=\"utf-8\")\n",
"for line in fp:\n",
" print(line, end=\"\")\n",
"\n",
"fp.close()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"lines = open(\"../uebungen/salaries.txt\", encoding=\"utf-8\").readlines()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Anna: 2000\n",
"Mark: 3000\n",
"Judith: 3500\n",
"Thomas: 2500\n",
"Barbara: 3000\n",
"Elke: 3300\n",
"Michael: 2800\n",
"Johann: 2000\n"
]
}
],
"source": [
"with open(\"../uebungen/salaries.txt\") as fp:\n",
" for line in fp:\n",
" print(line, end=\"\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Pseudo Code! Nicht ausführbar\n",
"fp = open(\"../uebungen/salaries.txt\")\n",
"ctx = new_context(fp)\n",
"fp.open_context(ctx)\n",
"# code block\n",
"fp.end_context(ctx)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Pseudo Code! Nicht ausführbar\n",
"\n",
"try:\n",
" fp = open(\"datei\")\n",
"except FileNotFoundError:\n",
" # code\n",
"except OSError:\n",
" # code"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3]\n",
"{\"foo\": 42, \"bar\": 23}\n",
"{\"foo\": 42, \"bar\": 23, \"baz\": [1, 2, 3]}\n",
"[{\"foo\": 42, \"bar\": 23}, {\"foo\": 42, \"bar\": 23, \"baz\": [1, 2, 3]}]\n"
]
}
],
"source": [
"import json\n",
"\n",
"data = json.loads(\"[1, 2, 3]\")\n",
"print(data)\n",
"d1 = {\"foo\": 42, \"bar\": 23}\n",
"print(json.dumps(d1))\n",
"d2 = {\"foo\": 42, \"bar\": 23, \"baz\": [1, 2, 3]}\n",
"print(json.dumps(d2))\n",
"l = [d1, d2]\n",
"print(json.dumps(l))\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"data = {\"foo\": 42, \"bar\": 23, \"baz\": [1, 2, 3]}\n",
"with open(\"data.json\", \"w\") as fp:\n",
" json.dump(data, fp)\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"with open(\"data.json\") as fp:\n",
" data2 = json.load(fp)\n",
"\n",
"assert data == data2, \"Not equal\""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "softed",
"language": "python",
"name": "softed"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

99
notebooks/dateien.py Normal file
View File

@ -0,0 +1,99 @@
# %% [markdown]
# # Dateien
# %%
fp = open("../uebungen/salaries.txt", encoding="utf-8")
content = fp.read()
print(type(content))
# %%
fp = open("../uebungen/salaries.txt", "rb")
content = fp.read()
print(type(content))
text = content.decode("utf-8")
print(type(text))
# %%
fp = open("../uebungen/salaries.txt", mode="r")
line = fp.readline()
print(repr(line))
# %%
fp = open("../uebungen/salaries.txt", mode="r", encoding="utf-8")
while True:
line = fp.readline()
if not line:
break
print(line, end="")
# %%
fp = open("../uebungen/salaries.txt", mode="r", encoding="utf-8")
lines = fp.readlines()
print(type(lines))
print(lines)
# %%
fp = open("../uebungen/salaries.txt", mode="r", encoding="utf-8")
for line in fp:
print(line, end="")
# %%
fp = open("../uebungen/salaries.txt", mode="r", encoding="utf-8")
for line in fp:
print(line, end="")
fp.close()
# %%
lines = open("../uebungen/salaries.txt", encoding="utf-8").readlines()
# %%
with open("../uebungen/salaries.txt") as fp:
for line in fp:
print(line, end="")
# %%
# Pseudo Code! Nicht ausführbar
fp = open("../uebungen/salaries.txt")
ctx = new_context(fp)
fp.open_context(ctx)
# code block
fp.end_context(ctx)
# %%
# Pseudo Code! Nicht ausführbar
try:
fp = open("datei")
except FileNotFoundError:
# code
except OSError:
# code
# %%
import json
data = json.loads("[1, 2, 3]")
print(data)
d1 = {"foo": 42, "bar": 23}
print(json.dumps(d1))
d2 = {"foo": 42, "bar": 23, "baz": [1, 2, 3]}
print(json.dumps(d2))
l = [d1, d2]
print(json.dumps(l))
# %%
data = {"foo": 42, "bar": 23, "baz": [1, 2, 3]}
with open("data.json", "w") as fp:
json.dump(data, fp)
# %%
with open("data.json") as fp:
data2 = json.load(fp)
assert data == data2, "Not equal"

7923
notebooks/objects.html Normal file

File diff suppressed because one or more lines are too long

337
notebooks/objects.ipynb Normal file
View File

@ -0,0 +1,337 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Objekte und OO-Programmierung"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```py\n",
"# Ausgangslage\n",
"employees = {\n",
" \"name 1\": {\n",
" \"salary\": ...,\n",
" \"skills\": [...],\n",
" },\n",
" \"name 2\": {\n",
" \"salary\": ...,\n",
" \"skills\": [...],\n",
" },\n",
" ...\n",
"}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Unknown\n",
"0\n",
"[]\n",
"Unknown\n",
"0\n",
"[]\n"
]
}
],
"source": [
"class Employee:\n",
" name = \"Unknown\"\n",
" gehalt = 0\n",
" skills = []\n",
"\n",
"employee1 = Employee()\n",
"print(employee1.name)\n",
"print(employee1.gehalt)\n",
"print(employee1.skills)\n",
"\n",
"employee2 = Employee()\n",
"print(employee2.name)\n",
"print(employee2.gehalt)\n",
"print(employee2.skills)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Joe Doe\n",
"Unknown\n"
]
}
],
"source": [
"employee1.name = \"Joe Doe\"\n",
"print(employee1.name)\n",
"print(employee2.name)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Init!\n"
]
}
],
"source": [
"class Employee:\n",
" def __init__(self):\n",
" print(\"Init!\")\n",
" \n",
"employee3 = Employee()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class '__main__.Employee'>\n",
"Joe Doe\n",
"2000\n",
"[]\n",
"Jane Oh\n",
"3000\n",
"['marketing']\n"
]
}
],
"source": [
"class Employee:\n",
" def __init__(self, name=\"\", gehalt=0, skills=[]):\n",
" self.name = name\n",
" self.gehalt = gehalt\n",
"\n",
" if skills is None:\n",
" self.skills = []\n",
" else:\n",
" self.skills = skills\n",
"\n",
"employee4 = Employee(\"Joe Doe\", 2000, [])\n",
"print(type(employee4))\n",
"print(employee4.name)\n",
"print(employee4.gehalt)\n",
"print(employee4.skills)#\n",
"\n",
"employee5 = Employee(\"Jane Oh\", 3000, [\"marketing\"])\n",
"print(employee5.name)\n",
"print(employee5.gehalt)\n",
"print(employee5.skills)\n",
"\n",
"# Geht nicht, weil die Klasse kein Attribut \"name\" definiert\n",
"#print(Employee.name)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"0\n",
"[]\n"
]
}
],
"source": [
"employee6 = Employee()\n",
"print(employee6.name)\n",
"print(employee6.gehalt)\n",
"print(employee6.skills)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Employee 'Bob Smith'\n",
" gehalt: 0\n",
" skills:\n",
" - software development\n",
" - it support\n"
]
}
],
"source": [
"class Employee:\n",
" def __init__(self, name=\"\", gehalt=0, skills=[]):\n",
" self.name = name\n",
" self.gehalt = gehalt\n",
"\n",
" if skills is None:\n",
" self.skills = []\n",
" else:\n",
" self.skills = skills\n",
"\n",
" def __str__(self):\n",
" s = [f\"Employee '{self.name}'\"]\n",
" s.append(f\" gehalt: {self.gehalt}\")\n",
" s.append(f\" skills:\")\n",
" for skill in self.skills:\n",
" s.append(f\" - {skill}\")\n",
" return \"\\n\".join(s)\n",
"\n",
"\"\"\"\n",
"Employee 'name':\n",
" gehalt: XXX\n",
" skills:\n",
" - skill 1\n",
" - skill 2\n",
"\"\"\"\n",
"employee7 = Employee(name=\"Bob Smith\", skills=[\"software development\", \"it support\"])\n",
"print(employee7)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hallo, Welt\n",
"Guten Tag, Welt\n",
"Welt, Guten Tag\n",
"Welt, Guten Tag\n",
"Guten Tag, Welt\n"
]
}
],
"source": [
"# Exkurs: Strings und `format()`\n",
"\n",
"s = \"Hallo, {}\"\n",
"print(s.format(\"Welt\"))\n",
"\n",
"s = \"{}, {}\"\n",
"print(s.format(\"Guten Tag\", \"Welt\"))\n",
"\n",
"s = \"{name}, {grussformel}\"\n",
"print(s.format(grussformel=\"Guten Tag\", name=\"Welt\"))\n",
"\n",
"grussformel=\"Guten Tag\"\n",
"name=\"Welt\"\n",
"print(f\"{name}, {grussformel}\")\n",
"\n",
"\n",
"def sagehallo(name):\n",
" # 'grussformel' ist eine globale variable!\n",
" print(f\"{grussformel}, {name}\")\n",
"\n",
"sagehallo(\"Welt\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5000\n"
]
},
{
"ename": "ValueError",
"evalue": "Zu hohes gehalt!",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/home/chris/work/python-kurs-softed/notebooks/objects.ipynb Cell 10\u001b[0m line \u001b[0;36m2\n\u001b[1;32m <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/objects.ipynb#X12sZmlsZQ%3D%3D?line=21'>22</a>\u001b[0m \u001b[39mprint\u001b[39m(employee8\u001b[39m.\u001b[39mgehalt)\n\u001b[1;32m <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/objects.ipynb#X12sZmlsZQ%3D%3D?line=22'>23</a>\u001b[0m \u001b[39m# ValueError!\u001b[39;00m\n\u001b[0;32m---> <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/objects.ipynb#X12sZmlsZQ%3D%3D?line=23'>24</a>\u001b[0m employee8\u001b[39m.\u001b[39;49mset_gehalt(\u001b[39m4000\u001b[39;49m)\n\u001b[1;32m <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/objects.ipynb#X12sZmlsZQ%3D%3D?line=24'>25</a>\u001b[0m \u001b[39m# TypeError!\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/objects.ipynb#X12sZmlsZQ%3D%3D?line=25'>26</a>\u001b[0m employee8\u001b[39m.\u001b[39mset_gehalt(\u001b[39m\"\u001b[39m\u001b[39mein sack mehl\u001b[39m\u001b[39m\"\u001b[39m)\n",
"\u001b[1;32m/home/chris/work/python-kurs-softed/notebooks/objects.ipynb Cell 10\u001b[0m line \u001b[0;36m1\n\u001b[1;32m <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/objects.ipynb#X12sZmlsZQ%3D%3D?line=14'>15</a>\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mgehalt \u001b[39m=\u001b[39m neues_gehalt\n\u001b[1;32m <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/objects.ipynb#X12sZmlsZQ%3D%3D?line=15'>16</a>\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m---> <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/objects.ipynb#X12sZmlsZQ%3D%3D?line=16'>17</a>\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mZu hohes gehalt!\u001b[39m\u001b[39m\"\u001b[39m)\n",
"\u001b[0;31mValueError\u001b[0m: Zu hohes gehalt!"
]
}
],
"source": [
"class Employee:\n",
" def __init__(self, name=\"\", gehalt=0, skills=[]):\n",
" self.name = name\n",
" self.gehalt = gehalt\n",
"\n",
" if skills is None:\n",
" self.skills = []\n",
" else:\n",
" self.skills = skills\n",
"\n",
" def set_gehalt(self, neues_gehalt):\n",
" if not isinstance(neues_gehalt, int):\n",
" raise TypeError(\"gehalt muss ein Integer sein\")\n",
" if neues_gehalt < 4000:\n",
" self.gehalt = neues_gehalt\n",
" else:\n",
" raise ValueError(\"Zu hohes gehalt!\")\n",
"\n",
"employee8 = Employee()\n",
"# Ok\n",
"employee8.gehalt = 5000\n",
"print(employee8.gehalt)\n",
"# ValueError!\n",
"employee8.set_gehalt(4000)\n",
"# TypeError!\n",
"employee8.set_gehalt(\"ein sack mehl\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "softed",
"language": "python",
"name": "softed"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

160
notebooks/objects.py Normal file
View File

@ -0,0 +1,160 @@
# %% [markdown]
# # Objekte und OO-Programmierung
# %% [markdown]
# ```py
# # Ausgangslage
# employees = {
# "name 1": {
# "salary": ...,
# "skills": [...],
# },
# "name 2": {
# "salary": ...,
# "skills": [...],
# },
# ...
# }
# ```
# %%
class Employee:
name = "Unknown"
gehalt = 0
skills = []
employee1 = Employee()
print(employee1.name)
print(employee1.gehalt)
print(employee1.skills)
employee2 = Employee()
print(employee2.name)
print(employee2.gehalt)
print(employee2.skills)
# %%
employee1.name = "Joe Doe"
print(employee1.name)
print(employee2.name)
# %%
class Employee:
def __init__(self):
print("Init!")
employee3 = Employee()
# %%
class Employee:
def __init__(self, name="", gehalt=0, skills=[]):
self.name = name
self.gehalt = gehalt
if skills is None:
self.skills = []
else:
self.skills = skills
employee4 = Employee("Joe Doe", 2000, [])
print(type(employee4))
print(employee4.name)
print(employee4.gehalt)
print(employee4.skills)#
employee5 = Employee("Jane Oh", 3000, ["marketing"])
print(employee5.name)
print(employee5.gehalt)
print(employee5.skills)
# Geht nicht, weil die Klasse kein Attribut "name" definiert
#print(Employee.name)
# %%
employee6 = Employee()
print(employee6.name)
print(employee6.gehalt)
print(employee6.skills)
# %%
class Employee:
def __init__(self, name="", gehalt=0, skills=[]):
self.name = name
self.gehalt = gehalt
if skills is None:
self.skills = []
else:
self.skills = skills
def __str__(self):
s = [f"Employee '{self.name}'"]
s.append(f" gehalt: {self.gehalt}")
s.append(f" skills:")
for skill in self.skills:
s.append(f" - {skill}")
return "\n".join(s)
"""
Employee 'name':
gehalt: XXX
skills:
- skill 1
- skill 2
"""
employee7 = Employee(name="Bob Smith", skills=["software development", "it support"])
print(employee7)
# %%
# Exkurs: Strings und `format()`
s = "Hallo, {}"
print(s.format("Welt"))
s = "{}, {}"
print(s.format("Guten Tag", "Welt"))
s = "{name}, {grussformel}"
print(s.format(grussformel="Guten Tag", name="Welt"))
grussformel="Guten Tag"
name="Welt"
print(f"{name}, {grussformel}")
def sagehallo(name):
# 'grussformel' ist eine globale variable!
print(f"{grussformel}, {name}")
sagehallo("Welt")
# %%
class Employee:
def __init__(self, name="", gehalt=0, skills=[]):
self.name = name
self.gehalt = gehalt
if skills is None:
self.skills = []
else:
self.skills = skills
def set_gehalt(self, neues_gehalt):
if not isinstance(neues_gehalt, int):
raise TypeError("gehalt muss ein Integer sein")
if neues_gehalt < 4000:
self.gehalt = neues_gehalt
else:
raise ValueError("Zu hohes gehalt!")
employee8 = Employee()
# Ok
employee8.gehalt = 5000
print(employee8.gehalt)
# ValueError!
employee8.set_gehalt(4000)
# TypeError!
employee8.set_gehalt("ein sack mehl")