lehrkraefte:blc:informatik:glf4-20:simulation:python-mini-cheat-sheet

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
lehrkraefte:blc:informatik:glf4-20:simulation:python-mini-cheat-sheet [2021/04/06 20:10]
Ivo Blöchliger created
lehrkraefte:blc:informatik:glf4-20:simulation:python-mini-cheat-sheet [2021/04/07 08:40] (current)
Ivo Blöchliger [Listen]
Line 1: Line 1:
 +====== Python Micro-Cheat-Sheet ======
 +Ein ausführlicheres Cheat-Sheet gibt es [[lehrkraefte:blc:informatik:glf4-20:simulation:python-repe|hier]].
 ==== Wiederholung ==== ==== Wiederholung ====
 <code python> <code python>
Line 10: Line 12:
 liste=[]   # Leere Liste liste=[]   # Leere Liste
 for i in range(6):     # i von 0 bis und mit 5 for i in range(6):     # i von 0 bis und mit 5
-    liste.append(i*2)  # Das doppelte von i der Liste hinten hinzufügen+    liste.append(i*2)  # Das doppelte von i der Liste hinten anfügen
 print(liste)           # Ergibt [0,2,4,6,8,10] print(liste)           # Ergibt [0,2,4,6,8,10]
 </code> </code>
Line 16: Line 18:
 <code python> <code python>
 liste = [23,42,123,1234] liste = [23,42,123,1234]
-csv = "Index;Wert\n"+csv = "Index;Wert\n"                        # Erste Zeile, Spalten durch Strichpunkt getrennt, Zeilenumbruch am Schluss
 for index in range(len(liste)):             # len(liste) ist die Anzahl Elemente in der Liste for index in range(len(liste)):             # len(liste) ist die Anzahl Elemente in der Liste
    csv += "%d;%d\n" % (index, liste[index]) # eckige Klammern: Zugriff auf ein bestimmtes Element, bestimmt durch den Index    csv += "%d;%d\n" % (index, liste[index]) # eckige Klammern: Zugriff auf ein bestimmtes Element, bestimmt durch den Index
Line 29: Line 31:
 </code> </code>
  
 +==== Funktionen ====
 +<code python>
 +# Definition der Funktion, wird nicht direkt ausgeführt.
 +def quadrat(x):  # x bekommt den Wert, mit dem man die Funktion aufruft. Diese Variable x lebt nur in dieser Funktion und hat nichts mit Variablen gleichen Namens anderswo im Programm zu tun.
 +    return x*x   # Quadrat von x berechnen und als Resultat zurückgeben.
  
 +x = 42
 +print("Das Quadrat von 12 ist %d" % quadrat(12) )
 +print("x=%d ist unverändert!" % x)
 +</code>
  • lehrkraefte/blc/informatik/glf4-20/simulation/python-mini-cheat-sheet.1617732605.txt.gz
  • Last modified: 2021/04/06 20:10
  • by Ivo Blöchliger