{
    "componentChunkName": "component---src-templates-course-content-template-js",
    "path": "/part-4/5-strings-and-lists/",
    "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Learning objectives"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After this section"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be familiar with more methods for slicing strings and lists"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand what immutability of strings means"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use the methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"count"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You are already familiar with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":" syntax for accessing a part of a string:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exemplary\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"mpla"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The same syntax works with lists. Lists can be sliced just like strings:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[4, 6, 1, 2]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"more-slices","style":"position:relative;"},"children":[{"type":"text","value":"More slices"},{"type":"element","tagName":"a","properties":{"href":"#more-slices","ariaLabel":"more slices permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In fact, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":" syntax works very similarly to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":" function, which means we can also give it a step:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exemplary\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmy_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"eepa\n[7, 6, 5, 4]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we omit either of the indexes, the operator defaults to including everything. Among other things, this allows us to write a very short program to reverse a string:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Please type in a string: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exemplary"}]},{"type":"text","value":"\nyralpmexe"}]}]},{"type":"comment","value":"a similar warning is in sections 3-4, 4-6 and 5-1, check them all if you're changing this"},{"type":"element","tagName":"h2","properties":{"id":"warning-using-global-variables-within-functions","style":"position:relative;"},"children":[{"type":"text","value":"Warning: using global variables within functions"},{"type":"element","tagName":"a","properties":{"href":"#warning-using-global-variables-within-functions","ariaLabel":"warning using global variables within functions permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We know it is possible to assign new variables within function definitions, but the function can also see variables assigned outside it, in the main function. Such variables are called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" variables."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using global variables from within functions is usually a bad idea. Among other issues, doing so may cause bugs which are difficult to trace."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below is an example of a function which uses a global variable \"by mistake\":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"print_reversed"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# using the global variable instead of the parameter by accident"}]},{"type":"text","value":"\n    i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n        i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# here the global variable is assigned"}]},{"type":"text","value":"\nname_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Steve\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jean\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Katherine\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paul\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nprint_reversed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_reversed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Huey\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dewey\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Louie\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Paul\nKatherine\nJean\nSteve"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Paul\nKatherine\nJean\nSteve"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Even though both function calls have the right kind of argument, the function always prints out what is stored in the global variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name_list"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To make matters even more muddled, remember that all code for testing your functions should be placed within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\":"}]},{"type":"text","value":" block for the automatic tests. The previous example should be modified:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"print_reversed"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# using the global variable instead of the parameter by accident"}]},{"type":"text","value":"\n    i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n        i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# All the code for testing the function should be within this block"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# here the global variable is assigned"}]},{"type":"text","value":"\n    name_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Steve\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jean\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Katherine\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paul\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n    print_reversed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    print_reversed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Huey\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dewey\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Louie\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the global variable is assigned within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" block now."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The automatic tests in the TMC system are executed without running any of the code in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" block. So, in this latter example the function wouldn't even theoretically work, since it refers to the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name_list"}]},{"type":"text","value":", which doesn't exist at all when the tests are executed."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Everything reversed","tmcname":"part04-33_everything_reversed"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The starter contains an incomplete function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"everything_reversed"}]},{"type":"text","value":". It is meant to reverse both the order of a list and the characters within every item in it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Before editing, predict the starter's output for the example below, then run it and compare. Repair the function so the returned list has both the item order and the characters in every item reversed, making the smallest change which satisfies the full requirement."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the function should work:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"there\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"one more\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nnew_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" everything_reversed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"new_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['erom eno', 'elpmaxe', 'ereht', 'iH']"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"formatting-values-as-strings","style":"position:relative;"},"children":[{"type":"text","value":"Formatting values as strings"},{"type":"element","tagName":"a","properties":{"href":"#formatting-values-as-strings","ariaLabel":"formatting values as strings permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"F-strings can specify how a value should be represented. The format specifier "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".2f"}]},{"type":"text","value":" displays a floating point number with two digits after the decimal point:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":".2f"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0.33"}]}]},{"type":"element","tagName":"h2","properties":{"id":"strings-are-immutable","style":"position:relative;"},"children":[{"type":"text","value":"Strings are immutable"},{"type":"element","tagName":"a","properties":{"href":"#strings-are-immutable","ariaLabel":"strings are immutable permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strings and lists have a lot in common, especially in the way they behave with different operators. The main difference is that strings are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"immutable"}]},{"type":"text","value":". That means they cannot be changed."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exemplary\""}]},{"type":"text","value":"\nmy_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strings cannot be changed, so the execution of this program causes an error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: 'str' object does not support item assignment"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A similar error follows if you try to sort a string with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" method."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strings themselves are immutable, but the variables holding them are not. A string can be replaced by another string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following two examples are thus fundamentally different:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 281px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n      "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/programming-digital-humanities/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/4_5_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 67.82608695652173%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAABi0lEQVQ4y42T2YoCQQxF66Mb/CzfFfFBQR9FUVRE3MW9u93XO5wMJc0wYgeunaQqqeQmuvF4rP1+b1gul9psNjqfzzoej4qiyLDb7RTHsZ0/n0+9Xq+PcFwk6HQ6WRK+BIHr9Wq+w+Ggy+Wi+/2ub+KUUh6Px7vi7XZrVXsdoNOBVUg1ONGpgmBAC1ycTqcKw9CSQIenhm7W67XZxFUqFbl8Pq9Op6NyuWyA0263axiNRioUCspkMhZIEFyTiAcWi4X5VquVPVyr1X5bxqBKJEkwAo+ccYdAOKVikqHP53MDHFerVTkf+DdZMilC0uTg/gNnqYeSVhycsRZ+SrQF0BkArcAfA/nUxcc9BD4J8NPGd7vd0lWYthU45BES8wBgYEndOOQHYR1oPfnXQqiaHWVNZrOZUTEcDjWZTMwPZdh0WiwW5dizdrutUqlkY2f3sMFgMFAul1MQBLbI7Fy/339TA1V+7bCbzaYcLeCgZAJ8Zf7r+SRhq9VSr9ezBW40GqbX63WzqTKbzeoHSo8xOO+L/WIAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n  "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n          "},{"type":"element","tagName":"source","properties":{"srcSet":["/programming-digital-humanities/static/6a61f09e6f9cd09d51315c7fde142ab9/a0b58/4_5_1.webp 230w","/programming-digital-humanities/static/6a61f09e6f9cd09d51315c7fde142ab9/f44a8/4_5_1.webp 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/webp"},"children":[]},{"type":"text","value":"\n          "},{"type":"element","tagName":"source","properties":{"srcSet":["/programming-digital-humanities/static/6a61f09e6f9cd09d51315c7fde142ab9/81c8e/4_5_1.png 230w","/programming-digital-humanities/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/4_5_1.png 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/png"},"children":[]},{"type":"text","value":"\n          "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/programming-digital-humanities/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/4_5_1.png","alt":"4 5 1","title":"4 5 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n        "}]},{"type":"text","value":"\n  "}]},{"type":"text","value":"\n    "}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hey\""}]},{"type":"text","value":"\nmy_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"!\""}]}]}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 296px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n      "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/programming-digital-humanities/static/374387f1b515a88857cfbac235dfb942/b1a44/4_5_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 81.30434782608695%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAABoUlEQVQ4y5WUyaoCQQxF6/8/wP9w60IUBFtQ1yIuHMB5aOdubb1y8oiIzwELQiqVVCo3ud1BkuI41mQy0W63M409Go1MH49Hrddr9Xo9DYdDbTYbruh6vb6UgDNJEm23W+33exOSYKMvl4sJjx0OB31bQT8sHpjP51osFloul4YAjY1gBw663a5V1mq11G63NR6PLRCIq9VK1WpVlUrFoJ9OJ0uOdhuorOl0qsDlWq1mPWo0Gmo2m4qiyILpIz0rl8sqFAoGO03Te7X4OfOE9P8nyPT1E2T2wbP75N6tT75/Q6F/lE4/eJW9UwaIwIYJxNEKbPz4uEPlVHg+n/8SckBDCaRst9Fc4MwhEoPMZjOjEX4S8fjPtHkmNPzMsszE94GNZ3d4Tg0Wfu8fF4BNHC1AP0ugJ/AM/tXrdZVKJRWLRaOAP+SVAe3xMSA7je5D8UAS48T2z7DT6WgwGNwrRHsSr9IH5/bXHlIp1ff7fXuID4EzhsZk4SV7zozY7/4aLr5yuZzy+bz1lAUqJg+Srz+HV2RnINCG1vi37BR7/H3dAGa01mXsIXE7AAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n  "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n          "},{"type":"element","tagName":"source","properties":{"srcSet":["/programming-digital-humanities/static/374387f1b515a88857cfbac235dfb942/a0b58/4_5_2.webp 230w","/programming-digital-humanities/static/374387f1b515a88857cfbac235dfb942/f6dfe/4_5_2.webp 296w"],"sizes":"(max-width: 296px) 100vw, 296px","type":"image/webp"},"children":[]},{"type":"text","value":"\n          "},{"type":"element","tagName":"source","properties":{"srcSet":["/programming-digital-humanities/static/374387f1b515a88857cfbac235dfb942/81c8e/4_5_2.png 230w","/programming-digital-humanities/static/374387f1b515a88857cfbac235dfb942/b1a44/4_5_2.png 296w"],"sizes":"(max-width: 296px) 100vw, 296px","type":"image/png"},"children":[]},{"type":"text","value":"\n          "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/programming-digital-humanities/static/374387f1b515a88857cfbac235dfb942/b1a44/4_5_2.png","alt":"4 5 2","title":"4 5 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n        "}]},{"type":"text","value":"\n  "}]},{"type":"text","value":"\n    "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first example changes the contents of the referenced list. The second example replaces the reference to the original string with a reference to another string. The original string is still somewhere in computer memory, but there is no reference to it, and it cannot be used in the program any longer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We will return to this subject in the next part, where references to lists are explored in more detail."}]},{"type":"element","tagName":"h2","properties":{"id":"more-methods-for-lists-and-strings","style":"position:relative;"},"children":[{"type":"text","value":"More methods for lists and strings"},{"type":"element","tagName":"a","properties":{"href":"#more-methods-for-lists-and-strings","ariaLabel":"more methods for lists and strings permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"count"}]},{"type":"text","value":" counts the number of times the specified item or substring occurs in the target. The method works similarly with both strings and lists:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"How much wood would a woodchuck chuck if a woodchuck could chuck wood\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ch\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmy_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method will not count overlapping occurrences. For example, in the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aaaa"}]},{"type":"text","value":" the method counts only two occurrences of the substring "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aa"}]},{"type":"text","value":", even though there would actually be three if overlapping occurrences were allowed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":" creates a new string, where a specified substring is replaced with another string:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hi there\""}]},{"type":"text","value":"\nnew_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hey\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"new_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hey there"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method will replace all occurrences of the substring:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sentence "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"sheila sells seashells on the seashore\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sentence"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"she\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"SHE\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"SHEila sells seaSHElls on the seashore"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":" method, a typical mistake is forgetting that strings are immutable:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python is fun\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Replaces the substring but doesn't store the result..."}]},{"type":"text","value":"\nmy_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python is fun"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the old string is no longer needed, the new string can be assigned to the same variable:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python is fun\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Replaces the substring and stores the result in the same variable"}]},{"type":"text","value":"\nmy_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Java is fun"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Most common character","tmcname":"part04-34_most_common_character"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"most_common_character"}]},{"type":"text","value":", which takes a string argument. The function returns the character which has the most occurrences within the string. If there are many characters with equally many occurrences, the one which appears first in the string should be returned."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"first_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcdbde\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most_common_character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"first_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsecond_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exemplaryelementary\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most_common_character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"second_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"b\ne"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"No vowels allowed","tmcname":"part04-35_no_vowels_allowed"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"no_vowels"}]},{"type":"text","value":", which takes a string argument. The function returns a new string, which should be the same as the original but with all vowels removed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can assume the string will contain only characters from the lowercase English alphabet a...z."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_string "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"this is an example\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"no_vowels"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ths s n xmpl"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"No shouting allowed","tmcname":"part04-36_no_shouting_allowed"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Python string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isupper()"}]},{"type":"text","value":" returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if a string consists of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"only"}]},{"type":"text","value":" uppercase characters."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"XYZ\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nis_it_upper "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Abc\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"is_it_upper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Copy your own "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"no_vowels"}]},{"type":"text","value":" function from the preceding exercise into this exercise. Adapt its filtering pattern into a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"no_shouting"}]},{"type":"text","value":", which takes a list of strings and returns a new list containing only items which are not solely uppercase. The data shape changes from one string to a list of strings, so decide what the loop should now visit and how the result should be built."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isupper"}]},{"type":"text","value":" method for the filtering decision."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"my_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"def\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"UPPER\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ANOTHERUPPER\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lower\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"another lower\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Capitalized\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\npruned_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" no_shouting"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pruned_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['def', 'lower', 'another lower', 'Capitalized']"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"tuples-a-fixed-group-of-values","style":"position:relative;"},"children":[{"type":"text","value":"Tuples: a fixed group of values"},{"type":"element","tagName":"a","properties":{"href":"#tuples-a-fixed-group-of-values","ariaLabel":"tuples a fixed group of values permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lists are not the only way of keeping several values together. A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tuple"}]},{"type":"text","value":" keeps a small, fixed number of values. Tuple values are written in parentheses and accessed by position, just as with a list:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"publication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Frankenstein\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1818"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"publication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"publication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tuples are especially convenient when the positions have an obvious meaning, such as a pair of coordinates, a start and end year, or two results returned by a function. A tuple can be passed as one argument and unpacked into separate variables:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"print_period"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"period"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n    first_year"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" last_year "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" period\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"From "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"first_year"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" to "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"last_year"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nyears "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1800"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1899"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_period"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"years"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A function can likewise return a tuple, which the caller unpacks. The parentheses may be omitted in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" statement:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"first_and_last"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nfirst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" last "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" first_and_last"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hamlet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Frankenstein\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Beloved\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Unlike a list, a tuple cannot be changed after it is created. This makes it a good signal that the number and order of the values are fixed. If there are many values, or their positions are not immediately obvious, the named structures introduced in the next section are usually more readable."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Longest run in ordered data","tmcname":"part04-37_neighbours_in_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lists often contain observations in a meaningful order, such as document types arranged by publication date. A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"run"}]},{"type":"text","value":" is an uninterrupted sequence of the same value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please complete the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"longest_run"}]},{"type":"text","value":", which finds the longest run in a non-empty list. The function should return a tuple containing the value in that run and the length of the run. If several runs share the longest length, return the first one."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the longest run below consists of three consecutive "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"diary\""}]},{"type":"text","value":" values:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"document_types "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"letter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"letter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"diary\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"diary\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"diary\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"letter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"longest_run"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"document_types"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"('diary', 3)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The starter code names four pieces of state: the value and length of the current run, and the value and length of the longest run found so far. The first item initializes both runs. This is why the loop processes the rest of the list with the slice "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"labels[1:]"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Complete the loop so that it first updates the current run and then updates the longest run when necessary. Finally, return the longest run as a tuple."}]}]},{"type":"element","tagName":"h2","properties":{"id":"developing-a-larger-programming-project","style":"position:relative;"},"children":[{"type":"text","value":"Developing a larger programming project"},{"type":"element","tagName":"a","properties":{"href":"#developing-a-larger-programming-project","ariaLabel":"developing a larger programming project permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This fourth part culminates in a slightly larger programming project, where you get to apply many of the techniques learnt so far."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rule No. 1 in tackling any programming project is not trying to solve everything at once. The program should be built out of smaller sections, such as helper functions. You should verify the operation of each part before moving on to the next. If you try to handle too much at once, most likely only chaos ensues."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To do this you will need a way of testing your functions outside the main function. You can achieve this by defining a main function explicitly, and calling this function from outside any other function in the program. A single function call is then easy to comment out for testing. The first steps in building the following programming project could look like this:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"main"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n    points "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# your program code goes here"}]},{"type":"text","value":"\n\nmain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the helper functions can be tested without running the main function:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# helper function for determining the grade based on the amount of points"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"grade"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"points"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# more code"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"main"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n    all_points "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# your program code goes here"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# comment out the main function"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"#main()"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# test the helper function"}]},{"type":"text","value":"\nstudent_points "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"35"}]},{"type":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" grade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"student_points"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"passing-data-from-one-function-to-another","style":"position:relative;"},"children":[{"type":"text","value":"Passing data from one function to another"},{"type":"element","tagName":"a","properties":{"href":"#passing-data-from-one-function-to-another","ariaLabel":"passing data from one function to another permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"comment","value":"- see also section 6-4, some significant overlap"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When a program contains multiple functions, the question arises: how do you pass data from one function to another?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example asks the user for some integer values. The program then prints out these values and performs an \"analysis\" on them. The program is divided into three separate functions:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"input_from_user"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"how_many"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Please type in "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"how_many"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" numbers:\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"how_many"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n        number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Number "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": \""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n        numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" numbers\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"print_result"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The numbers are: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n        "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"analyze"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n    mean "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"There are altogether "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" numbers, the mean is "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"mean"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", the smallest is "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" and the greatest is "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the \"main function\" using these functions"}]},{"type":"text","value":"\ninputs "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" input_from_user"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"inputs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nanalysis_result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" analyze"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"inputs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"analysis_result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the program is executed, it could go like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in 5 numbers:\nNumber 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nNumber 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"34"}]},{"type":"text","value":"\nNumber 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-32"}]},{"type":"text","value":"\nNumber 4: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"99"}]},{"type":"text","value":"\nNumber 5: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-53"}]},{"type":"text","value":"\nThe numbers are:\n10\n34\n-32\n99\n-53\nThere are altogether 5 numbers, the mean is 11.6, the smallest is -53 and the greatest is 99"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea here is that the main function \"saves\" all data processed by the program. In this case all that is needed is the input from the user in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inputs"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the input is needed in a function, it is passed as an argument. This happens with the functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_result"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"analyze"}]},{"type":"text","value":". If the function produces data that is needed elsewhere in the program, the function returns it with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" command, and it is stored in a variable in the main function. This happens with the functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input_from_user"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"analyze"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You could use the global variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inputs"}]},{"type":"text","value":" from the main function directly in the helper functions. We have already covered why that is a bad idea, but "},{"type":"element","tagName":"a","properties":{"href":"https://softwareengineering.stackexchange.com/questions/148108/why-is-global-state-so-evil","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"here is another explanation"}]},{"type":"text","value":". If functions are able to change a global variable, unexpected things may start happening in the program, especially when the number of functions grows large."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Passing data into and out of functions is best handled by arguments and return values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You could also separate the implicit main function in the example above into its own function. Then the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inputs"}]},{"type":"text","value":" would no longer be a global variable, but instead a local variable within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main"}]},{"type":"text","value":" function:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# your main function goes here"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"main"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n    inputs "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" input_from_user"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    print_result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"inputs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n    analysis_result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" analyze"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"inputs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n    "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"analysis_result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# run the main function"}]},{"type":"text","value":"\nmain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Grade statistics","tmcname":"part04-38_grade_statistics"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will write a program for printing out grade statistics for a university course."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program asks the user for results from different students on the course. These include exam points and numbers of exercises completed. The program then prints out statistics based on the results."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exam points are integers between 0 and 20. The number of exercises completed is an integer between 0 and 100."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program keeps asking for input until the user types in an empty line. You may assume all lines contain valid input, which means that there are two integers on each line, or the line is empty."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"And example of how the data is typed in:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exam points and exercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15 87"}]},{"type":"text","value":"\nExam points and exercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10 55"}]},{"type":"text","value":"\nExam points and exercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"11 40"}]},{"type":"text","value":"\nExam points and exercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4 17"}]},{"type":"text","value":"\nExam points and exercises completed:\nStatistics:"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the user types in an empty line, the program prints out statistics. They are formulated as follows:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercises completed are converted into "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exercise points"}]},{"type":"text","value":", so that completing at least 10% of the exercises grants one point, 20% grants two points, and so forth. Completing all 100 exercises grants 10 exercise points. The number of exercise points granted is an integer value, rounded down."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The grade for the course is determined based on the following table:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"exam points + exercise points"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"grade"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0–14"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0 (i.e. fail)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"15–17"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"1"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"18–20"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"2"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"21–23"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"24–27"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"4"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"28–30"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is also an exam cutoff threshold. If a student received less than 10 points from the exam, they automatically fail the course, regardless of their total number of points."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With the example input from above the program would print out the following statistics:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Statistics:\nPoints average: 14.5\nPass percentage: 75.0\nGrade distribution:\n  5:\n  4:\n  3: *\n  2:\n  1: **\n  0: *\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Floating point numbers should be printed out with one decimal precision."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" this exercise doesn't ask you to write any specific functions, so you should "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" place any code within an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block. If any functionality in your program is e.g. in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main"}]},{"type":"text","value":" function, you should include the code calling this function normally, and not contain it in an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" block like in the exercises which specify certain functions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The user input in this program consists of lines with two integer values:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exam points and exercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15 87"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You have to first split the input line in two and then convert the sections into integers with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" function. Splitting the input can be achieved in the same way as in the exercise "},{"type":"element","tagName":"a","properties":{"href":"/programming-digital-humanities/part-4/2-more-functions#programming-exercise-first-second-and-last-words"},"children":[{"type":"text","value":"First, second and last words"}]},{"type":"text","value":", but there is a simpler way as well. The string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" will chop the input up nicely. You will find more information by searching for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"python string split"}]},{"type":"text","value":" online."}]}]},{"type":"element","tagName":"h2","properties":{"id":"summary-of-parts-1-4","style":"position:relative;"},"children":[{"type":"text","value":"Summary of parts 1-4"},{"type":"element","tagName":"a","properties":{"href":"#summary-of-parts-1-4","ariaLabel":"summary of parts 1 4 permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In parts 1-4, you've learned about the fundamental building blocks of programs: the flow control instructions that dictate the order of execution, variables for organizing and storing data, as well as functions as a means to organise and package code into reusable components. With these, you've learned a basic capability to read code and follow its execution."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"At this point, if you've not already done so, check out the "},{"type":"element","tagName":"a","properties":{"href":"https://jiemakel.gitbook.io/cl4hss/data-processing-fundamental-concepts-of-programming-for-humanists","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"fundamental concepts of programming for humanists"}]},{"type":"text","value":" part of The "},{"type":"element","tagName":"a","properties":{"href":"https://jiemakel.gitbook.io/cl4hss/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Computational Literacy course"}]},{"type":"text","value":", which tries to cram the absolute fundamentals into as minimal a space as possible. By seeing the same thing explained concisely a bit differently, this may improve your undestanding (as has been verified by prior students on this course). Additionally, because the fundamental concepts -section also shows you these fundamentals across not just Python but also R and Scala, you'll get an insight into how these concepts are not just features of a particular language, but present in most with minor differences. Finally, the fundamental concepts section also provides a handy "},{"type":"element","tagName":"a","properties":{"href":"https://nbviewer.jupyter.org/github/jiemakel/dhintro/blob/master/programming_cheatsheet.ipynb","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"cheatsheet of key concepts"}]},{"type":"text","value":" which may be helpful as a reminder and an overview."}]}]},"html":"<div><text-box variant='learningObjectives' name=\"Learning objectives\"><p>After this section</p><ul>\n<li>You will be familiar with more methods for slicing strings and lists</li>\n<li>You will understand what immutability of strings means</li>\n<li>You will be able to use the methods <code class=\"language-text\">count</code> and <code class=\"language-text\">replace</code></li>\n</ul></text-box><p>You are already familiar with the <code class=\"language-text\">[]</code> syntax for accessing a part of a string:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_string <span class=\"token operator\">=</span> <span class=\"token string\">\"exemplary\"</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>my_string<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">:</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>mpla</p></sample-output><p>The same syntax works with lists. Lists can be sliced just like strings:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_list <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span><span class=\"token number\">4</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">4</span><span class=\"token punctuation\">,</span><span class=\"token number\">6</span><span class=\"token punctuation\">,</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">4</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>my_list<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">:</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>[4, 6, 1, 2]</p></sample-output><h2 id=\"more-slices\" style=\"position:relative;\">More slices<a href=\"#more-slices\" aria-label=\"more slices permalink\" class=\"anchor after\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h2><p>In fact, the <code class=\"language-text\">[]</code> syntax works very similarly to the <code class=\"language-text\">range</code> function, which means we can also give it a step:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_string <span class=\"token operator\">=</span> <span class=\"token string\">\"exemplary\"</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>my_string<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">:</span><span class=\"token number\">7</span><span class=\"token punctuation\">:</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\nmy_list <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span><span class=\"token number\">4</span><span class=\"token punctuation\">,</span><span class=\"token number\">5</span><span class=\"token punctuation\">,</span><span class=\"token number\">6</span><span class=\"token punctuation\">,</span><span class=\"token number\">7</span><span class=\"token punctuation\">,</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>my_list<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">:</span><span class=\"token number\">2</span><span class=\"token punctuation\">:</span><span class=\"token operator\">-</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>eepa\n[7, 6, 5, 4]</p></sample-output><p>If we omit either of the indexes, the operator defaults to including everything. Among other things, this allows us to write a very short program to reverse a string:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_string <span class=\"token operator\">=</span> <span class=\"token builtin\">input</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Please type in a string: \"</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>my_string<span class=\"token punctuation\">[</span><span class=\"token punctuation\">:</span><span class=\"token punctuation\">:</span><span class=\"token operator\">-</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>Please type in a string: <strong>exemplary</strong>\nyralpmexe</p></sample-output><!--a similar warning is in sections 3-4, 4-6 and 5-1, check them all if you're changing this--><h2 id=\"warning-using-global-variables-within-functions\" style=\"position:relative;\">Warning: using global variables within functions<a href=\"#warning-using-global-variables-within-functions\" aria-label=\"warning using global variables within functions permalink\" class=\"anchor after\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h2><p>We know it is possible to assign new variables within function definitions, but the function can also see variables assigned outside it, in the main function. Such variables are called <em>global</em> variables.</p><p>Using global variables from within functions is usually a bad idea. Among other issues, doing so may cause bugs which are difficult to trace.</p><p>Below is an example of a function which uses a global variable \"by mistake\":</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">def</span> <span class=\"token function\">print_reversed</span><span class=\"token punctuation\">(</span>names<span class=\"token punctuation\">:</span> <span class=\"token builtin\">list</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token comment\"># using the global variable instead of the parameter by accident</span>\n    i <span class=\"token operator\">=</span> <span class=\"token builtin\">len</span><span class=\"token punctuation\">(</span>name_list<span class=\"token punctuation\">)</span> <span class=\"token operator\">-</span> <span class=\"token number\">1</span>\n    <span class=\"token keyword\">while</span> i <span class=\"token operator\">>=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">:</span>\n        <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>name_list<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n        i <span class=\"token operator\">-=</span> <span class=\"token number\">1</span>\n\n<span class=\"token comment\"># here the global variable is assigned</span>\nname_list <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">\"Steve\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Jean\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Katherine\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Paul\"</span><span class=\"token punctuation\">]</span>\nprint_reversed<span class=\"token punctuation\">(</span>name_list<span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\nprint_reversed<span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token string\">\"Huey\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Dewey\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Louie\"</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>Paul\nKatherine\nJean\nSteve</p><p>Paul\nKatherine\nJean\nSteve</p></sample-output><p>Even though both function calls have the right kind of argument, the function always prints out what is stored in the global variable <code class=\"language-text\">name_list</code>.</p><p>To make matters even more muddled, remember that all code for testing your functions should be placed within the <code class=\"language-text\">if __name__ == \"__main__\":</code> block for the automatic tests. The previous example should be modified:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">def</span> <span class=\"token function\">print_reversed</span><span class=\"token punctuation\">(</span>names<span class=\"token punctuation\">:</span> <span class=\"token builtin\">list</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token comment\"># using the global variable instead of the parameter by accident</span>\n    i <span class=\"token operator\">=</span> <span class=\"token builtin\">len</span><span class=\"token punctuation\">(</span>name_list<span class=\"token punctuation\">)</span> <span class=\"token operator\">-</span> <span class=\"token number\">1</span>\n    <span class=\"token keyword\">while</span> i<span class=\"token operator\">>=</span><span class=\"token number\">0</span><span class=\"token punctuation\">:</span>\n        <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>name_list<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n        i <span class=\"token operator\">-=</span> <span class=\"token number\">1</span>\n\n<span class=\"token comment\"># All the code for testing the function should be within this block</span>\n<span class=\"token keyword\">if</span> __name__ <span class=\"token operator\">==</span> <span class=\"token string\">\"__main__\"</span><span class=\"token punctuation\">:</span>\n    <span class=\"token comment\"># here the global variable is assigned</span>\n    name_list <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">\"Steve\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Jean\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Katherine\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Paul\"</span><span class=\"token punctuation\">]</span>\n    print_reversed<span class=\"token punctuation\">(</span>name_list<span class=\"token punctuation\">)</span>\n    <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    print_reversed<span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token string\">\"Huey\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Dewey\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Louie\"</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div><p>Notice the global variable is assigned within the <code class=\"language-text\">if</code> block now.</p><p>The automatic tests in the TMC system are executed without running any of the code in the <code class=\"language-text\">if</code> block. So, in this latter example the function wouldn't even theoretically work, since it refers to the variable <code class=\"language-text\">name_list</code>, which doesn't exist at all when the tests are executed.</p><programming-exercise name='Everything reversed' tmcname='part04-33_everything_reversed'><p>The starter contains an incomplete function named <code class=\"language-text\">everything_reversed</code>. It is meant to reverse both the order of a list and the characters within every item in it.</p><p>Before editing, predict the starter's output for the example below, then run it and compare. Repair the function so the returned list has both the item order and the characters in every item reversed, making the smallest change which satisfies the full requirement.</p><p>An example of how the function should work:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_list <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">\"Hi\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"there\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"example\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"one more\"</span><span class=\"token punctuation\">]</span>\nnew_list <span class=\"token operator\">=</span> everything_reversed<span class=\"token punctuation\">(</span>my_list<span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>new_list<span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>['erom eno', 'elpmaxe', 'ereht', 'iH']</p></sample-output></programming-exercise><h2 id=\"formatting-values-as-strings\" style=\"position:relative;\">Formatting values as strings<a href=\"#formatting-values-as-strings\" aria-label=\"formatting values as strings permalink\" class=\"anchor after\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h2><p>F-strings can specify how a value should be represented. The format specifier <code class=\"language-text\">.2f</code> displays a floating point number with two digits after the decimal point:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">number <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">/</span> <span class=\"token number\">3</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span><span class=\"token string-interpolation\"><span class=\"token string\">f\"</span><span class=\"token interpolation\"><span class=\"token punctuation\">{</span>number<span class=\"token punctuation\">:</span><span class=\"token format-spec\">.2f</span><span class=\"token punctuation\">}</span></span><span class=\"token string\">\"</span></span><span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>0.33</p></sample-output><h2 id=\"strings-are-immutable\" style=\"position:relative;\">Strings are immutable<a href=\"#strings-are-immutable\" aria-label=\"strings are immutable permalink\" class=\"anchor after\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h2><p>Strings and lists have a lot in common, especially in the way they behave with different operators. The main difference is that strings are <em>immutable</em>. That means they cannot be changed.</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_string <span class=\"token operator\">=</span> <span class=\"token string\">\"exemplary\"</span>\nmy_string<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token string\">\"a\"</span></code></pre></div><p>Strings cannot be changed, so the execution of this program causes an error:</p><sample-output><p>TypeError: 'str' object does not support item assignment</p></sample-output><p>A similar error follows if you try to sort a string with the <code class=\"language-text\">sort</code> method.</p><p>Strings themselves are immutable, but the variables holding them are not. A string can be replaced by another string.</p><p>The following two examples are thus fundamentally different:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_list <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span>\nmy_list<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">10</span></code></pre></div><span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 281px; margin-bottom: 1rem;\">\n      <a class=\"gatsby-resp-image-link\" href=\"/programming-digital-humanities/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/4_5_1.png\" style=\"display: block\" target=\"_blank\" rel=\"noopener\">\n    <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 67.82608695652173%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAABi0lEQVQ4y42T2YoCQQxF66Mb/CzfFfFBQR9FUVRE3MW9u93XO5wMJc0wYgeunaQqqeQmuvF4rP1+b1gul9psNjqfzzoej4qiyLDb7RTHsZ0/n0+9Xq+PcFwk6HQ6WRK+BIHr9Wq+w+Ggy+Wi+/2ub+KUUh6Px7vi7XZrVXsdoNOBVUg1ONGpgmBAC1ycTqcKw9CSQIenhm7W67XZxFUqFbl8Pq9Op6NyuWyA0263axiNRioUCspkMhZIEFyTiAcWi4X5VquVPVyr1X5bxqBKJEkwAo+ccYdAOKVikqHP53MDHFerVTkf+DdZMilC0uTg/gNnqYeSVhycsRZ+SrQF0BkArcAfA/nUxcc9BD4J8NPGd7vd0lWYthU45BES8wBgYEndOOQHYR1oPfnXQqiaHWVNZrOZUTEcDjWZTMwPZdh0WiwW5dizdrutUqlkY2f3sMFgMFAul1MQBLbI7Fy/339TA1V+7bCbzaYcLeCgZAJ8Zf7r+SRhq9VSr9ezBW40GqbX63WzqTKbzeoHSo8xOO+L/WIAAAAASUVORK5CYII='); background-size: cover; display: block;\"></span>\n  <picture>\n          <source srcset=\"/programming-digital-humanities/static/6a61f09e6f9cd09d51315c7fde142ab9/a0b58/4_5_1.webp 230w,\n/programming-digital-humanities/static/6a61f09e6f9cd09d51315c7fde142ab9/f44a8/4_5_1.webp 281w\" sizes=\"(max-width: 281px) 100vw, 281px\" type=\"image/webp\">\n          <source srcset=\"/programming-digital-humanities/static/6a61f09e6f9cd09d51315c7fde142ab9/81c8e/4_5_1.png 230w,\n/programming-digital-humanities/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/4_5_1.png 281w\" sizes=\"(max-width: 281px) 100vw, 281px\" type=\"image/png\">\n          <img class=\"gatsby-resp-image-image\" src=\"/programming-digital-humanities/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/4_5_1.png\" alt=\"4 5 1\" title=\"4 5 1\" loading=\"lazy\" decoding=\"async\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\">\n        </picture>\n  </a>\n    </span><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_string <span class=\"token operator\">=</span> <span class=\"token string\">\"Hey\"</span>\nmy_string <span class=\"token operator\">=</span> my_string <span class=\"token operator\">+</span> <span class=\"token string\">\"!\"</span></code></pre></div><span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 296px; margin-bottom: 1rem;\">\n      <a class=\"gatsby-resp-image-link\" href=\"/programming-digital-humanities/static/374387f1b515a88857cfbac235dfb942/b1a44/4_5_2.png\" style=\"display: block\" target=\"_blank\" rel=\"noopener\">\n    <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 81.30434782608695%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAABoUlEQVQ4y5WUyaoCQQxF6/8/wP9w60IUBFtQ1yIuHMB5aOdubb1y8oiIzwELQiqVVCo3ud1BkuI41mQy0W63M409Go1MH49Hrddr9Xo9DYdDbTYbruh6vb6UgDNJEm23W+33exOSYKMvl4sJjx0OB31bQT8sHpjP51osFloul4YAjY1gBw663a5V1mq11G63NR6PLRCIq9VK1WpVlUrFoJ9OJ0uOdhuorOl0qsDlWq1mPWo0Gmo2m4qiyILpIz0rl8sqFAoGO03Te7X4OfOE9P8nyPT1E2T2wbP75N6tT75/Q6F/lE4/eJW9UwaIwIYJxNEKbPz4uEPlVHg+n/8SckBDCaRst9Fc4MwhEoPMZjOjEX4S8fjPtHkmNPzMsszE94GNZ3d4Tg0Wfu8fF4BNHC1AP0ugJ/AM/tXrdZVKJRWLRaOAP+SVAe3xMSA7je5D8UAS48T2z7DT6WgwGNwrRHsSr9IH5/bXHlIp1ff7fXuID4EzhsZk4SV7zozY7/4aLr5yuZzy+bz1lAUqJg+Srz+HV2RnINCG1vi37BR7/H3dAGa01mXsIXE7AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"></span>\n  <picture>\n          <source srcset=\"/programming-digital-humanities/static/374387f1b515a88857cfbac235dfb942/a0b58/4_5_2.webp 230w,\n/programming-digital-humanities/static/374387f1b515a88857cfbac235dfb942/f6dfe/4_5_2.webp 296w\" sizes=\"(max-width: 296px) 100vw, 296px\" type=\"image/webp\">\n          <source srcset=\"/programming-digital-humanities/static/374387f1b515a88857cfbac235dfb942/81c8e/4_5_2.png 230w,\n/programming-digital-humanities/static/374387f1b515a88857cfbac235dfb942/b1a44/4_5_2.png 296w\" sizes=\"(max-width: 296px) 100vw, 296px\" type=\"image/png\">\n          <img class=\"gatsby-resp-image-image\" src=\"/programming-digital-humanities/static/374387f1b515a88857cfbac235dfb942/b1a44/4_5_2.png\" alt=\"4 5 2\" title=\"4 5 2\" loading=\"lazy\" decoding=\"async\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\">\n        </picture>\n  </a>\n    </span><p>The first example changes the contents of the referenced list. The second example replaces the reference to the original string with a reference to another string. The original string is still somewhere in computer memory, but there is no reference to it, and it cannot be used in the program any longer.</p><p>We will return to this subject in the next part, where references to lists are explored in more detail.</p><h2 id=\"more-methods-for-lists-and-strings\" style=\"position:relative;\">More methods for lists and strings<a href=\"#more-methods-for-lists-and-strings\" aria-label=\"more methods for lists and strings permalink\" class=\"anchor after\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h2><p>The method <code class=\"language-text\">count</code> counts the number of times the specified item or substring occurs in the target. The method works similarly with both strings and lists:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_string <span class=\"token operator\">=</span> <span class=\"token string\">\"How much wood would a woodchuck chuck if a woodchuck could chuck wood\"</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>my_string<span class=\"token punctuation\">.</span>count<span class=\"token punctuation\">(</span><span class=\"token string\">\"ch\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n\nmy_list <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">4</span><span class=\"token punctuation\">,</span><span class=\"token number\">5</span><span class=\"token punctuation\">,</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>my_list<span class=\"token punctuation\">.</span>count<span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>5\n3</p></sample-output><p>The method will not count overlapping occurrences. For example, in the string <code class=\"language-text\">aaaa</code> the method counts only two occurrences of the substring <code class=\"language-text\">aa</code>, even though there would actually be three if overlapping occurrences were allowed.</p><p>The method <code class=\"language-text\">replace</code> creates a new string, where a specified substring is replaced with another string:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_string <span class=\"token operator\">=</span> <span class=\"token string\">\"Hi there\"</span>\nnew_string <span class=\"token operator\">=</span> my_string<span class=\"token punctuation\">.</span>replace<span class=\"token punctuation\">(</span><span class=\"token string\">\"Hi\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Hey\"</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>new_string<span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>Hey there</p></sample-output><p>The method will replace all occurrences of the substring:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">sentence <span class=\"token operator\">=</span> <span class=\"token string\">\"sheila sells seashells on the seashore\"</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>sentence<span class=\"token punctuation\">.</span>replace<span class=\"token punctuation\">(</span><span class=\"token string\">\"she\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"SHE\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>SHEila sells seaSHElls on the seashore</p></sample-output><p>When using the <code class=\"language-text\">replace</code> method, a typical mistake is forgetting that strings are immutable:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_string <span class=\"token operator\">=</span> <span class=\"token string\">\"Python is fun\"</span>\n\n<span class=\"token comment\"># Replaces the substring but doesn't store the result...</span>\nmy_string<span class=\"token punctuation\">.</span>replace<span class=\"token punctuation\">(</span><span class=\"token string\">\"Python\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Java\"</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>my_string<span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>Python is fun</p></sample-output><p>If the old string is no longer needed, the new string can be assigned to the same variable:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_string <span class=\"token operator\">=</span> <span class=\"token string\">\"Python is fun\"</span>\n\n<span class=\"token comment\"># Replaces the substring and stores the result in the same variable</span>\nmy_string <span class=\"token operator\">=</span> my_string<span class=\"token punctuation\">.</span>replace<span class=\"token punctuation\">(</span><span class=\"token string\">\"Python\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Java\"</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>my_string<span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>Java is fun</p></sample-output><programming-exercise name='Most common character' tmcname='part04-34_most_common_character'><p>Please write a function named <code class=\"language-text\">most_common_character</code>, which takes a string argument. The function returns the character which has the most occurrences within the string. If there are many characters with equally many occurrences, the one which appears first in the string should be returned.</p><p>An example of expected behaviour:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">first_string <span class=\"token operator\">=</span> <span class=\"token string\">\"abcdbde\"</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>most_common_character<span class=\"token punctuation\">(</span>first_string<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n\nsecond_string <span class=\"token operator\">=</span> <span class=\"token string\">\"exemplaryelementary\"</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>most_common_character<span class=\"token punctuation\">(</span>second_string<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>b\ne</p></sample-output></programming-exercise><programming-exercise name='No vowels allowed' tmcname='part04-35_no_vowels_allowed'><p>Please write a function named <code class=\"language-text\">no_vowels</code>, which takes a string argument. The function returns a new string, which should be the same as the original but with all vowels removed.</p><p>You can assume the string will contain only characters from the lowercase English alphabet a...z.</p><p>An example of expected behaviour:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_string <span class=\"token operator\">=</span> <span class=\"token string\">\"this is an example\"</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>no_vowels<span class=\"token punctuation\">(</span>my_string<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>ths s n xmpl</p></sample-output></programming-exercise><programming-exercise name='No shouting allowed' tmcname='part04-36_no_shouting_allowed'><p>The Python string method <code class=\"language-text\">isupper()</code> returns <code class=\"language-text\">True</code> if a string consists of <em>only</em> uppercase characters.</p><p>Some examples:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"XYZ\"</span><span class=\"token punctuation\">.</span>isupper<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n\nis_it_upper <span class=\"token operator\">=</span> <span class=\"token string\">\"Abc\"</span><span class=\"token punctuation\">.</span>isupper<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>is_it_upper<span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>True\nFalse</p></sample-output><p>Copy your own <code class=\"language-text\">no_vowels</code> function from the preceding exercise into this exercise. Adapt its filtering pattern into a function named <code class=\"language-text\">no_shouting</code>, which takes a list of strings and returns a new list containing only items which are not solely uppercase. The data shape changes from one string to a list of strings, so decide what the loop should now visit and how the result should be built.</p><p>Use the <code class=\"language-text\">isupper</code> method for the filtering decision.</p><p>An example of expected behaviour:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">my_list <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">\"ABC\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"def\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"UPPER\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"ANOTHERUPPER\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"lower\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"another lower\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Capitalized\"</span><span class=\"token punctuation\">]</span>\npruned_list <span class=\"token operator\">=</span> no_shouting<span class=\"token punctuation\">(</span>my_list<span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>pruned_list<span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>['def', 'lower', 'another lower', 'Capitalized']</p></sample-output></programming-exercise><h2 id=\"tuples-a-fixed-group-of-values\" style=\"position:relative;\">Tuples: a fixed group of values<a href=\"#tuples-a-fixed-group-of-values\" aria-label=\"tuples a fixed group of values permalink\" class=\"anchor after\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h2><p>Lists are not the only way of keeping several values together. A <em>tuple</em> keeps a small, fixed number of values. Tuple values are written in parentheses and accessed by position, just as with a list:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">publication <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token string\">\"Frankenstein\"</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1818</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>publication<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>publication<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div><p>Tuples are especially convenient when the positions have an obvious meaning, such as a pair of coordinates, a start and end year, or two results returned by a function. A tuple can be passed as one argument and unpacked into separate variables:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">def</span> <span class=\"token function\">print_period</span><span class=\"token punctuation\">(</span>period<span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    first_year<span class=\"token punctuation\">,</span> last_year <span class=\"token operator\">=</span> period\n    <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span><span class=\"token string-interpolation\"><span class=\"token string\">f\"From </span><span class=\"token interpolation\"><span class=\"token punctuation\">{</span>first_year<span class=\"token punctuation\">}</span></span><span class=\"token string\"> to </span><span class=\"token interpolation\"><span class=\"token punctuation\">{</span>last_year<span class=\"token punctuation\">}</span></span><span class=\"token string\">\"</span></span><span class=\"token punctuation\">)</span>\n\nyears <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token number\">1800</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1899</span><span class=\"token punctuation\">)</span>\nprint_period<span class=\"token punctuation\">(</span>years<span class=\"token punctuation\">)</span></code></pre></div><p>A function can likewise return a tuple, which the caller unpacks. The parentheses may be omitted in the <code class=\"language-text\">return</code> statement:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">def</span> <span class=\"token function\">first_and_last</span><span class=\"token punctuation\">(</span>items<span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token keyword\">return</span> items<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> items<span class=\"token punctuation\">[</span><span class=\"token operator\">-</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span>\n\nfirst<span class=\"token punctuation\">,</span> last <span class=\"token operator\">=</span> first_and_last<span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token string\">\"Hamlet\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Frankenstein\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Beloved\"</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div><p>Unlike a list, a tuple cannot be changed after it is created. This makes it a good signal that the number and order of the values are fixed. If there are many values, or their positions are not immediately obvious, the named structures introduced in the next section are usually more readable.</p><programming-exercise name='Longest run in ordered data' tmcname='part04-37_neighbours_in_list'><p>Lists often contain observations in a meaningful order, such as document types arranged by publication date. A <em>run</em> is an uninterrupted sequence of the same value.</p><p>Please complete the function <code class=\"language-text\">longest_run</code>, which finds the longest run in a non-empty list. The function should return a tuple containing the value in that run and the length of the run. If several runs share the longest length, return the first one.</p><p>For example, the longest run below consists of three consecutive <code class=\"language-text\">\"diary\"</code> values:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">document_types <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">\"letter\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"letter\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"diary\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"diary\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"diary\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"letter\"</span><span class=\"token punctuation\">]</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>longest_run<span class=\"token punctuation\">(</span>document_types<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span></code></pre></div><sample-output><p>('diary', 3)</p></sample-output><p>The starter code names four pieces of state: the value and length of the current run, and the value and length of the longest run found so far. The first item initializes both runs. This is why the loop processes the rest of the list with the slice <code class=\"language-text\">labels[1:]</code>.</p><p>Complete the loop so that it first updates the current run and then updates the longest run when necessary. Finally, return the longest run as a tuple.</p></programming-exercise><h2 id=\"developing-a-larger-programming-project\" style=\"position:relative;\">Developing a larger programming project<a href=\"#developing-a-larger-programming-project\" aria-label=\"developing a larger programming project permalink\" class=\"anchor after\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h2><p>This fourth part culminates in a slightly larger programming project, where you get to apply many of the techniques learnt so far.</p><p>Rule No. 1 in tackling any programming project is not trying to solve everything at once. The program should be built out of smaller sections, such as helper functions. You should verify the operation of each part before moving on to the next. If you try to handle too much at once, most likely only chaos ensues.</p><p>To do this you will need a way of testing your functions outside the main function. You can achieve this by defining a main function explicitly, and calling this function from outside any other function in the program. A single function call is then easy to comment out for testing. The first steps in building the following programming project could look like this:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">def</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    points <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span>\n    <span class=\"token comment\"># your program code goes here</span>\n\nmain<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div><p>Now the helper functions can be tested without running the main function:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\"># helper function for determining the grade based on the amount of points</span>\n<span class=\"token keyword\">def</span> <span class=\"token function\">grade</span><span class=\"token punctuation\">(</span>points<span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token comment\"># more code</span>\n\n<span class=\"token keyword\">def</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    all_points <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span>\n    <span class=\"token comment\"># your program code goes here</span>\n\n<span class=\"token comment\"># comment out the main function</span>\n<span class=\"token comment\">#main()</span>\n\n<span class=\"token comment\"># test the helper function</span>\nstudent_points <span class=\"token operator\">=</span> <span class=\"token number\">35</span>\nresult <span class=\"token operator\">=</span> grade<span class=\"token punctuation\">(</span>student_points<span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>result<span class=\"token punctuation\">)</span></code></pre></div><h2 id=\"passing-data-from-one-function-to-another\" style=\"position:relative;\">Passing data from one function to another<a href=\"#passing-data-from-one-function-to-another\" aria-label=\"passing data from one function to another permalink\" class=\"anchor after\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h2><!--- see also section 6-4, some significant overlap--><p>When a program contains multiple functions, the question arises: how do you pass data from one function to another?</p><p>The following example asks the user for some integer values. The program then prints out these values and performs an \"analysis\" on them. The program is divided into three separate functions:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">def</span> <span class=\"token function\">input_from_user</span><span class=\"token punctuation\">(</span>how_many<span class=\"token punctuation\">:</span> <span class=\"token builtin\">int</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span><span class=\"token string-interpolation\"><span class=\"token string\">f\"Please type in </span><span class=\"token interpolation\"><span class=\"token punctuation\">{</span>how_many<span class=\"token punctuation\">}</span></span><span class=\"token string\"> numbers:\"</span></span><span class=\"token punctuation\">)</span>\n    numbers <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span>\n\n    <span class=\"token keyword\">for</span> i <span class=\"token keyword\">in</span> <span class=\"token builtin\">range</span><span class=\"token punctuation\">(</span>how_many<span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n        number <span class=\"token operator\">=</span> <span class=\"token builtin\">int</span><span class=\"token punctuation\">(</span><span class=\"token builtin\">input</span><span class=\"token punctuation\">(</span><span class=\"token string-interpolation\"><span class=\"token string\">f\"Number </span><span class=\"token interpolation\"><span class=\"token punctuation\">{</span>i<span class=\"token operator\">+</span><span class=\"token number\">1</span><span class=\"token punctuation\">}</span></span><span class=\"token string\">: \"</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n        numbers<span class=\"token punctuation\">.</span>append<span class=\"token punctuation\">(</span>number<span class=\"token punctuation\">)</span>\n\n    <span class=\"token keyword\">return</span> numbers\n\n<span class=\"token keyword\">def</span> <span class=\"token function\">print_result</span><span class=\"token punctuation\">(</span>numbers<span class=\"token punctuation\">:</span> <span class=\"token builtin\">list</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"The numbers are: \"</span><span class=\"token punctuation\">)</span>\n    <span class=\"token keyword\">for</span> number <span class=\"token keyword\">in</span> numbers<span class=\"token punctuation\">:</span>\n        <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>number<span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">def</span> <span class=\"token function\">analyze</span><span class=\"token punctuation\">(</span>numbers<span class=\"token punctuation\">:</span> <span class=\"token builtin\">list</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    mean <span class=\"token operator\">=</span> <span class=\"token builtin\">sum</span><span class=\"token punctuation\">(</span>numbers<span class=\"token punctuation\">)</span> <span class=\"token operator\">/</span> <span class=\"token builtin\">len</span><span class=\"token punctuation\">(</span>numbers<span class=\"token punctuation\">)</span>\n    <span class=\"token keyword\">return</span> <span class=\"token string-interpolation\"><span class=\"token string\">f\"There are altogether </span><span class=\"token interpolation\"><span class=\"token punctuation\">{</span><span class=\"token builtin\">len</span><span class=\"token punctuation\">(</span>numbers<span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span></span><span class=\"token string\"> numbers, the mean is </span><span class=\"token interpolation\"><span class=\"token punctuation\">{</span>mean<span class=\"token punctuation\">}</span></span><span class=\"token string\">, the smallest is </span><span class=\"token interpolation\"><span class=\"token punctuation\">{</span><span class=\"token builtin\">min</span><span class=\"token punctuation\">(</span>numbers<span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span></span><span class=\"token string\"> and the greatest is </span><span class=\"token interpolation\"><span class=\"token punctuation\">{</span><span class=\"token builtin\">max</span><span class=\"token punctuation\">(</span>numbers<span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span></span><span class=\"token string\">\"</span></span>\n\n<span class=\"token comment\"># the \"main function\" using these functions</span>\ninputs <span class=\"token operator\">=</span> input_from_user<span class=\"token punctuation\">(</span><span class=\"token number\">5</span><span class=\"token punctuation\">)</span>\nprint_result<span class=\"token punctuation\">(</span>inputs<span class=\"token punctuation\">)</span>\nanalysis_result <span class=\"token operator\">=</span> analyze<span class=\"token punctuation\">(</span>inputs<span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>analysis_result<span class=\"token punctuation\">)</span></code></pre></div><p>When the program is executed, it could go like this:</p><sample-output><p>Please type in 5 numbers:\nNumber 1: <strong>10</strong>\nNumber 2: <strong>34</strong>\nNumber 3: <strong>-32</strong>\nNumber 4: <strong>99</strong>\nNumber 5: <strong>-53</strong>\nThe numbers are:\n10\n34\n-32\n99\n-53\nThere are altogether 5 numbers, the mean is 11.6, the smallest is -53 and the greatest is 99</p></sample-output><p>The idea here is that the main function \"saves\" all data processed by the program. In this case all that is needed is the input from the user in the variable <code class=\"language-text\">inputs</code>.</p><p>If the input is needed in a function, it is passed as an argument. This happens with the functions <code class=\"language-text\">print_result</code> and <code class=\"language-text\">analyze</code>. If the function produces data that is needed elsewhere in the program, the function returns it with the <code class=\"language-text\">return</code> command, and it is stored in a variable in the main function. This happens with the functions <code class=\"language-text\">input_from_user</code> and <code class=\"language-text\">analyze</code>.</p><p>You could use the global variable <code class=\"language-text\">inputs</code> from the main function directly in the helper functions. We have already covered why that is a bad idea, but <a href=\"https://softwareengineering.stackexchange.com/questions/148108/why-is-global-state-so-evil\" target=\"_blank\" rel=\"noopener noreferrer\">here is another explanation</a>. If functions are able to change a global variable, unexpected things may start happening in the program, especially when the number of functions grows large.</p><p>Passing data into and out of functions is best handled by arguments and return values.</p><p>You could also separate the implicit main function in the example above into its own function. Then the variable <code class=\"language-text\">inputs</code> would no longer be a global variable, but instead a local variable within the <code class=\"language-text\">main</code> function:</p><div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\"># your main function goes here</span>\n<span class=\"token keyword\">def</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    inputs <span class=\"token operator\">=</span> input_from_user<span class=\"token punctuation\">(</span><span class=\"token number\">5</span><span class=\"token punctuation\">)</span>\n    print_result<span class=\"token punctuation\">(</span>inputs<span class=\"token punctuation\">)</span>\n    analysis_result <span class=\"token operator\">=</span> analyze<span class=\"token punctuation\">(</span>inputs<span class=\"token punctuation\">)</span>\n\n    <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>analysis_result<span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># run the main function</span>\nmain<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div><programming-exercise name='Grade statistics' tmcname='part04-38_grade_statistics'><p>In this exercise you will write a program for printing out grade statistics for a university course.</p><p>The program asks the user for results from different students on the course. These include exam points and numbers of exercises completed. The program then prints out statistics based on the results.</p><p>Exam points are integers between 0 and 20. The number of exercises completed is an integer between 0 and 100.</p><p>The program keeps asking for input until the user types in an empty line. You may assume all lines contain valid input, which means that there are two integers on each line, or the line is empty.</p><p>And example of how the data is typed in:</p><sample-output><p>Exam points and exercises completed: <strong>15 87</strong>\nExam points and exercises completed: <strong>10 55</strong>\nExam points and exercises completed: <strong>11 40</strong>\nExam points and exercises completed: <strong>4 17</strong>\nExam points and exercises completed:\nStatistics:</p></sample-output><p>When the user types in an empty line, the program prints out statistics. They are formulated as follows:</p><p>The exercises completed are converted into <em>exercise points</em>, so that completing at least 10% of the exercises grants one point, 20% grants two points, and so forth. Completing all 100 exercises grants 10 exercise points. The number of exercise points granted is an integer value, rounded down.</p><p>The grade for the course is determined based on the following table:</p><table>\n<thead>\n<tr>\n<th align=\"center\">exam points + exercise points</th>\n<th align=\"center\">grade</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"center\">0–14</td>\n<td align=\"center\">0 (i.e. fail)</td>\n</tr>\n<tr>\n<td align=\"center\">15–17</td>\n<td align=\"center\">1</td>\n</tr>\n<tr>\n<td align=\"center\">18–20</td>\n<td align=\"center\">2</td>\n</tr>\n<tr>\n<td align=\"center\">21–23</td>\n<td align=\"center\">3</td>\n</tr>\n<tr>\n<td align=\"center\">24–27</td>\n<td align=\"center\">4</td>\n</tr>\n<tr>\n<td align=\"center\">28–30</td>\n<td align=\"center\">5</td>\n</tr>\n</tbody>\n</table><p>There is also an exam cutoff threshold. If a student received less than 10 points from the exam, they automatically fail the course, regardless of their total number of points.</p><p>With the example input from above the program would print out the following statistics:</p><sample-output><pre>\nStatistics:\nPoints average: 14.5\nPass percentage: 75.0\nGrade distribution:\n  5:\n  4:\n  3: *\n  2:\n  1: **\n  0: *\n</pre></sample-output><p>Floating point numbers should be printed out with one decimal precision.</p><p><strong>NB:</strong> this exercise doesn't ask you to write any specific functions, so you should <strong>not</strong> place any code within an <code class=\"language-text\">if __name__ == \"__main__\"</code> block. If any functionality in your program is e.g. in the <code class=\"language-text\">main</code> function, you should include the code calling this function normally, and not contain it in an <code class=\"language-text\">if</code> block like in the exercises which specify certain functions.</p><p><strong>Hint:</strong></p><p>The user input in this program consists of lines with two integer values:</p><sample-output><p>Exam points and exercises completed: <strong>15 87</strong></p></sample-output><p>You have to first split the input line in two and then convert the sections into integers with the <code class=\"language-text\">int</code> function. Splitting the input can be achieved in the same way as in the exercise <a href=\"/programming-digital-humanities/part-4/2-more-functions#programming-exercise-first-second-and-last-words\">First, second and last words</a>, but there is a simpler way as well. The string method <code class=\"language-text\">split</code> will chop the input up nicely. You will find more information by searching for <em>python string split</em> online.</p></programming-exercise><h2 id=\"summary-of-parts-1-4\" style=\"position:relative;\">Summary of parts 1-4<a href=\"#summary-of-parts-1-4\" aria-label=\"summary of parts 1 4 permalink\" class=\"anchor after\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h2><p>In parts 1-4, you've learned about the fundamental building blocks of programs: the flow control instructions that dictate the order of execution, variables for organizing and storing data, as well as functions as a means to organise and package code into reusable components. With these, you've learned a basic capability to read code and follow its execution.</p><p>At this point, if you've not already done so, check out the <a href=\"https://jiemakel.gitbook.io/cl4hss/data-processing-fundamental-concepts-of-programming-for-humanists\" target=\"_blank\" rel=\"noopener noreferrer\">fundamental concepts of programming for humanists</a> part of The <a href=\"https://jiemakel.gitbook.io/cl4hss/\" target=\"_blank\" rel=\"noopener noreferrer\">Computational Literacy course</a>, which tries to cram the absolute fundamentals into as minimal a space as possible. By seeing the same thing explained concisely a bit differently, this may improve your undestanding (as has been verified by prior students on this course). Additionally, because the fundamental concepts -section also shows you these fundamentals across not just Python but also R and Scala, you'll get an insight into how these concepts are not just features of a particular language, but present in most with minor differences. Finally, the fundamental concepts section also provides a handy <a href=\"https://nbviewer.jupyter.org/github/jiemakel/dhintro/blob/master/programming_cheatsheet.ipynb\" target=\"_blank\" rel=\"noopener noreferrer\">cheatsheet of key concepts</a> which may be helpful as a reminder and an overview.</p></div>","frontmatter":{"path":"/part-4/5-strings-and-lists/","title":"More strings and lists"},"fileAbsolutePath":"/home/runner/work/programming-digital-humanities/programming-digital-humanities/data/part-4/5-strings-and-lists.md"},"allPages":{"edges":[{"node":{"id":"5d899565-03f0-55ea-8073-4ee194950c4d","frontmatter":{"path":"/ai-tutor/","title":"Using the AI tutor on the course"}}},{"node":{"id":"c6198176-c061-5cca-a182-cc80cdb14e38","frontmatter":{"path":"/all-exercises/","title":"All exercises"}}},{"node":{"id":"6c2bb78f-1497-5d72-a964-8cb55e47559e","frontmatter":{"path":"/error_messages/","title":"Common error messages"}}},{"node":{"id":"96430bcf-4526-5d85-9206-c7336a6be270","frontmatter":{"path":"/faq/","title":"Frequently asked questions"}}},{"node":{"id":"0d6f8da4-7d4f-5228-a832-7ff574e63570","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"249c8a64-20fb-510c-8b3e-f5b3b72fe028","frontmatter":{"path":"/grading/","title":"Grading"}}},{"node":{"id":"5b5e6522-0d23-577b-9ba9-4ecd1c1c5d24","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"bcdb2bdc-e92c-5c75-8dd1-13f57e1129da","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"d54c2afb-004d-500e-9d19-aa3d154a39a5","frontmatter":{"path":"/part-0/","title":"Part 0: See what an agent can build"}}},{"node":{"id":"fff8e65b-0281-55fe-a21a-9e7e872ddc33","frontmatter":{"path":"/part-1/1-getting-started/","title":"Getting started"}}},{"node":{"id":"9fa0a7fc-c673-56ad-9dc8-bf6aeb6d3b05","frontmatter":{"path":"/part-1/2-information-from-the-user/","title":"Information from the user"}}},{"node":{"id":"c2286d4a-4cf7-5fc4-ab0f-f7cd2acf49df","frontmatter":{"path":"/part-1/3-more-about-variables/","title":"More about variables"}}},{"node":{"id":"3ebadf79-0e16-5ed7-bcb2-bec3731cffc2","frontmatter":{"path":"/part-1/4-arithmetic-operations/","title":"Arithmetic operations"}}},{"node":{"id":"c77004ff-fe4d-5533-b77f-c861fa42430d","frontmatter":{"path":"/part-1/5-conditional-statements/","title":"Conditional statements"}}},{"node":{"id":"50ee91dc-7fe8-508d-940f-577449a335f4","frontmatter":{"path":"/part-1/","title":"Part 1"}}},{"node":{"id":"d963c6b4-b2d4-59ff-b310-265b28e2c8d2","frontmatter":{"path":"/part-2/1-programming-terminology/","title":"Programming terminology"}}},{"node":{"id":"075f597d-a49f-59c1-ad1a-d68ba19aa0f4","frontmatter":{"path":"/part-2/2-else-elif/","title":"More conditionals"}}},{"node":{"id":"26776fa9-27c7-5554-990b-09d6661a07a8","frontmatter":{"path":"/part-2/3-combining-conditions/","title":"Combining conditions"}}},{"node":{"id":"1da4e234-e81f-5494-b2d4-96e30cbb4464","frontmatter":{"path":"/part-2/4-simple-loops/","title":"Simple loops"}}},{"node":{"id":"db78d781-f405-5904-91bf-e96f7f04353f","frontmatter":{"path":"/part-2/","title":"Part 2"}}},{"node":{"id":"10808e92-b68f-58c3-b767-cf5e9c0efeb7","frontmatter":{"path":"/part-3/1-loops-with-conditions/","title":"Loops with conditions"}}},{"node":{"id":"bceed420-a129-52a7-81ed-dafd67ad38df","frontmatter":{"path":"/part-3/2-working-with-strings/","title":"Working with strings"}}},{"node":{"id":"e2dfd9d3-07fa-59a0-b748-370355606256","frontmatter":{"path":"/part-3/3-more-loops/","title":"More loops"}}},{"node":{"id":"5b546d28-ac00-564f-9d75-cc09c4382b64","frontmatter":{"path":"/part-3/4-defining-functions/","title":"Defining functions"}}},{"node":{"id":"12122cff-c09e-53f1-8b76-8517f845e08c","frontmatter":{"path":"/part-3/","title":"Part 3"}}},{"node":{"id":"8fbb5845-152f-5be7-ac92-fa280093b2a2","frontmatter":{"path":"/part-4/1-vscode/","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b8d090a9-7e49-5ab7-a5a1-d126179dd9a0","frontmatter":{"path":"/part-4/2-more-functions/","title":"More functions"}}},{"node":{"id":"271308ca-b267-5c86-8051-8a1a3480cfcb","frontmatter":{"path":"/part-4/3-lists/","title":"Lists"}}},{"node":{"id":"e5a8fd16-890d-509e-9666-9c622648c18a","frontmatter":{"path":"/part-4/4-definite-iteration/","title":"Definite iteration"}}},{"node":{"id":"ca4caea3-eaad-5cc3-a1fa-83b9bb90a25e","frontmatter":{"path":"/part-4/5-strings-and-lists/","title":"More strings and lists"}}},{"node":{"id":"adeebce2-76be-5528-90a1-6feaa066fcd8","frontmatter":{"path":"/part-4/6-data-structures/","title":"Data structures and data shapes"}}},{"node":{"id":"07e761ca-39a1-50a1-97d8-9149e4737e89","frontmatter":{"path":"/part-4/","title":"Part 4"}}},{"node":{"id":"80fe5d23-31eb-5ff6-b339-99e173e9279f","frontmatter":{"path":"/part-5/0-working-with-ai/","title":"Working with AI from Part 5 onward"}}},{"node":{"id":"111a6af4-b8a7-5d71-8966-f750f6c161a6","frontmatter":{"path":"/part-5/1-tables-as-nested-lists/","title":"Tables as nested lists"}}},{"node":{"id":"7e2cf429-03ea-5e8c-86ce-62c71c689a75","frontmatter":{"path":"/part-5/2-references/","title":"References and transformed results"}}},{"node":{"id":"3901dc36-3116-5444-9120-6b38454e245f","frontmatter":{"path":"/part-5/3-grouping-and-structured-records/","title":"Grouping and structured records"}}},{"node":{"id":"076a1db2-8d62-5725-ad72-c43ea25897c6","frontmatter":{"path":"/part-5/4-small-database/","title":"Building a small database"}}},{"node":{"id":"cf823ebf-3d67-53ca-8fcf-9b53fee10c1d","frontmatter":{"path":"/part-5/5-in-memory-analysis-pipeline/","title":"An in-memory analysis pipeline"}}},{"node":{"id":"92cbc670-d401-53b8-949f-9725b81a5e4a","frontmatter":{"path":"/part-5/","title":"Part 5"}}},{"node":{"id":"52ecf5a7-9266-58c8-bac5-407b1a29a9dd","frontmatter":{"path":"/part-6/2-writing-files/","title":"Writing and exporting files"}}},{"node":{"id":"a296984d-7503-558e-86da-ddb801f8a59b","frontmatter":{"path":"/part-6/3-scope-of-variables/","title":"Make data flow explicit"}}},{"node":{"id":"98bdbc42-0ebc-5f9c-b767-b987266a0040","frontmatter":{"path":"/part-6/4-modules/","title":"Use existing libraries"}}},{"node":{"id":"0ca5c730-bd8e-5099-9768-8fcb8eb0b7b4","frontmatter":{"path":"/part-6/1-reading-files/","title":"Reading files"}}},{"node":{"id":"11759056-5a15-5820-a693-30f4df62f118","frontmatter":{"path":"/part-6/","title":"Part 6"}}},{"node":{"id":"6e748518-2c76-5428-8779-0f0455b17a0d","frontmatter":{"path":"/part-7/0-dataframes-and-views/","title":"Working with pandas dataframes"}}},{"node":{"id":"047a5114-7ebb-5a5c-8ed0-eadf81564546","frontmatter":{"path":"/part-7/1-times-and-dates/","title":"Times and dates"}}},{"node":{"id":"7cc9d1d2-fb68-52da-b5ec-034bf9f4585f","frontmatter":{"path":"/part-7/3-more-features/","title":"More Python features"}}},{"node":{"id":"5055044d-7938-5e5d-be5c-dfb0ff2c81b4","frontmatter":{"path":"/part-7/2-data-processing/","title":"Data processing"}}},{"node":{"id":"cea3f325-56bb-5a93-885e-275e465c1ced","frontmatter":{"path":"/part-7/","title":"Part 7"}}},{"node":{"id":"3abea309-839b-562f-bb00-3be40d963200","frontmatter":{"path":"/part-8/","title":"Part 8"}}}]}},"pageContext":{}},
    "staticQueryHashes": ["1357757885","994120085"]}