Newer
Older
"source": [
"<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Jupyter-Notebook-Grundlagen\" data-toc-modified-id=\"Jupyter-Notebook-Grundlagen-1\"><span class=\"toc-item-num\">1 </span>Jupyter Notebook Grundlagen</a></span><ul class=\"toc-item\"><li><span><a href=\"#Standardbrowser\" data-toc-modified-id=\"Standardbrowser-1.1\"><span class=\"toc-item-num\">1.1 </span>Standardbrowser</a></span><ul class=\"toc-item\"><li><span><a href=\"#Anleitungen\" data-toc-modified-id=\"Anleitungen-1.1.1\"><span class=\"toc-item-num\">1.1.1 </span>Anleitungen</a></span></li></ul></li><li><span><a href=\"#Shortcuts\" data-toc-modified-id=\"Shortcuts-1.2\"><span class=\"toc-item-num\">1.2 </span>Shortcuts</a></span><ul class=\"toc-item\"><li><span><a href=\"#Beispiel:-Markdown-Zelle-vs.-Code-Zelle\" data-toc-modified-id=\"Beispiel:-Markdown-Zelle-vs.-Code-Zelle-1.2.1\"><span class=\"toc-item-num\">1.2.1 </span>Beispiel: Markdown Zelle vs. Code Zelle</a></span></li></ul></li></ul></li><li><span><a href=\"#Python-Grundlagen\" data-toc-modified-id=\"Python-Grundlagen-2\"><span class=\"toc-item-num\">2 </span>Python Grundlagen</a></span><ul class=\"toc-item\"><li><span><a href=\"#Grundlegende-Datentypen\" data-toc-modified-id=\"Grundlegende-Datentypen-2.1\"><span class=\"toc-item-num\">2.1 </span>Grundlegende Datentypen</a></span></li><li><span><a href=\"#Listen\" data-toc-modified-id=\"Listen-2.2\"><span class=\"toc-item-num\">2.2 </span>Listen</a></span></li><li><span><a href=\"#Mengen\" data-toc-modified-id=\"Mengen-2.3\"><span class=\"toc-item-num\">2.3 </span>Mengen</a></span></li><li><span><a href=\"#Ranges\" data-toc-modified-id=\"Ranges-2.4\"><span class=\"toc-item-num\">2.4 </span>Ranges</a></span></li><li><span><a href=\"#Tupel\" data-toc-modified-id=\"Tupel-2.5\"><span class=\"toc-item-num\">2.5 </span>Tupel</a></span></li><li><span><a href=\"#Dictionaries\" data-toc-modified-id=\"Dictionaries-2.6\"><span class=\"toc-item-num\">2.6 </span>Dictionaries</a></span></li><li><span><a href=\"#If-Anweisungen\" data-toc-modified-id=\"If-Anweisungen-2.7\"><span class=\"toc-item-num\">2.7 </span>If-Anweisungen</a></span></li><li><span><a href=\"#Schleifen\" data-toc-modified-id=\"Schleifen-2.8\"><span class=\"toc-item-num\">2.8 </span>Schleifen</a></span></li><li><span><a href=\"#Funktionen\" data-toc-modified-id=\"Funktionen-2.9\"><span class=\"toc-item-num\">2.9 </span>Funktionen</a></span></li></ul></li><li><span><a href=\"#----------------Praktisch-für-Python,-aber-nicht-für-unseren-Kurs------------------\" data-toc-modified-id=\"----------------Praktisch-für-Python,-aber-nicht-für-unseren-Kurs-------------------3\"><span class=\"toc-item-num\">3 </span><font color=\"red\">--------------- Praktisch für Python, aber nicht für unseren Kurs -----------------</font></a></span><ul class=\"toc-item\"><li><span><a href=\"#Lambda-Funktionen\" data-toc-modified-id=\"Lambda-Funktionen-3.1\"><span class=\"toc-item-num\">3.1 </span>Lambda-Funktionen</a></span></li><li><span><a href=\"#List-Comprehensions\" data-toc-modified-id=\"List-Comprehensions-3.2\"><span class=\"toc-item-num\">3.2 </span>List Comprehensions</a></span></li><li><span><a href=\"#Klassen\" data-toc-modified-id=\"Klassen-3.3\"><span class=\"toc-item-num\">3.3 </span>Klassen</a></span></li></ul></li></ul></div>"
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Falls Sie im Python-Programmierkurs sind und dies ihr erstes geöffnetes Jupyter Notebook ist: Herzlichen Glückwunsch! :)\n",
"Falls Sie im Python-Programmierkurs sind und dies ihr erstes geöffnetes Jupyter Notebook ist, lesen Sie nur bis bis zum Kapitel \"Python.Grundlagen\". Danach wechseln Sie zum Notebook [seminar00.ipynb](seminar00.ipynb).\n",
"- Jupyter ist freie Software – ein browserbasiertes Tool für verschiedene Programmiersprachen (wir benutzen Python).\n",
"- Es stellt Quellcode, Visualisierungen, Text und Erklärungen in einem (Web-)Dokument – dem Jupyter Notebook – dar.\n",
"- Notebooks bestehen aus Blöcken bzw. \"Zellen\".\n",
"- Es gibt zwei Arten von Blöcken: \"Code\" ist für Python-Code, \"Markdown\" ist für Texte, die Sie mit Hilfe der [Markdown-Syntax](https://de.wikipedia.org/wiki/Markdown#Auszeichnungsbeispiele) formatieren können.\n",
"- Blöcke können \"ausgeführt\" werden, dabei wird der Python-Code ausgeführt und Markdown in HTML umgewandelt und angezeigt.\n",
"- Unterhalb eines Blocks mit Quellcode wird der Rückgabewert der letzten Anweisung ausgegeben.\n",
"- Quellcode kann auf mehrere Blöcke aufgeteilt werden (Variablen behalten ihre Gültigkeit/Sichtbarkeit).\n",
"- Sie können auf Text doppelt klicken, um sich den Markdown-Quellcode anzuzeigen. Probieren Sie es mit diesem Text aus.\n",
"- Durch die Tastenkombination \"Strg\" und \"Enter\" oder durch Drücken des \"Run\"-Buttons oben im Menü kommen Sie wieder in den Lesemodus \n",
"- Wenn Sie mal etwas \"kaputtgespielt\" haben, hilft es evtl., im \"Kernel\"-Menü den \"Restart\"-Eintrag auszuwählen.\n",
"\n",
"\n",
"\n",
"## Standardbrowser\n",
"\n",
"Bitte verwenden Sie **nicht** Safari oder Internet Explorer. Auch mit Windows Edge gab es in der Vergangenheit Probleme. Wir empfehlen die Verwendung von Firefox, aber auch Google Chrome lief in der Vergangenheit problemlos. Wenn Sie unsicher sind, was ihr aktueller Standardbrowser ist, folgen Sie einfach der folgenden Anleitung zum Ändern des Standardbrowsers und gehen Sie sicher, dass der richtige Standardbrowser ausgewählt ist.\n",
"\n",
"### Anleitungen\n",
"\n",
"**Änderung des Standardbrowsers in...** \n",
"\n",
"*macOS:* \n",
"1. Öffnen sie die Systemeinstellungen.\n",
"2. Klicken Sie auf „Allgemein“.\n",
"3. Wählen Sie unter „Standard-Webbrowser“ den gewünschten Browser aus.\n",
"\n",
"*Ubuntu Linux:* \n",
"1. Öffnen Sie die System Settings.\n",
"2. Klicken Sie auf „Applications“.\n",
"3. Wählen Sie in der linken Spalte „Default Applications“ aus.\n",
"4. Klicken Sie in der Spalte rechts davon auf „Web Browser“.\n",
"5. Wählen Sie „in the following browser:“ aus.\n",
"6. Wählen Sie den gewünschten Browser aus.\n",
"\n",
"*Windows:* \n",
"1. Öffnen Sie die Systemsteuerung.\n",
"2. Klicken Sie auf „Standardprogramme“.\n",
"3. Klicken Sie auf „Standardprogramme festlegen“.\n",
"4. Klicken Sie in der Liste auf den gewünschten Browser.\n",
"5. Klicken Sie dann auf „Dieses Programm als Standard festlegen“.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Bitte probieren Sie alle unten stehenden Befehle und verstehen Sie, was gemacht wird. Das ist die Basis, die Sie für das Arbeiten mit Jupyter Notebooks brauchen.**\n",
"\n",
"Übersicht: *Menü Help -> Keyboard Shortcuts*\n",
"\n",
"Wichtigste Shortcuts: \n",
"\n",
"- *Enter*: Editiermodus für selektierte Zelle (grün umrandet)\n",
"- *Esc*: Editiermodus verlassen/Kommandomodus (blau umrandet)\n",
"- *Strg + Enter*: Selektierte Zelle ausführen\n",
"- *Shift + Enter*: Selektierte Zelle ausführen und in nächste Zelle springen\n",
"- *Tab*: Autocomplete oder Einrücken\n",
"- *Shift + Tab*: Einrücken rückwärts\n",
"- *B*: Zelle darunter einfügen\n",
"- *A*: Zelle darüber einfügen\n",
"- *DD*: Zelle löschen\n",
"- *M*: Zelltyp Markdown (für formatierte beschreibende Texte, zB diese Zelle)\n",
"- *Y*: Zelltyp Code (Default)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Beispiel: Markdown Zelle vs. Code Zelle"
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dies ist eine **Markdown Zelle**. Hier kann jeglicher Text geschrieben werden. Durch Strg+Enter wird er in den Lesemodus gebracht, durch ein einfach Enter (oder Doppelklick) in den Editiermodus.\n",
"\n",
"Hier werden keine Rechnungen ausgeführt, siehe:\n",
"\n",
"5+3\n",
"\n",
"In diesen Feldern werden Erklärungen, Aufgabenstellungen und schriftliche Antworten von euch stehen."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Das ist eine **Code Zelle**, bei Strg+Enter wird die folgende Rechnung ausgeführt und darunter angezeigt\n",
"# Mit Enter kommt man in den Editiermodus, mit Strg+Enter wird der Code ausgeführt.\n",
"# Der Text kann hier nur stehen, da eine Raute am Anfang der Zeile steht. \n",
"# Dadurch werden diese Zeilen nicht ausgeführt.\n",
"\n",
"5+3\n",
"\n",
"# In diesen Zellen wird der Python Code geschrieben, manchmal müssen Sie ihn schreiben, \n",
"# manchmal steht der Code schon dort und muss von Ihnen nur ausgeführt werden."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
"- Dynamisch typisierte Sprache\n",
"- Statt geschweifter Klammern für Codeblöcke (Funktionen, Schleifen) wird Code mit vier Leerzeichen (pro Hierarchieebene) eingerückt\n",
"- Die meisten Editoren ersetzen Tab automatisch durch vier Leerzeichen\n",
"- Kommentare beginnen mit Raute #"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Grundlegende Datentypen\n",
"Zahlentypen (int/float) sind größtenteils äquivalent zu Java. Strings können mit doppelten oder einfachen Anführungszeichen deklariert werden."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Variablen definieren\n",
"\n",
"a = 2*2.0\n",
"b = 5\n",
"\n",
"# Ausgabe \n",
"\n",
"print(a + b)\n",
"\n",
"# Diese Zelle auswählen und mit `<SHIFT> + <ENTER>` ausführen.\n",
"# Der letzte Rückgabewert der Zelle wird dann unten ausgegeben,\n",
"# hier also der Wert von `a+b`:b\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Die grundlegenden Rechenoperationen __`+`, `-`, `*`, `/`__ und __`**`__ sind ebenfalls in Python verfügbar und verhalten sich, wie man es erwartet:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# äquivalent zu a = a + 1\n",
"a += 1\n",
"\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Potenzieren\n",
"\n",
"2**4"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Division\n",
"\n",
"2/4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Es gibt verschiedene Variablentypen:\n",
" - int (Ganzzahl, z.B. 1, 20, 52432432)\n",
" - float (Gleitkommazahl, z.B. 1.423, 1/3, 0.23487235723)\n",
" - str (Zeichenkette, z.B. \"Hello World\", 'this is a string')\n",
" - bool (Wahrheitswerte, z.B. True, False)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"# Zeichenketten bzw. strings\n",
"\n",
"text1 = 'Hallo '\n",
"text2 = \"Welt\"\n",
"\n",
"print (text1 + text2)\n",
"\n",
"# Andere Datentypen müssen explizit in Strings konvertiert werden,\n",
"# wenn sie an einen String angehängt werden sollen.\n",
"print(text1 + str(1))\n",
"\n",
"# oder man muss sie durch Kommata trennen:\n",
"\n",
"print(text1,1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Zeichenkette zu float \n",
"float('1')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# integer zu float \n",
"\n",
"float(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*None* entspricht *null* in Java."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"myvar = None"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(myvar)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if myvar is None:\n",
" print('x ist nicht definiert')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Listen\n",
"Listen werden mit eckigen Klammern oder list() initialisiert."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"l = [1,2,3,3]\n",
"l.append(4)\n",
"\n",
"print(l)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Vorsicht bei der Kombination von Listen. append hängt die übergebene Variable als einzelnen Listeneintrag an die Liste an."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"l.append([5, 6])\n",
"\n",
"print(l)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zur Kombination von zwei Listen wird der +-Operator verwendet."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"l2 = l + [5,6]\n",
"\n",
"print(l2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mengen\n",
"Mengen können mit geschweiften Klammern oder set() initialisiert werden."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s = {1,2,3,3}\n",
"s2 = set([2,3,4,5])\n",
"\n",
"print(s)\n",
"print(s.intersection(s2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s.add(9)\n",
"s.remove(2)\n",
"s"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Mit list() und set() können die Typen auch ineinander konvertiert werden."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list(s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"set([1,2,3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ranges\n",
"Auflistung von Zahlen, werden z.Bsp. für for-Schleifen mit Index verwendet."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(5):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tupel\n",
"Für sortierete Werte mit fester Komponentenanzahl und fester Bedeutung für jeden Eintrag."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"essen = 'Chili'\n",
"preis = 2.51\n",
"boneintrag = (essen, preis)\n",
"\n",
"print(boneintrag)\n",
"print(boneintrag[0])\n",
"print(boneintrag[1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(essen2, preis2) = boneintrag\n",
"\n",
"print(essen2)\n",
"print(preis2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Klammern können oft weggelassen werden"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"boneintrag = essen, preis\n",
"essen2, preis2 = boneintrag\n",
"\n",
"print(boneintrag)\n",
"print(essen2)\n",
"print(preis2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dictionaries\n",
"Äquivalent zu Maps."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"d = {'Chili': 1.90, 'Penne': 2.50}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"d.keys()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"d.values()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for key, val in d.items():\n",
" print('{}: {}'.format(key, val))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"d['Chili']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"d['Burger'] = 4.90\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"del d['Burger']\n",
"d"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## If-Anweisungen"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if 2>1 or 1>2:\n",
" print ('Bedingung erfüllt.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = 10\n",
"x = 5 if y > 10 else 4\n",
"\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Schleifen"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 5\n",
"while x > 0:\n",
" x -= 1\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for x in ['a', 'b', 'c']:\n",
" print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Funktionen"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def sum_upto(n):\n",
" return n*(n+1)/2\n",
"\n",
"sum_upto(4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zur besseren Übersicht (insbesondere bei vielen Parametern) können diese auch namentlich zugewiesen werden."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sum_upto(n=4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Funktionen können *Default-Parameter* haben, d.h. Werte, die von der Funktion als Standardwert verwendet werden (wenn kein anderer Wert übergeben wird). Default-Parameter müssen am Ende der Funktion stehen und übergeben werden."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def fun_with_default(x=3):\n",
" print('Parameter is {}'.format(x))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fun_with_default()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fun_with_default(x=4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Funktionen können wie Variablen referenziert werden (Pointer auf die Funktion) und zum Beispiel als Parameter übergeben werden."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def calc_and_print(calc_function, n):\n",
" calc_result = calc_function(n)\n",
" print(calc_result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"calc_and_print(sum_upto, 4)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# <font color='red'>--------------- Praktisch für Python, aber nicht für unseren Kurs -----------------</font> \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lambda-Funktionen\n",
"I.d.R. für Inline-Funktionen. Können zum Beispiel an Funktionen als Parameter übergeben oder als Variable deklariert werden. Lambdas enthalten ein einzelnes Statement, dessen Wert automatisch der Rückgabewert ist."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"calc_square = lambda x: x**2\n",
"calc_square(3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"calc_and_print(calc_square, 4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"calc_and_print(lambda x: x**3, 4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## List Comprehensions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = [x**2 for x in range(10)]\n",
"y"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"xy = [(x, x**2) for x in range(10) if x%2 == 0]\n",
"xy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Klassen\n",
"Objekte einer Klasse bekommen bei Methodenaufrufen als ersten Parameter automatisch das *\"self\"*-Objekt übergeben (vgl. *this* in Java). Dieser Parameter muss also immer auch als erster Parameter einer Klassenmethode übergeben werden. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Vehicle():\n",
" # Constructor\n",
" def __init__(self, n_wheels=4, noise='beep'):\n",
" self.n_wheels = n_wheels\n",
" self.noise = noise\n",
" \n",
" def make_noise(self):\n",
" print(self.noise)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"basicVehicle = Vehicle()\n",
"basicVehicle.make_noise()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Properties sind immer von außen sichtbar und lassen sich verändern. Properties, die nicht zur Veränderung gedacht sind, werden nach Konvention durch zwei Unterstriche im Namen gekennzeichnet."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"basicVehicle.n_wheels"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Vererbung ist in Python möglich. In der Regel wird aber Duck-Typing verwendet. \n",
"\n",
"“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bike = Vehicle(n_wheels=2, noise='ring')\n",
"bike.make_noise()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Bike(Vehicle):\n",
" def __init__(self):\n",
" self.n_wheels = 2\n",
" self.noise = 'ring'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Bike().make_noise()"
]
}
],
"metadata": {
"language_info": {
"name": "python",
}
},
"nbformat": 4,
"nbformat_minor": 2
}