{"id":5526,"date":"2012-09-12T20:35:41","date_gmt":"2012-09-13T00:35:41","guid":{"rendered":"http:\/\/cscircles.cemc.uwaterloo.ca\/?page_id=5526"},"modified":"2024-07-11T05:46:41","modified_gmt":"2024-07-11T09:46:41","slug":"13-de","status":"publish","type":"page","link":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/13-de\/","title":{"rendered":"13: Listen (Arrays)"},"content":{"rendered":"<p>Eine <strong>Liste<\/strong> ist eine Reihe von mehreren Variablen, die unter einem einzelnen Namen zusammengefasst werden. Anstatt ein Programm mit vielen Variablen <code>x0<\/code>, <code>x1<\/code>, <code>x2<\/code> zu schreiben, kannst du eine einzelne Variable x definieren und ihre einzelnen Mitglieder <code>x[0]<\/code>, <code>x[1]<\/code>, <code>x[2]<\/code>, etc aufrufen. Was noch wichtiger ist: du kannst andere Ausdr\u00fccke und Variablen in die eckigen Klammern setzen, wie <code>x[i]<\/code> und <code>x[i+1]<\/code>. Dies erlaubt es uns, mit beliebig gro\u00dfen Datens\u00e4tzen umzugehen, indem wir nur ein einziges kleines St\u00fcck Code benutzen.<\/p>\n<p>Ein Weg, eine Liste zu erzeugen ist es, mehrere Variablen beizuf\u00fcgen, die durch Kommas in eckigen Klammern getrennt werden:<\/p>\n<pre>myList = [\"the first value in the list\", 1999, 4.5]<\/pre>Dies erzeugt eine Liste, die den Namen <code>myList<\/code> und eine L\u00e4nge von 3 hat. Jedes Element der Liste bekommt eine Nummer zugeordnet, die man seinen Index nennt: das erste Element hat den Index 0, das n\u00e4chste hat den Index 1 und so weiter. Die individuellen Variablen, aus denen die Liste besteht, haben die Namen<\/p>\n<p style=\"text-align: center;\"><code>\u00ablistName\u00bb[\u00abindexNumber\u00bb]<\/code><\/p>\n<p>Also ist in diesem Beispiel <code>myList[0]<\/code> eine Variable, deren Wert folgende Zeichenkette ist: <code>\"the first value in the list\"<\/code> und <code>print(myList[2])<\/code> <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/console\/?consolecode=myList%20%3D%20%5B%22the%20first%20value%20in%20the%20list%22%2C%201999%2C%204.5%5D%0Aprint%28myList%5B2%5D%29\" target=\"_blank\">ergibt<\/a> <code>4.5<\/code>. Du kannst au\u00dferdem die Werte der einzelnen Elemente der Liste \u00e4ndern und komplette Listen ausgeben:<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform0\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox0'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Eine Liste \u00e4ndern und ausgeben<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode0' id='usercode0'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\nnumbers = ['zero', 'one', 'two']\nnumbers[0] = 'zilch'\nprint(numbers)<\/textarea><\/div>\n<div id='pbhistory0' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit0' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(0)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(0,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse0\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"0\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"d82a0deb4b4717421add40670f2e07b9\"\/>\n<div id='pbresults0' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit0\").value = \"Run program\";document.getElementById(\"inputInUse0\").value = \"N\";<\/script>\n<\/p>\n<p>Wie du sehen kannst, verh\u00e4lt sich <code>numbers[0],<\/code> als ob es selbst eine Variable sei und ver\u00e4ndert werden k\u00f6nnte.<\/p>\n<p>Versuche als n\u00e4chstes den finalen Zustand des folgenden visualisierten Beispiels vorauszusagen,<br \/>\nvergleiche ihn dann, indem du den Code laufen l\u00e4sst.<\/p>\n<p><iframe width='100%' height='480' frameborder='0' scrolling='no' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/OnlinePythonTutor3-cemc\/iframe-embed.html#code=myList+%3D+%5B1%2C+10%2C+100%2C+1000%5D%0Afor+i+in+range%280%2C+2%29%3A%0A+myList%5Bi%5D+%3D+2%2AmyList%5Bi%5D%2Bi&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=350&rightStdout=1'><\/iframe><\/p>\n<div class='pybox modeNeutral' id='pybox1'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Multiple Choice Exercise: <\/span><span class='title'>Meta-Kram<\/span><\/div><div>Was ist die Ausgabe des folgenden Codefragments?<\/p>\n<pre>stuff = [2, 25, 80, 12]\nstuff[stuff[0]] = stuff[3]\nprint(stuff)<\/pre><\/div><label>Your choice: <\/label><select id=\"pyselect1\"><option value=\"d\" selected>Select one<\/option><option value=\"w\">[2, 25, 80, 12]<\/option><option value=\"r\">[2, 25, 12, 12]<\/option><option value=\"w\">[2, 25, 80, 80]<\/option><option value=\"w\">[12, 25, 80, 12]<\/option><option value=\"w\">Dieses Code-Fragment erzeugt einen Fehler.<\/option><\/select><div class='pyboxbuttons'><input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" name=\"slug\" value=\"13.indexindex\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick='pbMultiCheck(1)'\/><\/div><div class=\"pbresults\" id=\"pyMultiResults1\"><\/div><div class=\"epilogue\">Korrekt! Sieh dir das Zuordnungsstatement an (2te Zeile). Der Wert von <code>stuff[3]<\/code> auf der rechten Seite ist <code>12<\/code>. Auf der linken Seite ist <code>stuff[0]<\/code> gleich <code>2<\/code>, also verweist <code>stuff[stuff[0]]<\/code> auf die Variable <code>stuff[2]<\/code>. Der Wert dieser Variable wird aufgewertet (von <code>80<\/code>) auf <code>12<\/code>.<\/div><\/div>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> Was Python eine <strong>Liste<\/strong> nennt, w\u00fcrde in den meisten anderen Programmiersprachen als <strong>Array<\/strong> bezeichnet werden. Python hat au\u00dferdem etwas <a href=\"http:\/\/docs.python.org\/py3k\/library\/array.html\">anderes<\/a> und wesentlich fortgeschritteneres, das es als Array bezeichnet. <\/span><\/td><\/table><\/p>\n<h3>Ein \u00fcblicher Fehler<\/h3>\n<p>Wenn du versuchst, Python nach einem Index zu fragen, der nicht existiert, bekommst du eine Fehlermeldung:<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform2\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox2'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Au\u00dferhalb-der-Reichweite-Fehler.<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode2' id='usercode2'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nmyList=[1, 2, 4, 8]\nprint(myList[4])<\/textarea><\/div>\n<div id='pbhistory2' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit2' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(2)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(2,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse2\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"2\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"d236bc08f9f6bf1c1ceac57fa1675129\"\/>\n<div id='pbresults2' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit2\").value = \"Run program\";document.getElementById(\"inputInUse2\").value = \"N\";<\/script>\n<\/p>\n<p>Im obigen Beispiel, weil <code>myList<\/code> die L\u00e4nge 4 hat und der erste Index 0 ist, ist der maximal zul\u00e4ssige Index 3. Wenn du nun nach einem Index von 4, 5 oder h\u00f6her fragst, bekommst du diese Art von Fehler.<\/p>\n<p><span style=\"color: #000000; font-size: 23px; line-height: 35px;\">\u00dcbliche hilfreiche Operationen<\/span><\/p>\n<h3>Die L\u00e4nge einer Liste: <code>len(\u00ablist\u00bb)<\/code><\/h3>\n<p>Um die Anzahl von Elementen in einer Liste zu bestimmen, rufe die Funktion <code>len()<\/code> auf dieser Liste auf. Schau dir an, wie <code>range<\/code> im folgenden Beispiel verwendet wird.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform3\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox3'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Die L\u00e4nge einer Liste bekommen und sie verwenden, um durch die Liste zu schleifen.<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 110px;'><textarea wrap='off' name='usercode3' id='usercode3'  cols=10 rows=4 readonly='readonly'  style = 'height : 110px;'  class='pyboxCode RO'>\nmyList = [3, 12, \"pizza\", 3, 4]\nprint(\"myList has length\", len(myList))\nfor i in range(0, len(myList)):\n print(\"item at index\", i, \":\", myList[i])<\/textarea><\/div>\n<div id='pbhistory3' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit3' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(3)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(3,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse3\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"3\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"0931b8acbf88dcb03b9532487b8af890\"\/>\n<div id='pbresults3' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit3\").value = \"Run program\";document.getElementById(\"inputInUse3\").value = \"N\";<\/script>\n<\/p>\n<p>Es ist \u00fcblich, <code>len<\/code> zu benutzen, um Code zu schreiben, der mit Listen jeglicher L\u00e4nge funktioniert, wie im Beispiel oben und der n\u00e4chsten \u00dcbung.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform4\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox4'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Monkey in the Middle<\/span><\/div>Schreibe eine Funktion <code>middle(L)<\/code>, die eine Liste <code>L<\/code> als ihr Argument nimmt und das Element in der <em>mittleren<\/em> Position von <code>L<\/code> zur\u00fcckgibt. (Damit die Mitte gut definiert ist, solltest du annehmen, dass die L\u00e4nge von <code>L<\/code> ungerade ist.) Zum Beispiel sollte, wenn du <code>middle([8, 0, 100, 12, 1])<\/code> aufrufst, <code>100<\/code> zur\u00fcckgegeben werden.<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode4' id='usercode4'  cols=10 rows=20   class='pyboxCode RW'>\ndef...\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory4' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput4\">Enter testing statements like <code>print(myfunction(\"test argument\"))<\/code> below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<input type='hidden' id='defaultCode4' value='def...\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit4' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch4\" value=\"Input Switch\" onclick=\"pbInputSwitch(4,'Y')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(4)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(4,'Y')\" ><\/td>\n<\/tr><\/table><select id='pbSelect4' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(4,'13.middle')\" >History<\/option>\n<option name='default' data-pbonclick=\"pbSetText(4,descape($('#defaultCode4').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(4);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse4\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"4\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"378144365f3118ad432dc4c1eb3ef2ab\"\/>\n<div id='pbresults4' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(4);});pbInputSwitch(4,\"Y\");<\/script>\n<\/p>\n<h3>Sind Listen wie Zeichenketten?<\/h3>\n<p>Mittlerweile solltest du gemerkt haben, dass die Operationen, die auf Listen ausgef\u00fchrt werden, ziemlich \u00e4hnlich sind, wie Zeichenketten bzw. Strings: bei beiden ist die L\u00e4nge anhand der <code>len()<\/code> Funktion ersichtlich und beide verwenden <code>X[\u00abindex\u00bb]<\/code>, um einzelne Elemente auszuw\u00e4hlen. Listen und Strings sind tats\u00e4chlich verwandt: sie werden von Python beide als \"<a href=\"http:\/\/docs.python.org\/py3k\/library\/stdtypes.html?highlight=sequence#sequence-types-str-bytes-bytearray-list-tuple-range\">sequence types<\/a>\" bezeichnet. Der eine gro\u00dfe Unterschied ist, dass einzelne Zeichen in Zeichenketten nicht ver\u00e4ndert werden k\u00f6nnen.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform5\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox5'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Fehler, hervorgerufen durch den Versuch einem Zeichen einen Wert in einem String zuzuordnen.<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode5' id='usercode5'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\nword='slack'\nprint(word, word[1])\nword[1]='n'<\/textarea><\/div>\n<div id='pbhistory5' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit5' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(5)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(5,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse5\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"5\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"071e91b26fabcbb7947f2acafc8ec3ec\"\/>\n<div id='pbresults5' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit5\").value = \"Run program\";document.getElementById(\"inputInUse5\").value = \"N\";<\/script>\n<\/p>\n<p>Aus diesem Grund nennt man Listen <em>ver\u00e4nderlich<\/em> und Zeichenketten <em>unver\u00e4nderlich<\/em>; du wirst ein paar weitere Informationen dar\u00fcber sehen in Lektion 17.<\/p>\n<h3>Verkettung und Erzeugung<\/h3>\n<p>Aus der <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/7a-de\/\">Lektion<\/a> \u00fcber den Typ <code>str<\/code> kannst du dich vielleicht noch daran erinnern, dass es m\u00f6glich ist, <code>+<\/code> zu verwenden, um zwei Strings miteinander zu verschmelzen (verketten). Das Gleiche kannst du mit Listen machen:<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform6\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox6'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Listen kombinieren mit <code>+<\/code>.<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 136px;'><textarea wrap='off' name='usercode6' id='usercode6'  cols=10 rows=5 readonly='readonly'  style = 'height : 136px;'  class='pyboxCode RO'>\nlistA = [3, 'go', 7.1]\nlistB = [4, 'hi', 1]\nlistC = listA + listB\nprint(len(listC))\nprint(listC)<\/textarea><\/div>\n<div id='pbhistory6' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit6' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(6)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(6,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse6\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"6\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"9de0c01c5030f213b37d8ba3491ec8f8\"\/>\n<div id='pbresults6' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit6\").value = \"Run program\";document.getElementById(\"inputInUse6\").value = \"N\";<\/script>\n<\/p>\n<p>Ganz \u00e4hnlich kannst du das Multiplikationszeichen * benutzen, um eine Liste durch Wiederholungen zu verl\u00e4ngern. Dies ist hilfreich, um eine neue Liste zu erzeugen, die so lang ist, wie du sie haben m\u00f6chtest.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform7\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox7'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div> <code><code>*<\/code><\/code> verwenden.<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 110px;'><textarea wrap='off' name='usercode7' id='usercode7'  cols=10 rows=4 readonly='readonly'  style = 'height : 110px;'  class='pyboxCode RO'>\nmyPhoneNumber = [9, 6, 7] + [1]*4\nprint(myPhoneNumber)\nnewEmptyList = [0]*20\nprint(newEmptyList)<\/textarea><\/div>\n<div id='pbhistory7' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit7' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(7)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(7,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse7\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"7\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"b3108d3ef8326dc1609d500870b8d584\"\/>\n<div id='pbresults7' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit7\").value = \"Run program\";document.getElementById(\"inputInUse7\").value = \"N\";<\/script>\n<\/p>\n<p>Um die n\u00e4chste \u00dcbung zu l\u00f6sen, verwende einen der Operatoren, die wir gerade eingef\u00fchrt haben, und eine <code>for<\/code> Schleife.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform8\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox8'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>It's Natural<\/span><\/div>Schreibe eine Funktion <code>naturalNumbers<\/code>, die eine positive Ganzzahl <code>n<\/code> als Eingabe nimmt, und eine Liste <code>[1, 2, ...]<\/code> zur\u00fcckgibt, die aus den ersten <code>n<\/code> nat\u00fcrlichen Zahlen besteht.<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode8' id='usercode8'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory8' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput8\">Enter testing statements like <code>print(myfunction(\"test argument\"))<\/code> below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit8' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch8\" value=\"Input Switch\" onclick=\"pbInputSwitch(8,'Y')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(8)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(8,'Y')\" ><\/td>\n<\/tr><\/table><select id='pbSelect8' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(8,'13.natural')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(8);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse8\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"8\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"29cf97d65c103ebdcb24797b00634ac2\"\/>\n<div id='pbresults8' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(8);});pbInputSwitch(8,\"Y\");<\/script>\n<\/p>\n<h3>Am Ende: Negative Indizes<\/h3>\n<p>Um an das letzte Element einer Liste zu kommen, verwende<\/p>\n<p style=\"text-align: center;\"><code>\u00ablistName\u00bb[-1]<\/code><\/p>\n<p>Allgemeiner gesprochen, gibt <code>L[-k]<\/code> das <code>k-te<\/code> Element vom Ende der Liste aus. Python behandelt dies intern, indem es das Element zu <code>L[len(L)-k]<\/code> \u00fcbersetzt. Diese Abk\u00fcrzung funktioniert auch f\u00fcr Zeichenketten!<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform9\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox9'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Palindrome<\/span><\/div>Ein <em>Palindrom<\/em> ist ein Wort, das von vorne genauso buchstabiert wie r\u00fcckw\u00e4rts. So ist zum Beispiel das englische Wort<\/p>\n<p style=\"text-align: center;\"><code>racecar<\/code><\/p>\n<p>ein Palindrom: der erste und letzte Buchstabe sind der gleiche (r), der zweite und der vorletzte Buchstabe sind der gleiche (a), etc. Schreibe eine Funktion <code>isPalindrome(S)<\/code>, die einen String s als Input nimmt und <code>True<\/code> ausgibt, wenn die Zeichenkette ein Palindrom ist und <code>False<\/code>, wenn sie es nicht ist.<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode9' id='usercode9'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory9' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput9\">Enter testing statements like <code>print(myfunction(\"test argument\"))<\/code> below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit9' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch9\" value=\"Input Switch\" onclick=\"pbInputSwitch(9,'Y')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(9)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(9,'Y')\" ><\/td>\n<\/tr><\/table><select id='pbSelect9' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(9,'13.palindrome')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(9);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse9\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"9\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"2532bbb1724ba0f361a0f2cee3cfe648\"\/>\n<div id='pbresults9' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(9);});pbInputSwitch(9,\"Y\");<\/script>\n<\/p>\n<h3><code>max<\/code> und <code>sum<\/code><\/h3>\n<p>Die Funktion <code>max<\/code>, die wir bereits gesehen haben, kann auch bei einer Liste von Zahlen verwendet werden: sie gibt die gr\u00f6\u00dfte Zahl in der Liste aus. F\u00fcr Listen mit Zahlen gibt es auch die Funktion <code>sum(L)<\/code>, welche die Summe der Zahlen in der Liste <code>L<\/code> liefert. \u00dcbrigens: W\u00e4hrend man <code>max<\/code> auch f\u00fcr Listen mit Elementen anderer Typen (wie z.B. characters) verwenden kann, funktioniert <code>sum<\/code> nur f\u00fcr Zahlen.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform10\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox10'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div><div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode10' id='usercode10'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\nlist = [3, 10, 4, 9, 0]\nprint(sum(list))\nprint(max(list))<\/textarea><\/div>\n<div id='pbhistory10' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit10' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(10)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(10,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse10\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"10\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"8391f798f13bdcc5e3debef1c0636f8c\"\/>\n<div id='pbresults10' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit10\").value = \"Run program\";document.getElementById(\"inputInUse10\").value = \"N\";<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform11\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox11'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Product<\/span><\/div>Definiere eine Funktion <code>prod(L)<\/code>, die das Produkt der Elemente in einer Liste <code>L<\/code> ausgibt.<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode11' id='usercode11'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory11' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput11\">Enter testing statements like <code>print(myfunction(\"test argument\"))<\/code> below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit11' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch11\" value=\"Input Switch\" onclick=\"pbInputSwitch(11,'Y')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(11)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(11,'Y')\" ><\/td>\n<\/tr><\/table><select id='pbSelect11' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(11,'13.product')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(11);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse11\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"11\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"950b81c2774eed4e8ee18e6885d7450c\"\/>\n<div id='pbresults11' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(11);});pbInputSwitch(11,\"Y\");<\/script>\n<\/p>\n<h3>Durch Listen schleifen<\/h3>\n<p>Es ist sehr \u00fcblich (wie in der vorherigen \u00dcbung), durch jeden Wert einer Liste zu schleifen. Python erlaubt es, eine Abk\u00fcrzung zu benutzen, um diese Art von Operation durchzuf\u00fchren, die man \u00fcblicherweise eine \"f\u00fcr alles\" Schleife oder eine \"f\u00fcr jedes\" Schleife nennt. Um genau zu sein: wenn L eine Liste ist, macht dieser Code<\/p>\n<pre>for x in L:\n  \u00abloop body block\u00bb<\/pre>das folgende: zuerst wird x auf den ersten Wert in <code>L<\/code> eingestellt und der K\u00f6rper wird ausgef\u00fchrt; dann wird x auf den zweiten Wert in <code>L<\/code> eingestellt und der K\u00f6rper ausgef\u00fchrt; dies wird f\u00fcr alle Unterpunkte von <code>L<\/code> genau so durchgef\u00fchrt.<\/p>\n<p>Hier ist ein visualisiertes Beispiel, welches die Elemente einer Liste ausgibt:<\/p>\n<p><iframe width='100%' height='480' frameborder='0' scrolling='no' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/OnlinePythonTutor3-cemc\/iframe-embed.html#code=message%3D%5B%27s%27%2C+%27o%27%2C+%27s%27%5D%0Afor+x+in+message%3A%0A+print%28x%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=400&rightStdout=1'><\/iframe><\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform12\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox12'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'><code>for in<\/code><\/span><\/div>Definiere die Funktion <code>prod(L)<\/code> wie vorher, aber diesmal verwende die neue Art von Schleife.<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode12' id='usercode12'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory12' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput12\">Enter testing statements like <code>print(myfunction(\"test argument\"))<\/code> below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit12' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch12\" value=\"Input Switch\" onclick=\"pbInputSwitch(12,'Y')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(12)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(12,'Y')\" ><\/td>\n<\/tr><\/table><select id='pbSelect12' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(12,'13.foreach')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(12);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse12\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"12\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"9ce3c28d9fba6297a35bc9d80720b66a\"\/>\n<div id='pbresults12' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(12);});pbInputSwitch(12,\"Y\");<\/script>\n<\/p>\n<p>\"F\u00fcr alle\" Schleifen funktionieren auch f\u00fcr Zeichenketten: versuche <code><a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/console\/?consolecode=for%20char%20in%20%22hello%22%3A%20print%28char%29\" target=\"_blank\">for char in \"hello\"<\/a><\/code>.<\/p>\n<p><em>Gut gemacht! Du kannst zur n\u00e4chsten Lektion weiter, oder ein paar Bonus\u00fcbungen weiter unten ausprobieren.<\/em><\/p>\n<hr \/>\n<p><div class='pybox modeNeutral' id='pybox13'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Short Answer Exercise: <\/span><span class='title'>Mystery Function<\/span><\/div>Was ist der Wert von <code>x<\/code>, der <code>mystery(x)<\/code> dazu bringen wird, unendlich zu laufen?<\/p>\n<pre>def mystery(x):\n  a = [0, 4, 0, 3, 2]\n  while x &gt; 0:\n    x = a[x]\n  return \"Done\"<\/pre><label for=\"pyShortAnswer13\">Your answer: <\/label><input type=\"text\" onkeypress=\"{if (event.keyCode==13) pbShortCheck(13)}\" id=\"pyShortAnswer13\"><div class=\"pyboxbuttons\"><input type=\"hidden\" name=\"type\" value=\"trimmableString\"\/><input type=\"hidden\" name=\"correct\" value=\"3\"\/><input type=\"hidden\" name=\"slug\" value=\"13.mystery\"\/><input type=\"hidden\" name=\"lang\" value=\"\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick = 'pbShortCheck(13)'\/><\/div><div class=\"pbresults\" id=\"pyShortResults13\"><\/div><div class=\"epilogue\">Correct!<\/div><\/div>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform14\" method=\"POST\">\n<div class='pybox modeNeutral scramble' id='pybox14'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Scramble Exercise: <\/span><span class='title'>\u00e0 la Mode<\/span><\/div>Der <em>mode<\/em> einer Liste ist das Element, das am h\u00e4ufigsten in der Liste vorkommt (bzw. maximal h\u00e4ufig vorkommt). Entwirre das folgende Programm so, dass <code>mode(L)<\/code> korrekt den mode findet, unter der Annahme, dass <code>L<\/code> eine Liste von Zahlen von 0 bis 9 ist. (In unseren Tests wird es keine zwei Zahlen geben, die gleichauf liegen in Bezug auf die maximale H\u00e4ufigkeit.).<ul class=\"pyscramble\" name=\"pyscramble\" id=\"pyscramble14\">\n <li class=\"pyscramble\">def mode(L):<\/li>\n <li class=\"pyscramble\">  for i in range(0, 10):<\/li>\n <li class=\"pyscramble\">  frequency = [0]*10<\/li>\n <li class=\"pyscramble\">  for i in L:<\/li>\n <li class=\"pyscramble\">    if frequency[i]==max(frequency):<\/li>\n <li class=\"pyscramble\">      return i<\/li>\n <li class=\"pyscramble\">    frequency[i] = frequency[i] + 1<\/li>\n<\/ul>\n<input type='hidden' id='usercode14' name='usercode14'\/>\n<div id='pbhistory14' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit14' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse14\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"14\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"eb28fafe2e15b7d63b2120819163c0b8\"\/>\n<div id='pbresults14' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit14\").value = \"Run program\";document.getElementById(\"inputInUse14\").value = \"N\";<\/script>\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Eine Liste ist eine Reihe von mehreren Variablen, die unter einem einzelnen Namen zusammengefasst werden. Anstatt ein Programm mit vielen Variablen x0, x1, x2 zu schreiben, kannst du eine einzelne Variable x definieren und ihre einzelnen Mitglieder x[0], x[1], x[2], &hellip; <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/13-de\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2653,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5526","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/5526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/users\/2653"}],"replies":[{"embeddable":true,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/comments?post=5526"}],"version-history":[{"count":46,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/5526\/revisions"}],"predecessor-version":[{"id":15942,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/5526\/revisions\/15942"}],"wp:attachment":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/media?parent=5526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}